运行代码时,如果我输入的凭据不正确,则会显示成功添加的消息以及错误消息,并且我不确定为什么会这样。我们需要为我们的工作显示一条错误消息和一条成功消息。 当代码运行时,并且我输入的凭据不正确时,将显示成功添加的消息以及错误消息,并且我不确定为什么会这样。我们需要显示一条错误消息和一条成功消息。
public class FXMLDocumentController implements Initializable {
private FadeTransition fade = new FadeTransition(
Duration.millis(5000)
);
Employee em = new Employee();
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
TV_id.setCellValueFactory(new PropertyValueFactory<>("ID"));
TV_fname.setCellValueFactory(new PropertyValueFactory<>("FirstName"));
TV_lname.setCellValueFactory(new PropertyValueFactory<>("LastName"));
TV_date.setCellValueFactory(new PropertyValueFactory<>("HireDate"));
fade.setNode(label_success);
fade.setFromValue(1.0);
fade.setToValue(0.0);
fade.setCycleCount(1);
fade.setAutoReverse(false);
}
@FXML
private void handleButtonAddAction(ActionEvent event) {
label_fail.setText("");
label_success.setText("Successfully Added");
int ID = 0, year = 0, month = 0, day = 0;
String fname = "", lname = "";
if (intValidation(TF_id) == true) {
label_id.setTextFill(Color.web("#000000"));
rec_id.setVisible(false);
ID = Integer.parseInt(TF_id.getText());
} else {
rec_id.setVisible(true);
label_id.setTextFill(Color.web("#ff0000"));
}
if (stringValidation(TF_fname) == true) {
label_fname.setTextFill(Color.web("#000000"));
rec_fname.setVisible(false);
fname = TF_fname.getText();
} else {
rec_fname.setVisible(true);
label_fname.setTextFill(Color.web("#ff0000"));
}
if (stringValidation(TF_lname) == true) {
label_fname.setTextFill(Color.web("#000000"));
rec_lname.setVisible(false);
lname = TF_lname.getText();
} else {
rec_lname.setVisible(true);
label_lname.setTextFill(Color.web("#ff0000"));
}
if (intValidation(TF_month) == true) {
label_date.setTextFill(Color.web("#000000"));
rec_month.setVisible(false);
month = Integer.parseInt(TF_month.getText());
} else {
rec_month.setVisible(true);
label_date.setTextFill(Color.web("#ff0000"));
}
if (intValidation(TF_day) == true) {
label_date.setTextFill(Color.web("#000000"));
rec_day.setVisible(false);
day = Integer.parseInt(TF_day.getText());
} else {
rec_day.setVisible(true);
label_date.setTextFill(Color.web("#ff0000"));
}
if (intValidation(TF_year) == true) {
label_date.setTextFill(Color.web("#000000"));
rec_year.setVisible(false);
year = Integer.parseInt(TF_year.getText());
} else {
rec_year.setVisible(true);
label_date.setTextFill(Color.web("#ff0000"));
}
if (!rec_id.isVisible() && !rec_fname.isVisible()
&& !rec_lname.isVisible() && !rec_month.isVisible()
&& !rec_day.isVisible() && !rec_year.isVisible()) {
if (em.addEmployee(fname, lname, ID, year, month, day) == false) {
label_fail.setText("No two employees can have the same ID.");
} else {
updateList();
label_success.setVisible(true);
fade.playFromStart();
TF_id.setText("");
TF_fname.setText("");
TF_lname.setText("");
TF_month.setText("");
TF_day.setText("");
TF_year.setText("");
}
}
}
private void updateList() {
TV_employee.getItems().clear();
}
private boolean intValidation(TextField txt) {
if (txt.getText().isEmpty() || txt.getText() == null) {
label_fail.setText("Error: You can not leave any fields blank.");
return false;
}
try {
int x = Integer.parseInt(txt.getText());
return true;
} catch (NumberFormatException e) {
label_fail.setText("Error: " + txt.getText() + " is not an integer.");
return false;
}
}
private boolean stringValidation(TextField txt) {
if (txt.getText().isEmpty() || txt.getText() == null) {
label_fail.setText("Error: You can not leave any fields blank.");
return false;
} else if (txt.getText().matches(".*\\d.*")) {
String invalidCharacters = txt.getText().replaceAll("[^0-9]", "");
label_fail.setText("Error: You've entered illegal characters in a text field. "
+ "Please remove \"" + invalidCharacters + "\" "
+ "from the highlighted field or enter a different value.");
return false;
} else {
return true;
}
}
}
答案 0 :(得分:0)
我正在发生的事情:label_success始终可见。一开始它没有文本,所以您不会看到它是可见的,但是当您在方法开始时将文本添加到标签时,这种情况会改变。淡入淡出负责将其不透明度设置为0。但是,当您输入无效值时,淡入淡出永远不会开始,因此它将保持在其初始值,这意味着它将可见。您可以通过将label_success.setText("Successfully Added");
移动到最终if语句内恰好开始衰减的位置上方来解决此问题。
您也可以通过将输入的验证与标签的面膜设置分离为不同的方法,使生活更轻松,因为现在同一方法中正在进行很多操作。