如何在没有System.exit(0)的情况下向退出按钮添加功能?

时间:2011-08-21 04:57:39

标签: java swing windowlistener

每当用户点击窗口的红色退出按钮时,我都会通过程序System.exit(0)。是否有更有效的方法为该按钮添加功能?

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.equals("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);
        }
    }
}

3 个答案:

答案 0 :(得分:4)

你在谈论哪个窗口的红色按钮?

这一个?

enter image description here

如果是,那么您可以为WindowListener

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

或者您可以简单地设置默认关闭操作:

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

答案 1 :(得分:1)

如果您只是希望程序在用户按下X按钮时退出,您可以执行以下操作:

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

这样,当窗口关闭时,程序退出。

Oracle为初学者here提供了关于Swing(Java窗口的东西)的优秀(并且非常简单)的教程。它们是整体Java Tutorials的一部分。

答案 2 :(得分:1)

即使你做了什么,你的代码也行不通。

当用户按下enter image description here按钮时,来自输入框的值将为空。

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

如果用户关闭对话框(无论是否输入值),您都会获得nullnull不是"null"。 (后者是一个字符串)。

因此,当用户关闭对话框时,name.equals("null")将抛出空指针异常。

为了处理这个关闭按钮很简单,在你知道的时候检查值是否为空。

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;
}

基本问题是你有一个无限循环。你需要突破循环,你可以使用break但是你有另一个无限循环。将此名称的空检查添加到另一个循环:

while (name != null)  //Enter this loop only when the user has entered a name
{
    gender = JOptionPane.showInputDialog("Are you a boy or a girl?: ");
顺便说一句,我认为这是在某种程度上学习你在这里做的事情。这在真正的应用程序中绝对不是很好。而且,你可以合并两个循环,你只需要一个,而不是两个。