问题
I'm working on a code in eclipse with swing and I have a long-ish if, else function that I'm having trouble with. So, I kind of forgot about nested if, else functions and made all the if's separate, which of course only brings back the last if as true. I want the else function to take into consider multiple constraints, but how do I do this when each "if" has its own dialog message that pops up?
I tried to just make it one long if function with &&'s and a single dialog message, which would work fine for what I need, but I don't know how to do the confirm password and make sure that's not null with that still having its own dialog message and still being in the same if, else function.
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);
}
}});
回答1:
Any reason why simply using else if
won't work?
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);
}
else if (p2.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "Confirm password Field field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else if (p1 != null && p2 != null)
{
if (!Arrays.equals(p1.getPassword(), p2.getPassword()))
{
JOptionPane.showMessageDialog(null, "Passwords do not match.", "Woops", JOptionPane.ERROR_MESSAGE);
}
else if (txtname.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "Name field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else if (txtDOB.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "DOB field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else if (txtNum.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "Phone Number field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else if (txtState.getText().isEmpty())
{
JOptionPane.showMessageDialog(null, "State field must be filled in", "Login Warning", JOptionPane.WARNING_MESSAGE);
}
else 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);
}
}
}
});
来源:https://stackoverflow.com/questions/58652461/if-else-with-multiple-constraints-and-dialog-message-function