1

そのため、インターネット全体を検索し、すべてのトピックで JTextField 入力を制限するためのこのソリューションを見つけました。

public class FixedDocument extends PlainDocument {
  private int limit;
  // optional uppercase conversion
  private boolean toUppercase = false;

  FixedDocument(int limit) {
   super();
   this.limit = limit;
   }

  FixedDocument(int limit, boolean upper) {
   super();
   this.limit = limit;
   toUppercase = upper;
   }

  public void insertString (int offset, String  str, AttributeSet attr) throws BadLocationException {
   if (str == null){
       return;
   }
    if ((getLength() + str.length()) <= limit) {
     if (toUppercase) str = str.toUpperCase();
     super.insertString(offset, str, attr);
     }
   }
}

しかし、私はそのコードに問題があります。このコード行「super.insertString(offset, str, attr);」エラーが発生します:

no suitable method found for insertString(int,java.lanf.String,javax.print.attribute.AttributeSet)
 method javax.swing.text.PlainDocument.insertString(int,java.lang.String,javax.text.AttributeSet) is not applicable
  (actual argument javax.printattribute.AttributeSet cannot be converted to javax.swing.text.AttributeSet by method invocation conversion)

ここで私が間違っていることを誰かが考えましたか?

4

1 に答える 1

2

あなたの問題は、間違った AttributeSet クラスをインポートしていることです。をインポートjavax.print.attribute.AttributeSetする必要があるときに をインポートしjavax.swing.text.AttributeSetていますが、エラー メッセージがこれを示しています。繰り返しますが、私自身は、これが構築された目的であるため、これには DocumentFilter を使用します。

于 2012-05-09T16:09:50.767 に答える