开发者

how do i limit the length of the input i want inside a Jtextfield?

开发者 https://www.devze.com 2023-03-08 12:52 出处:网络
username = new JTextField(\"\"); us开发者_如何学JAVAername.setBounds(330, 550, 230, 30); username.addActionListener(this);
    username = new JTextField("");
    us开发者_如何学JAVAername.setBounds(330, 550, 230, 30);
    username.addActionListener(this);
    username.requestFocus(); // sets focus on JTextField
    this.add(username);


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);
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消