问题
username = new JTextField("");
username.setBounds(330, 550, 230, 30);
username.addActionListener(this);
username.requestFocus(); // sets focus on JTextField
this.add(username);
回答1:
JTextField username = new JTextField("") ;
final int limit = 10;
username .setDocument(new PlainDocument(){
@Override
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if(getLength() + str.length() <= limit)
super.insertString(offs, str, a);
}
});
来源:https://stackoverflow.com/questions/6146274/how-do-i-limit-the-length-of-the-input-i-want-inside-a-jtextfield