仅接受正数的Textfield -Javafx

发布于 2025-01-23 14:14:49 字数 274 浏览 5 评论 0原文

我正在尝试在Javafx(使用场景构建器)中创建一个仅接受正数的TextField。

我实际上是为了制作一个“信用卡号”的文本字段,该文本字段接受13-16个数字值,而不包括开始时的减号,并且可以在文本开头接受0。

另外,由于这是一个“信用卡号” Textfield,我正在寻找一种可以在每4个字母/号码插入它之后插入自动空间的方式。

注意: 我知道如何创建一个仅接受数字的文本字段,但它在开始时也接受减号,并且不接受0作为第一个输入。

I'm trying to create a TextField in JavaFX (using Scene Builder) that accepts only positive numbers.

I'm trying actually to make a TextField for a 'Credit Card Number' which accepts 13 - 16 numeric values, not including the minus sign at the beginning, and which can accept 0 at the beginning of the text.

Also, since it's a 'Credit Card Number' TextField, I'm looking for a way where I can insert automatic space after each 4 letters/numbers inserted to it.

Note: I know how to create a TextField that accepts only numbers but it also accepts minus sign at the beginning and doesn't accept the 0 as the first input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浅忆 2025-01-30 14:14:49

这是我一直在使用的内容,所以我的Textfield仅接受数字,它的工作方式如下所示,

public static void digitsTxtFld(TextField field) {
    field.setTextFormatter(new TextFormatter<Integer>(change -> {
        String newText = change.getControlNewText();
        if (newText.matches("\\d*")) {
            return change;
        }
        return null;
    }));
}

并且要设置字符限制,我使用此功能,

    public static void setTextLimit(TextArea textArea, int length) {
        textArea.setTextFormatter(new TextFormatter<>(change -> {
            String string = change.getControlNewText();
            if (string.length() > length) {
                textArea.positionCaret(string.length());
                return change;
            } else {
                return null;
            }
        }));
    }

编辑:
显然,一个文本格式化是一个文本格式,因为您称之为的第二个将替换第一个,并且您的字段只能一次运行。因此,我进行了一个联合格式,如下所示:

    public static void setTextLimitToDigitFld(TextField field, int length) {
        field.setTextFormatter(new TextFormatter<Integer>(change -> {
            String newText = change.getControlNewText();
            if (newText.matches("\\d*") && newText.length() > length) {
                return change;
            }
            return null;
        }));
    }

Here is what I have been using so my TextField only accepts digits and it works like a charm as below,

public static void digitsTxtFld(TextField field) {
    field.setTextFormatter(new TextFormatter<Integer>(change -> {
        String newText = change.getControlNewText();
        if (newText.matches("\\d*")) {
            return change;
        }
        return null;
    }));
}

And to set a character limit I use this,

    public static void setTextLimit(TextArea textArea, int length) {
        textArea.setTextFormatter(new TextFormatter<>(change -> {
            String string = change.getControlNewText();
            if (string.length() > length) {
                textArea.positionCaret(string.length());
                return change;
            } else {
                return null;
            }
        }));
    }

Edit:
Apparently one cannot have both text formatters as the second one you call will replace the first and your field will only run with one at a time. So I made a joint formatter as follows:

    public static void setTextLimitToDigitFld(TextField field, int length) {
        field.setTextFormatter(new TextFormatter<Integer>(change -> {
            String newText = change.getControlNewText();
            if (newText.matches("\\d*") && newText.length() > length) {
                return change;
            }
            return null;
        }));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文