JFormattedTextField 问题

发布于 2024-11-07 04:49:07 字数 423 浏览 3 评论 0原文

嘿伙计们,我想知道是否可以设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相守太难 2024-11-14 04:49:07

我认为最好的选择是继承 AbstractFormatter 并使用电子邮件的正则表达式,例如:

public class EmailFormatter extends AbstractFormatter {
    @Override public Object stringToValue(String string) throws ParseException {
        Matcher matcher = regexp.matcher(string);
        if (matcher.matches())
            return string;
        throw new ParseException("Not an email", 0);
    }

    @Override public String valueToString(Object value) {
        return value;
    }

    final private Pattern regexp = Pattern.compile("EMAIL REGEXP TO FIND BY YOURSELF");
}

...
JFormattedTextField email = new JFormattedTextField(new EmailFormatter());

请注意,我让您发现电子邮件的正确正则表达式;您可以采用简单的方法,或者如果您愿意,请检查一个 RFC,该 RFC 描述了一页长的正则表达式,该正则表达式根据实际规范涵盖了电子邮件,但也许您可以简化您的要求;-)

I think your best bet is to subclass AbstractFormatter and use a regexp for email, something like:

public class EmailFormatter extends AbstractFormatter {
    @Override public Object stringToValue(String string) throws ParseException {
        Matcher matcher = regexp.matcher(string);
        if (matcher.matches())
            return string;
        throw new ParseException("Not an email", 0);
    }

    @Override public String valueToString(Object value) {
        return value;
    }

    final private Pattern regexp = Pattern.compile("EMAIL REGEXP TO FIND BY YOURSELF");
}

...
JFormattedTextField email = new JFormattedTextField(new EmailFormatter());

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 ;-)

辞取 2024-11-14 04:49:07

没有简单的方法(也就是说,没有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文