JTextField textField = new JTextField();
//限制输入文本框的长度
textField.setDocument(new PlainDocument() {
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
String text = textField.getText();
if (text.length() + str.length() > 10) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(MFrame.this, "Larger than 10 characters!");
return;
}
super.insertString(offs, str, a);
}
});
Swing 文本框(JTextFiled)的限制输入长度
最新推荐文章于 2022-10-28 20:40:16 发布