限制和格式化JTextfield [英] Limiting and formatting a JTextfield

查看:143
本文介绍了限制和格式化JTextfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 JTextfield ,用户输入社会安全号码。 SSN有9位数和2个破折号,共11个字符。

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.

如何将 JTextField 限制为在11个字符的4 th 和7 th 位置的文本字段中有11个字符并且还有2个破折号?

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?

推荐答案

您可能想要使用: JFormattedTextField

有关更多帮助,请参阅此内容:如何使用格式化文本字段

See this for more help : How to Use Formatted Text Fields

这也可能有所帮助: MaskFormatter

以下是一个示例代码段:

Here is a sample snippet :

MaskFormatter formatter = new MaskFormatter("####-###-####");

编辑:

经过一番研究后,我发现 DocumentFilter 可以做得更好,

After some research I found out that a DocumentFilter would do things better,

详情请见:实施文件过滤器

这篇关于限制和格式化JTextfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆