您如何将文本形式安装到文本字段?
我正在制作一个简单的分数表,并且我要让用户创建自定义表。显然,我不希望他们仅放入任何整数Willy Nilly,因此我试图将创建给Textfield的非选法者连接起来,但是我不确定如何按照我的Javafx项目的设置方式进行操作。
public class MainMenuCtrl {
@FXML
private TextField playsFld;
@FXML
void initialize() {
UnaryOperator<TextFormatter.Change> playsValidator = change -> {
if (change.getText().matches("\\d{1,2,3,4,5,6}")){
return change;
} else {
change.setText("");
change.setRange(change.getRangeStart(), change.getRangeEnd());
return change;
}
};
}
}
I am making a simple score sheet and I'm at the phase where I want to let the user create a custom sheet. Obviously I don't want them to just put in any integer willy nilly so I'm trying to connect the UnararyOperator I created to the TextField, but I'm not sure how to do it the way my JavaFX project is set up.
public class MainMenuCtrl {
@FXML
private TextField playsFld;
@FXML
void initialize() {
UnaryOperator<TextFormatter.Change> playsValidator = change -> {
if (change.getText().matches("\\d{1,2,3,4,5,6}")){
return change;
} else {
change.setText("");
change.setRange(change.getRangeStart(), change.getRangeEnd());
return change;
}
};
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,正则表达式很奇怪,而且文档似乎并不总是正确的。玩了一下后我发现:
意思是 1-10,但只有 1 位数字,除非它特别是 10
意思是 1-6,只有 1 位数字
意思是 1-100 最多 3 位数字,但只有百位中的 1
*0 是匹配的前面的字符出现 0 次。我很确定这就是锁定位数的方法
?:意味着前面的字符可能不会出现在字符串中。我认为这使得你不需要 01.
[nn] 用于搜索的字符。我很确定上面的[1-9][0-9]是十位的1-9,个位的0-9。所以你可以为每个地方添加括号。
|用于匹配任何一个元素。所以 |100 就是精确匹配 100。
$是告诉计算机你必须在结束之前匹配。
It turns out regex is weird and the documentation doesn't always seem to be right. After playing around a bit I found:
Means 1-10, but only 1 digit unless it is specifically 10
Means 1-6 and only 1 digit
Means 1-100 up to 3 digits but only a 1 in the hundreds place
*0 is match the preceding character 0 times. I'm pretty sure this is how you lock the number of digits
?: means the preceding character might not show up in the string. I think this makes it so you don't need 01.
[n-n] are for the characters searched. I'm pretty sure [1-9][0-9] in the above are 1-9 for the tens place and 0-9 for the ones. So you could add brackets for every place.
| is for matching any one element. So |100 is to match 100 exactly.
$ is to tell the computer that you have to match before the end.