栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaFX输入验证文本字段

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaFX输入验证文本字段

textField.focusedProperty().addListener((arg0, oldValue, newValue) -> {        if (!newValue) { //when focus lost if(!textField.getText().matches("[1-5]\.[0-9]|6\.0")){     //when it not matches the pattern (1.0 - 6.0)     //set the textField empty     textField.setText(""); }        }    });

您还可以将模式更改为,

[1-5](.[0-9]){0,1}|6(.0){0,1}
然后
1,2,3,4,5,6
也可以(不仅是
1.0,2.0,...

更新 这是一个小型测试应用程序,允许使用值1(.00)至6(.00):

public class JavaFxSample extends Application {@Overridepublic void start(Stage primaryStage) {    primaryStage.setTitle("Enter number and hit the button");    GridPane grid = new GridPane();    grid.setAlignment(Pos.CENTER);    Label label1To6 = new Label("1.0-6.0:");    grid.add(label1To6, 0, 1);    TextField textField1To6 = new TextField();    textField1To6.focusedProperty().addListener((arg0, oldValue, newValue) -> {        if (!newValue) { // when focus lost     if (!textField1To6.getText().matches("[1-5](\.[0-9]{1,2}){0,1}|6(\.0{1,2}){0,1}")) {         // when it not matches the pattern (1.0 - 6.0)         // set the textField empty         textField1To6.setText("");     } }        });    grid.add(textField1To6, 1, 1);    grid.add(new Button("Hit me!"), 2, 1);    Scene scene = new Scene(grid, 300, 275);    primaryStage.setScene(scene);    primaryStage.show();}public static void main(String[] args) {    launch(args);}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号