textfield.setDocument(new PlainDocument() { int MAX_LENGTH = 20; public void insertString(int offset, String s, AttributeSet attributeSet) throws BadLocationException { if (s == null || offset < 0) { return; } for (int i = 0; i < s.length(); i++) { if (getLength() > MAX_LENGTH - 1) { break; } super.insertString(offset + i, s.substring(i, i + 1), attributeSet); } return; } });
直接插入这段代码就可以了,直接替换掉textfield的Document。默认最大长度是20,想改成什么样自己调吧。
发表于 @ 2009年02月20日 11:49:00 | 评论( loading... ) | 编辑| 举报| 收藏