有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在出现错误时使我的成功消息不显示

当代码运行时,我输入的凭据不正确,成功添加的消息将与错误消息一起出现,我不确定为什么会发生这种情况。我们需要为我们的工作显示一条错误消息和一条成功消息。 当代码运行时,我输入的凭据不正确,成功添加的消息将与错误消息一起出现,我不确定为什么会发生这种情况。我们需要为我们的工作显示一条错误消息和一条成功消息

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;
    }
}

}

共 (1) 个答案

  1. # 1 楼答案

    我所期待的是:成功的标签总是可见的。在开始时,它没有文本,所以你看不到它是可见的,但当你在方法的开头将文本添加到标签时,情况会发生变化。淡入度负责将其不透明度设置为0。但是当你输入一个无效值时,淡入淡出永远不会开始,所以它保持在它的起始值,这意味着它将是可见的。您可以通过将label_success.setText("Successfully Added");移动到最终if语句中开始淡入淡出的位置上方来解决这个问题

    此外,你还可以通过将输入的验证与标签的visal设置分离成不同的方法,让你的生活变得更加轻松,因为现在在同一个方法中有很多事情正在进行