限制JTextField中的输入长度不起作用 [英] Limiting Length of Input in JTextField is not working
本文介绍了限制JTextField中的输入长度不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图限制用户可以在文本字段中输入的字符的最大长度,但似乎无效。
I am trying to limit the max length of character a user can input in a textfield but It seems to be not working.
以下是代码:
text2 = new JTextField("Enter text here",8);
我有什么问题吗?如何使限制正常工作?
Is there anything I am doing wrong? How can I make the limit to work properly?
推荐答案
构造函数
new JTextField("Enter text here",8);
将可见列数设置为8,但不限制您输入更多。
sets the number of visible columns to 8 but doesn't restrict you from entering more.
您可以使用 DocumentFilter 限制输入长度。
You could use a DocumentFilter to restrict the input length.
这篇关于限制JTextField中的输入长度不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文