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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7136470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:46:37  来源:igfitidea点击:

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

javaswingwindowlistener

提问by JDN

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?

每当用户单击窗口的红色退出按钮时,我都会执行 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);
        }
    }
}

回答by Behrang Saeedzadeh

Which window's red button are you talking about?

你说的是哪个窗口的红色按钮?

This one?

这个?

enter image description here

在此处输入图片说明

If so, then you can implement a WindowListenerfor your JFrame:

如果是这样,那么您可以WindowListener为您的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);

回答by Daniel

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

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

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.

Oracle在此处为初学者提供关于 Swing(Java 窗口的东西)的优秀(而且非常简单)教程。它们是整个Java 教程的一部分

回答by Nivas

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

即使你做了什么,你的代码也不会工作。

When the user presses the enter image description herebutton, the value of the from input box will be null.

当用户按下在此处输入图片说明按钮时,from 输入框的值为空。

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. nullnot "null". (the latter is a string).

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

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

所以当用户关闭对话框时name.equals("null")会抛出空指针异常。

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 problemis that you have a infinite loop. You need to break out of the loop,you can use breakbut then you have another infinite loop. Add a null check on this name to the other loop also:

基本问题是你有一个无限循环。你需要跳出循环,你可以使用,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?: ");

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

顺便说一句,我认为这是某种学习你在这里做什么。这在实际应用中绝对不好。而且,您可以合并两个循环,您只需要一个,而不是两个。