开发者

How do I add functionality to the exit button without System.exit(0)?

I went through and had the program System.exit(0) whenever the user clicked the window's red exit button. Is there a more efficient way to add functionality to that button?

import javax.swing.JOptionPane;
import java.util.Scanner;

class codeWithProblem
{
    public static void main (String[] args)
    {
        String name, gender, passions, enjoy;
        int yesNo, permission, endProgram;
        String prefix = "";
        while (true)
        {
            name = JOptionPane.showInputDialog("Please enter your name: ");
            if (name.equals("null"))
            {
                System.exit(0);
            }
            yesNo = JOptionPane.showConfirmDialog(null, "So your name is           "+name+". Is this correct?", "", JOptionPane.YES_NO_OPTION);
            if (yesNo == -1)
            {
                System.exit(0);
            }
            if (yesNo == 0)
            {
                break;
            }
        }
        while (true)
        {
            gender = JOptionPane.showInputDialog("Are you a boy or a girl?: ");
            if (gender.equals("null"))
            {
                System.exit(0);
            }
            if ("boy".equalsIgnoreCase(gender)||"girl".equalsIgnoreCase(gender))
            {
                break;
            }
        }
        if ("boy".equalsIgnoreCase(gender))
        {
            prefix = "Mr. ";
        }
        if ("girl".equalsIgnoreCase(gender))
        {
            prefix = "Ms. ";
        }
        JOptionPane.showMessageDialog(null, "It's nice to meet you "+prefix+name+".", "Hello", JOptionPane.PLAIN_MESSAGE);
        permission = JOptionPane.showConfirmDialog(null, "Lets ask some easy questions to start out with. Is this okay with you?: ",
        "", JOptionPane.YES_NO_OPTION);
        if (permission == 1)
        {
            endProgram = JOptionPane.showConfirmDialog(null, "Do you want to end this program? Click CANCEL to end and OKAY to continue.",
            "End Program?", JOptionPane.OK_CANCEL_OPTION);
            if (endProgram == 0)
            {
                JOptionPane.showMessageDialog(null, "Good, lets continue...", "Continue", JOptionPane.PLAIN_MESSAGE);
                permission = 0;
            }
            if (endProgram == 1)
            {
                JOptionPane.showMessageDialog(null, "Goodbye....", "END", JOptionPane.PLAIN_MESSAGE);
            }
        }
        if (permission == 0)
        {
            passions = JOptionPane.showInputDialog("What are some of your passions?: ");
            if (passions.equals("null"))
            {
                System.exit(0);
            }
            enjoy = JOptionPane.showInputDialog("Why do you enjoy "+passions+"?: ");
            if (enjoy.equ开发者_StackOverflowals("null"))
            {
                System.exit(0);
            }
            JOptionPane.showMessageDialog(null, "That's interesting", "hmmm", JOptionPane.PLAIN_MESSAGE);
            JOptionPane.showMessageDialog(null, "This program has no point and is going to end now.", "BACON", JOptionPane.PLAIN_MESSAGE);
        }
    }
}


Which window's red button are you talking about?

This one?

How do I add functionality to the exit button without System.exit(0)?

If so, then you can implement a WindowListener for your JFrame:

JFrame f = ...;
f.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent we) {
      System.exit(0);
   }
});

Or you can simply set the default close operation:

JFrame f = ...;
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


If you're just wanting the program to exit when the user presses the X button, you can do something like this:

JFrame frame = new JFrame("Greeter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add your questions to the frame
frame.setVisible(true);

That way, when the window gets closed, the program exits.

Oracle has excellent (and really simple) tutorials on Swing (the Java windowy stuff) for beginners here. They are part of the overall Java Tutorials.


Your code will not work even with what you have done.

When the user presses the

How do I add functionality to the exit button without System.exit(0)?

button, the value of the from input box will be null.

name = JOptionPane.showInputDialog("Please enter your name: ");

If the user closes the dialog (no matter whether a value is entered or not) you get a null. null not "null". (the latter is a string).

So when the user closes the dialog name.equals("null") will throw the null pointer exception.

And for handling this close button is easy, check whether the value is null when you know that.

name = JOptionPane.showInputDialog("Please enter your name: ");
if (name != null)  //Proceed only when the user has entered a valid name
{
    yesNo = JOptionPane.showConfirmDialog(null, "So your name is "+name+". Is this correct?", "", JOptionPane.YES_NO_OPTION);
    if (yesNo == -1)
    {
        System.exit(0);
    }
    if (yesNo == 0)
    {
        break;
    }
}
else
{
    break;
}

The basic problem is that you have a infinite loop. You need to break out of the loop,you can use break but then you have another infinite loop. Add a null check on this name to the other loop also:

while (name != null)  //Enter this loop only when the user has entered a name
{
    gender = JOptionPane.showInputDialog("Are you a boy or a girl?: ");

BTW, I assume that this is some sort of learning what you are doing here. This is definitely not good in real apps. And, you can merge the two loops, you need only one, not two.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜