JFormattedTextField 问题
嘿伙计们,我想知道是否可以设置 JFormattedTextField 来自动设置电子邮件输入格式。例如,当用户输入电子邮件地址时,我希望它接受如下内容: [email ;protected]
但我希望它始终像这样 [email protected] (net,com,edu)
基本上我总是想要“@”和“.”
Hey guys, I was wondering if there is anyway I can set a JFormattedTextField to have an auto format of an email input. For example, When the user type an email address I want it to accept something like this: [email protected]
But I want it to always be like this [email protected](net,com,edu)
basically i always want the '@' and '.'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最好的选择是继承
AbstractFormatter
并使用电子邮件的正则表达式,例如:请注意,我让您发现电子邮件的正确正则表达式;您可以采用简单的方法,或者如果您愿意,请检查一个 RFC,该 RFC 描述了一页长的正则表达式,该正则表达式根据实际规范涵盖了电子邮件,但也许您可以简化您的要求;-)
I think your best bet is to subclass
AbstractFormatter
and use a regexp for email, something like:Note that I let you discover the right regexp for an email; you can the easy way, or if you prefer, check one RFC that describes a one-page long regexp that covers emails as per the real specs, but maybe you can simplify your requirements ;-)
没有简单的方法(也就是说,没有 JFormattedTextField.autoEmail() 这样的东西)。但编写这样的代码并不难。在用户按下按钮或其他输入内容的字段中,如果没有 @ 和 ,则不接受输入。人物。或者快速检查一下它们是否包含它,如果不包含,则将其附加到末尾。
No easy way (that is, no such thing as JFormattedTextField.autoEmail()). But it wouldn't be hard to code something like this. In the field where the user presses a button or whatever to enter what's in the field, just don't accept the input if it doesn't have the @ and . characters. Either that or do a quick check to see if they included it, and if not, append it to the end.