限制和格式化 JTextfield Java
可能的重复:
限制和格式化 JTextfield
我有一个 JTextfield,用户在其中输入“社会保险号” ”。 SSN 由 9 位数字和 2 个破折号组成,总共 11 个字符。
如何将 JTextField 限制为 11 个字符,并且在文本字段中的 11 个字符的第 4 个和第 7 个位置处有 2 个破折号?
一个想法是,我使用 IF 语句声明 soc(这将是 Jtextfield 的输入)必须大于或等于 000000001 且小于或等于 999999999,然后一旦确定。我可以使用一个子字符串来表示位置 0-2 将其分配给变量而不是将其与“-”连接,然后为位置 3-4 创建另一个子字符串并将其与前面的变量连接并添加最后一个“-”,然后完成输入。
这可能吗?这符合逻辑吗?
Possible Duplicate:
Limiting and formatting a JTextfield
I have a JTextfield where a user enters a "social security number". The SSN has 9 digits and 2 dashes for a total of 11 characters.
How do I limit the JTextField to 11 characters and also have 2 dashes in the textfield at the 4th and 7th position of the 11 characters?
one thought, i have is using an IF statement stating that the soc(which would be the input to the Jtextfield) has to be greater than or equal 000000001 and less than or equal to 999999999 then once that is determined. could i use a substring to for positions 0-2 assign that to varible than concatenate that with a "-" then create another subsstring for positions 3-4 and concatenate that with the prior varibles and add the last "-" and then finish the input.
is that possible? does that make logical sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JFormattedTextField 可能看起来很有用,但相信我,事实并非如此。尝试正确配置过于复杂和神秘。
相反,请考虑扩展 Document。通过重写
insertString
、replace
和delete
,您可以控制在何处输入哪些字符。JFormattedTextField may seem helpful, but trust me, it's not. It's overly complicated and arcane to try and properly configure.
Instead, look into extending Document. By overriding
insertString
,replace
, anddelete
, you can control what characters are entered where.