我想让我的代码检查我的表单中的文本字段是否为空&然后显示弹出警报消息。我的问题是,即使文本字段填写在这个警报消息中,一旦我点击“ENTER”就会弹出。任何帮助表示赞赏!
BtnEnter.setOnAction((ActionEvent e) -> {
if (txtfName.getText().isEmpty() | txtlName.getText().isEmpty() |
txtMI.getText().isEmpty() | txtStreeAdd.getText().isEmpty()
| txtCity.getText().isEmpty() | txtZip.getText().isEmpty()
| txtPhone.getText().isEmpty() | txtEmail.getText().isEmpty()
| txtSecEmail.getText().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Warning");
alert.setHeaderText("Required Fields Empty");
alert.setContentText("The fields highlighted in red must be filled "
+ "out.\nPlease try again.");
alert.showAndWait();
答案 0 :(得分:2)
我认为部分问题可能是使用单个管道|
代替标准||
。
然而,您可以采取另一种方法,可能更容易阅读。这就是我在应用程序中处理验证的方法:
public boolean validate() {
StringBuilder errors = new StringBuilder();
// Confirm mandatory fields are filled out
if (txtfName.getText().trim().isEmpty()) {
errors.append("- Please enter a first name.\n");
}
if (txtlName.getText().trim().isEmpty()) {
errors.append("- Please enter a last name.\n");
}
if (txtMI.getText().trim().isEmpty()) {
errors.append("- Please enter a middle initial.\n");
}
if (txtStreetAdd.getText().trim().isEmpty()) {
errors.append("- Please enter a street address.\n");
}
if (txtCity.getText().trim().isEmpty()) {
errors.append("- Please enter a city.\n");
}
if (txtZip.getText().trim().isEmpty()) {
errors.append("- Please enter a ZIP code.\n");
}
if (txtPhone.getText().trim().isEmpty()) {
errors.append("- Please enter a phone number.\n");
}
if (txtEmail.getText().trim().isEmpty()) {
errors.append("- Please enter a primary email address.\n");
}
if (txtSecEmail.getText().trim().isEmpty()) {
errors.append("- Please enter a secondary email address.\n");
}
// If any missing information is found, show the error messages and return false
if (errors.length() > 0) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Warning");
alert.setHeaderText("Required Fields Empty");
alert.setContentText(errors.toString());
alert.showAndWait();
return false;
}
// No errors
return true;
}
这样做的另一个好处是让用户确切地知道缺少哪些信息。这样,您只需在用户单击操作按钮时调用validate()
方法。如果validate()
恢复正常,您就知道您拥有所需的所有信息。
答案 1 :(得分:-1)
您也可以通过另一种方式来处理它,而无需Alert Alert = new Alert(Alert.AlertType.WARNING);这是一段代码:
public boolean validateForm() {
StringBuilder errors = new StringBuilder();
// Confirm mandatory fields are filled out
if (jTextField1.getText().trim().isEmpty()) {
errors.append("- Please enter a first name.\n");
jTextField1.requestFocusInWindow();
JOptionPane.showMessageDialog(null, errors, "Warning!", JOptionPane.ERROR_MESSAGE);
return false;
}
if (jTextField2.getText().trim().isEmpty()) {
errors.append("- Please enter a last name.\n");
jTextField2.requestFocusInWindow();
JOptionPane.showMessageDialog(null, errors, "Warning!", JOptionPane.ERROR_MESSAGE);
return false;
}
String s = ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText();
if (s.equals("")) {
errors.append("- Please choose a date.\n");
jDateChooser1.requestFocusInWindow();
JOptionPane.showMessageDialog(null, errors, "Warning!", JOptionPane.ERROR_MESSAGE);
return false;
}
if (jTextField3.getText().trim().isEmpty()) {
errors.append("- Please enter the age.\n");
jTextField3.requestFocusInWindow();
JOptionPane.showMessageDialog(null, errors, "Warning!", JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
通过这种方式,您可以检查用户是否知道确切缺少的内容,并向他们显示警告消息。为按钮添加一个侦听器。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(validateForm()) {
System.out.println("First name: "+jTextField1.getText());
System.out.println("Last name: "+jTextField2.getText());
System.out.println("Date: "+jDateChooser1.getDate());
System.out.println("Age: "+jTextField3.getText());
}
}