【问题标题】:How can you set a TextArea in JavaFX to allow only integers to be entered? [duplicate]如何在 JavaFX 中设置 TextArea 以只允许输入整数? [复制]
【发布时间】:2017-04-07 14:57:35
【问题描述】:

我想在 JavaFX 中创建一个只接受整数值的 TextArea?谁能给我关于如何实施的建议?

【问题讨论】:

  • 查看链接的问题 - TextArea 的工作方式与 TextField 的工作方式类似。
  • 大概在TextArea 中,您至少希望允许换行符,并且通常可能是空格,因此您需要修改该答案中的正则表达式。

标签: javafx textarea


【解决方案1】:

使用TextFormatter

TextArea textArea = new TextArea();
// allow digits and whitespace:
Pattern allowedText = Pattern.compile("[0-9\\s]*");
TextFormatter formatter = new TextFormatter((TextFormatter.Change c) -> {
    if (allowedText.matcher(c.getText()).matches()) {
        return c ;
    } else {
        return null ;
    }
});

【讨论】:

    猜你喜欢
    • 2015-02-26
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多