Java Validations GUI Netbeans



你好,我有一个java的GUI Netbeans项目,当我点击提交时,它会检查文本字段的验证

这是我的代码

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");
    }
    String roomType = cmbRoomType.getSelectedItem().toString();
    String roomNumber = cmbRoomNumber.getSelectedItem().toString();
    String checkin = ftxtCheckinDate.getText();
    String checkout = txtCheckOut.getText();
    String Name = txtName.getText();
    String IdType = cmbPhotoId.getSelectedItem().toString().trim();
    String IdNumber = txtIdNumber.getText();
    String Phone = txtPhoneNum.getText();
    String email = txtEmail.getText().trim();

    JOptionPane.showMessageDialog(null,roomType,roomNumber,checkin,checkout,Name,IdType,IdNumber,Phone,email);
NOT WORKING (optionpane with variables)^^^^^^^^

}    

我习惯于确保,如果所有这些都是正确的,那么程序有一个弹出窗口,显示所有输入表格的变量。

这是GUI的图片https://i.stack.imgur.com/ygp1G.jpg

JOptionPane不接受这么多参数。您应该连接字符串,而不是单独传递它们。

尝试:

JOptionPane.showMessageDialog(null, "Title", roomType+roomNumber+checkin+checkout+Name+IdType+IdNumber+Phone+email, JOptionPane.OK_OPTION);

您可以将数组传递到选项窗格。阵列中的每个对象将显示在不同的行上:

String[] messages = new String[3];
messages[0] = "Message line1";
messages[1] = "Message line1";
messages[2] = "Message line1";
JOptionPane.showMessageDialog(
    null, // parent frame
    messages,
    "Multiline Message",
    JOptionPane.INFORMATION_MESSAGE);

阅读Swing教程中关于如何创建对话框的部分,了解JOptionPane的更多示例和功能。

相关内容

  • 没有找到相关文章

最新更新