我的程序/ JFrame
中有一个提交按钮,它检查验证并显示错误消息,然后弹出另一个表单并输入所有详细信息:
private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {
//VALIDATIONS-----------------------------------------------------------
if(txtName.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "Must have name");
jlblNameVer.setVisible(true);
}
else
{
jlblNameVer.setVisible(false);
}
//ID VERIFICATION
if (txtIdNumber.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "Photo Id must not be emplty");
}
//EMAIL VALIDATION
if(txtEmail==null ||txtEmail.getText().length() < 10|| txtEmail.getText()== null ||!(txtEmail.getText().trim().contains("@") && txtEmail.getText().trim().contains(".")))
{
JOptionPane.showMessageDialog(null, "Invalid Email");
}
//Phone Number Validation
if(txtPhoneNum.getText().length() < 10)
{
JOptionPane.showMessageDialog(null, "Must atleast 10 characters");
}
//COMBOBOX VALIDATIONS
if(cmbStayDuration.getSelectedIndex() == -1)
{
JOptionPane.showMessageDialog(null, "Please select stay duration");
}
//Photo ID
if(cmbPhotoId.getSelectedIndex() == -1)
{
JOptionPane.showMessageDialog(null, "Please select Photo ID type");
}
//Popup form
jfrmDetails nf1 = new jfrmDetails();
jfrmDetails.txtRoomTypef2.setText(this.cmbRoomType.getSelectedItem().toString());
jfrmDetails.txtRoomNumf2.setText(this.cmbRoomNumber.getSelectedItem().toString());
jfrmDetails.txtCheckIn.setText(this.ftxtCheckinDate.getText());
jfrmDetails.txtCheckOut.setText(this.txtCheckOut.getText());
jfrmDetails.txtName.setText(this.txtName.getText());
jfrmDetails.txtIdType.setText(this.cmbPhotoId.getSelectedItem().toString());
jfrmDetails.txtIdNum.setText(this.txtIdNumber.getText());
jfrmDetails.txtPhoneNum.setText(this.txtPhoneNum.getText());
jfrmDetails.txtEmail.setText(this.txtEmail.getText());
nf1.setVisible(true);
}
问题是即使这些验证错误,表格仍然会弹出
如果任何验证不正确,我不希望弹出窗体显示,我该怎么办?
答案 0 :(得分:3)
您需要将if
块重组为if-else if-else
块,以便最后一个(else
)条件显示新视图或使用boolean
字段确定是否验证passed
,例如......
boolean passed = true;
if (txtName.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Must have name");
jlblNameVer.setVisible(true);
passed = false;
} else {
jlblNameVer.setVisible(false);
}
//ID VERIFICATION
if (txtIdNumber.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Photo Id must not be emplty");
passed = false;
}
//EMAIL VALIDATION
if (txtEmail == null || txtEmail.getText().length() < 10 || txtEmail.getText() == null || !(txtEmail.getText().trim().contains("@") && txtEmail.getText().trim().contains("."))) {
JOptionPane.showMessageDialog(null, "Invalid Email");
passed = false;
}
//Phone Number Validation
if (txtPhoneNum.getText().length() < 10) {
JOptionPane.showMessageDialog(null, "Must atleast 10 characters");
passed = false;
}
//COMBOBOX VALIDATIONS
if (cmbStayDuration.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select stay duration");
passed = false;
}
//Photo ID
if (cmbPhotoId.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select Photo ID type");
passed = false;
}
if (passed) {
//Popup form
jfrmDetails nf1 = new jfrmDetails();
jfrmDetails.txtRoomTypef2.setText(this.cmbRoomType.getSelectedItem().toString());
jfrmDetails.txtRoomNumf2.setText(this.cmbRoomNumber.getSelectedItem().toString());
jfrmDetails.txtCheckIn.setText(this.ftxtCheckinDate.getText());
jfrmDetails.txtCheckOut.setText(this.txtCheckOut.getText());
jfrmDetails.txtName.setText(this.txtName.getText());
jfrmDetails.txtIdType.setText(this.cmbPhotoId.getSelectedItem().toString());
jfrmDetails.txtIdNum.setText(this.txtIdNumber.getText());
jfrmDetails.txtPhoneNum.setText(this.txtPhoneNum.getText());
jfrmDetails.txtEmail.setText(this.txtEmail.getText());
nf1.setVisible(true);
}
或
if (txtName.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Must have name");
jlblNameVer.setVisible(true);
} else {
jlblNameVer.setVisible(false);
//ID VERIFICATION
if (txtIdNumber.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Photo Id must not be emplty");
passed = false;
} else if (txtEmail == null || txtEmail.getText().length() < 10 || txtEmail.getText() == null || !(txtEmail.getText().trim().contains("@") && txtEmail.getText().trim().contains("."))) {
JOptionPane.showMessageDialog(null, "Invalid Email");
passed = false;
} else if (txtPhoneNum.getText().length() < 10) {
JOptionPane.showMessageDialog(null, "Must atleast 10 characters");
passed = false;
} else if (cmbStayDuration.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select stay duration");
passed = false;
} else if (cmbPhotoId.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select Photo ID type");
passed = false;
} else {
//Popup form
jfrmDetails nf1 = new jfrmDetails();
jfrmDetails.txtRoomTypef2.setText(this.cmbRoomType.getSelectedItem().toString());
jfrmDetails.txtRoomNumf2.setText(this.cmbRoomNumber.getSelectedItem().toString());
jfrmDetails.txtCheckIn.setText(this.ftxtCheckinDate.getText());
jfrmDetails.txtCheckOut.setText(this.txtCheckOut.getText());
jfrmDetails.txtName.setText(this.txtName.getText());
jfrmDetails.txtIdType.setText(this.cmbPhotoId.getSelectedItem().toString());
jfrmDetails.txtIdNum.setText(this.txtIdNumber.getText());
jfrmDetails.txtPhoneNum.setText(this.txtPhoneNum.getText());
jfrmDetails.txtEmail.setText(this.txtEmail.getText());
nf1.setVisible(true);
}
}
您还应该查看The Use of Multiple JFrames, Good/Bad Practice?,也可以考虑使用CardLayout
,但这取决于您的基本需求
您还可以查看Validating Input,这样您就可以在丢失焦点时对字段进行后期验证