Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java If、else具有多个约束和对话框消息功能_Java_Swing_If Statement - Fatal编程技术网

Java If、else具有多个约束和对话框消息功能

Java If、else具有多个约束和对话框消息功能,java,swing,if-statement,Java,Swing,If Statement,我正在用swing在eclipse中编写代码,我有一个很长的if,else函数,我遇到了麻烦。所以,我有点忘记了嵌套的if,else函数,并将所有的if分开,这当然只会将最后一个if恢复为true。我想让其他函数考虑多个约束,但是当每个“IF”都有自己的对话框消息弹出时,我怎么做?< /P> 我试着让它成为一个长的if函数,带有&,还有一个对话框消息,这对于我所需要的很好,但是我不知道如何做确认密码,确保它不是空的,因为它仍然有自己的对话框消息,并且仍然在相同的if,else函数中 btnSub

我正在用swing在eclipse中编写代码,我有一个很长的if,else函数,我遇到了麻烦。所以,我有点忘记了嵌套的if,else函数,并将所有的if分开,这当然只会将最后一个if恢复为true。我想让其他函数考虑多个约束,但是当每个“IF”都有自己的对话框消息弹出时,我怎么做?< /P> 我试着让它成为一个长的if函数,带有&,还有一个对话框消息,这对于我所需要的很好,但是我不知道如何做确认密码,确保它不是空的,因为它仍然有自己的对话框消息,并且仍然在相同的if,else函数中

btnSubmit = new JButton("Submit");  
            btnSubmit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    JFrame frmregistration_test = new JFrame("Submit");
                    if (p1.getText().isEmpty())
                    {
                        JOptionPane.showMessageDialog(null, "Password Field field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
                    }

                    if (p2.getText().isEmpty())
                    {
                        JOptionPane.showMessageDialog(null, "Confirm password Field field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
                    }


                    if (p1 != null && p2 != null) {
                    if (!Arrays.equals(p1.getPassword(), p2.getPassword())) 
                    {   
                           JOptionPane.showMessageDialog(null, "Passwords do not match.", "Woops", JOptionPane.ERROR_MESSAGE);


                    }
                    if (txtname.getText().isEmpty())
                    {
                        JOptionPane.showMessageDialog(null, "Name field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
                    }
                    if (txtDOB.getText().isEmpty())
                    {
                        JOptionPane.showMessageDialog(null, "DOB field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
                    }
                    if (txtNum.getText().isEmpty())
                    {
                        JOptionPane.showMessageDialog(null, "Phone Number field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
                    }
                    if (txtState.getText().isEmpty())
                    {
                        JOptionPane.showMessageDialog(null, "State field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
                    }
                    if (txtid.getText().isEmpty())
                    {
                        JOptionPane.showMessageDialog(null, "Email Field field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);

                    }


                    else 

                        JOptionPane.showMessageDialog(null, "Registered Successfully", "Login Warning", JOptionPane.WARNING_MESSAGE);
                    }



                }});

如果不起作用,为什么只使用
else

btnSubmit=newjbutton(“提交”);
btnSubmit.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件arg0)
{
JFrame FRM注册测试=新JFrame(“提交”);
if(p1.getText().isEmpty())
{
JOptionPane.showMessageDialog(空,“必须填写密码字段”,“登录警告”,JOptionPane.Warning\u消息);
}
else if(p2.getText().isEmpty())
{
JOptionPane.showMessageDialog(空,“必须填写确认密码字段”、“登录警告”、“JOptionPane.Warning\u消息”);
}    
else if(p1!=null&&p2!=null)
{
如果(!Arrays.equals(p1.getPassword(),p2.getPassword()))
{   
showMessageDialog(null,“密码不匹配”,“Woops”,JOptionPane.ERROR_消息);
}
else if(txtname.getText().isEmpty())
{
JOptionPane.showMessageDialog(空,“必须填写名称字段”,“登录警告”,JOptionPane.Warning\u消息);
}
else if(txtDOB.getText().isEmpty())
{
JOptionPane.showMessageDialog(空,“必须填写DOB字段”,“登录警告”,JOptionPane.Warning\u消息);
}
else if(txtNum.getText().isEmpty())
{
JOptionPane.showMessageDialog(空,“必须填写电话号码字段”,“登录警告”,JOptionPane.Warning\u消息);
}
else if(txtState.getText().isEmpty())
{
JOptionPane.showMessageDialog(空,“必须填写状态字段”,“登录警告”,JOptionPane.Warning\u消息);
}
else if(txtid.getText().isEmpty())
{
JOptionPane.showMessageDialog(空,“必须填写电子邮件字段”,“登录警告”,JOptionPane.Warning\u消息);
}
其他的
{
showMessageDialog(null,“注册成功”,“登录警告”,JOptionPane.Warning\u消息);
}
}    
}
});
1)要更快获得更好的帮助,请添加或。2) IDE与该问题无关。除非直接相关,否则不要添加IDE标记。if,else if,else没有错。不过,您可以考虑只使用一个消息对话框。您的if和else可能应该将所需的消息文本放入字符串变量中。如果消息变量包含某些内容,则在对话框中显示该消息并退出该方法,例如:
String msg=“”;If(blah==blah){msg=“Message 1”}else If(blah==blah2){msg=“Message 2”}If(!msg.equals(“”){JOptionPane.showMessageDialog(null,msg,“Login Warning”,JOptionPane.Warning_Message);return;}