JavaFX:如果 TextField 的条件在 ActionEvent 上无法正常工作?
JavaFX: if condition forTextField not working properly on ActionEvent?
我有一段代码,JavaFX 中的登录表单。它只是一个原型,它基于一个没有涵盖此类主题的教程。我想添加一个 TextField 验证,我是这样做的:
Button btn = new Button("Login");
HBox hBtn = new HBox(10);
hBtn.setAlignment(Pos.BOTTOM_RIGHT);
hBtn.getChildren().add(btn);
grid.add(hBtn, 1, 4);
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
if (userTextField.getText().trim().isEmpty() && !pwField.getText().trim().isEmpty()) {
btn.setOnAction(event
-> {
actiontarget.setFill(Color.FIREBRICK);
actiontarget.setText("No login provided!");
});
} else if (pwField.getText().trim().isEmpty() && !userTextField.getText().trim().isEmpty()) {
btn.setOnAction(event
-> {
actiontarget.setFill(Color.FIREBRICK);
actiontarget.setText("Please provide a password!");
});
} else if (userTextField.getText().trim().isEmpty() && pwField.getText().trim().isEmpty()) {
btn.setOnAction(event
-> {
actiontarget.setFill(Color.FIREBRICK);
actiontarget.setText("Please provide login and password!");
});
} else {
btn.setOnAction(event
-> {
actiontarget.setFill(Color.GREEN);
actiontarget.setText("Login succesfull");
});
}
问题是,此代码总是从第三个条件返回文本:Please provide login and password!
,这些字段中的输入无关紧要。我可以只提供密码,只提供登录名,两者,或两者的 none,结果总是一样的。
我是不是漏掉了什么?这是一个(非常)错误的方法吗?还是我只是累了,应该去睡觉了?
干杯!
if/else if
在您创建 GUI 时计算。
这意味着已完成对 TextField
的初始化值(或默认值)的检查。
将检查移至事件处理程序以检查单击按钮时的值:
...
grid.add(actiontarget, 1, 6);
btn.setOnAction(evt -> {
String user = userTextField.getText().trim();
String password = pwField.getText().trim();
if (!(user.isEmpty() || password.isEmpty())) {
actiontarget.setFill(Color.GREEN);
actiontarget.setText("Login succesfull");
} else {
actiontarget.setFill(Color.FIREBRICK);
if (user.isEmpty()) {
actiontarget.setText(password.isEmpty() ? "Please provide login and password!" : "No login provided!");
} else {
actiontarget.setText("Please provide a password!");
}
}
});
我有一段代码,JavaFX 中的登录表单。它只是一个原型,它基于一个没有涵盖此类主题的教程。我想添加一个 TextField 验证,我是这样做的:
Button btn = new Button("Login");
HBox hBtn = new HBox(10);
hBtn.setAlignment(Pos.BOTTOM_RIGHT);
hBtn.getChildren().add(btn);
grid.add(hBtn, 1, 4);
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
if (userTextField.getText().trim().isEmpty() && !pwField.getText().trim().isEmpty()) {
btn.setOnAction(event
-> {
actiontarget.setFill(Color.FIREBRICK);
actiontarget.setText("No login provided!");
});
} else if (pwField.getText().trim().isEmpty() && !userTextField.getText().trim().isEmpty()) {
btn.setOnAction(event
-> {
actiontarget.setFill(Color.FIREBRICK);
actiontarget.setText("Please provide a password!");
});
} else if (userTextField.getText().trim().isEmpty() && pwField.getText().trim().isEmpty()) {
btn.setOnAction(event
-> {
actiontarget.setFill(Color.FIREBRICK);
actiontarget.setText("Please provide login and password!");
});
} else {
btn.setOnAction(event
-> {
actiontarget.setFill(Color.GREEN);
actiontarget.setText("Login succesfull");
});
}
问题是,此代码总是从第三个条件返回文本:Please provide login and password!
,这些字段中的输入无关紧要。我可以只提供密码,只提供登录名,两者,或两者的 none,结果总是一样的。
我是不是漏掉了什么?这是一个(非常)错误的方法吗?还是我只是累了,应该去睡觉了?
干杯!
if/else if
在您创建 GUI 时计算。
这意味着已完成对 TextField
的初始化值(或默认值)的检查。
将检查移至事件处理程序以检查单击按钮时的值:
...
grid.add(actiontarget, 1, 6);
btn.setOnAction(evt -> {
String user = userTextField.getText().trim();
String password = pwField.getText().trim();
if (!(user.isEmpty() || password.isEmpty())) {
actiontarget.setFill(Color.GREEN);
actiontarget.setText("Login succesfull");
} else {
actiontarget.setFill(Color.FIREBRICK);
if (user.isEmpty()) {
actiontarget.setText(password.isEmpty() ? "Please provide login and password!" : "No login provided!");
} else {
actiontarget.setText("Please provide a password!");
}
}
});