3

我有一个 Java 程序。当我运行程序时,它会给我一个我附加的 GUI。

当我想关闭它时,它会提示一个确认对话框。如果我按下是按钮,它将使用System.exit().

public static void main(String args[])
{
    ButtonTest app = new ButtonTest( );
    app.addWindowListener( 
        new WindowAdapter( )
        {
            public void windowClosing (WindowEvent e)
            {
                String message = " Really Quit ? ";
                String title = "Quit";
                int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
                if (reply == JOptionPane.YES_OPTION)
                {
                    System.exit(0);
                }

            }
        }
    );
}

如果我不想退出程序,我该怎么办?System.continued()?

4

5 回答 5

3

在这种情况下你不需要 else

于 2012-05-23T09:14:43.767 回答
3

尝试设置这个,

app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)

[已编辑]

所以,你的代码会变成这样,

public static void main(String args[]) {
    ButtonTest app = new ButtonTest();
    app.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            int reply = JOptionPane.showConfirmDialog(null,
                    "Really Quit ?", "Quit", JOptionPane.YES_NO_OPTION);
            if (reply == JOptionPane.YES_OPTION)
                System.exit(0);
        }
    });
    app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    app.setSize(640, 480);
    app.setVisible(true);
}

[解释]

你可能会想为什么会这样。JFrame与 不同的是,窗口关闭按钮的行为Frame是隐藏窗口。因此,无论如何它都会隐藏/关闭窗口。但是当你指定它也必须退出程序时,当用户点击yes. 然后,除了关闭窗口之外,它还退出程序。当用户点击时no,它什么都不做,只是关闭窗口。因此,您必须明确告诉它DO_NOTHING_ON_CLOSE.

[文档]

与 Frame 不同,JFrame 有一些关于当用户试图关闭窗口时如何响应的概念。默认行为是在用户关闭窗口时简单地隐藏 JFrame。要更改默认行为,请调用方法 setDefaultCloseOperation(int)。要使 JFrame 的行为与 Frame 实例相同,请使用 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)。

参考:JFrame 文档

于 2012-05-23T09:15:18.683 回答
1

如果你问我,我会选择 onYES SELECTION而不是突然关闭我的应用程序System.exit(0),我会选择关闭我的应用程序的优雅方式,使用frameObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)and on NO SELECTION,我会选择frameObject.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)。这是一个示例程序,可以帮助您:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ApplicationCloseExample
{   
    private void displayGUI()
    {
        final JFrame frame = new JFrame("Application Close Example");

        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent we)
            {
                int result = JOptionPane.showConfirmDialog(
                                frame, "Do you want to Exit ?"
                                , "Exit Confirmation : ", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION)               
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                else if (result == JOptionPane.NO_OPTION)   
                    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            }
        });

        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ApplicationCloseExample().displayGUI();
            }
        });
    }
}
于 2012-05-23T13:20:26.940 回答
0

If you want the program to continue when you press NO, place the rest of your code in else block or call the function where you have placed the rest of your code.

Removing else block is also an option if you don't want to place any action on the NO button because the JOptionPane.showConfirmDialog() will close anyways. You can continue with rest of your code after the if statement.

FYI- There is no System.continue(). The program pretty much does that on it's own.

于 2013-08-06T16:55:16.313 回答
0

您可以添加一个else块。如果你想再次运行 main 方法(我假设你这样做),它应该看起来像这样。如果用户选择否,您应该有一些您运行的方法,无论是主要方法main(null)还是其他方法。

public static void main(String args[])
{
ButtonTest app = new ButtonTest( );
app.addWindowListener( 
        new WindowAdapter( )
        {
            public void windowClosing (WindowEvent e)
            {
                String message = " Really Quit ? ";
                String title = "Quit";
                int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
                if (reply == JOptionPane.YES_OPTION)
                {
                    System.exit(0);
                } 
                else 
                {
                  //whatever you plan on running instead here, instead of quitting,
                  //main(null) to run the main method, or put another method if you want
                }
            }
        }
    );
}
于 2017-03-20T20:08:27.297 回答