因此,我在整个互联网上进行了搜索,在每个主题中,我都找到了限制 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)
有人知道我在这里做错了什么吗?