JoptionPane ShowConfirmDialog [英] JoptionPane ShowConfirmDialog

查看:248
本文介绍了JoptionPane ShowConfirmDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Java program. When I run the program, it will give me a GUI which as I attached.

当我要关闭它时,它会提示确认对话框。如果我按下是按钮,它将使用 System.exit()退出程序。

When I want to close it, it will prompt out a confirm dialog. If I press the Yes button, it will quit the program using 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()

If I don't want to quit the program, what can I do? System.continued() ?

推荐答案

尝试设置此项,

app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)

[已编辑]

因此,您的代码将变成这样的代码,

So, your code will become something like this,

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

[说明]

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

You might be thinking that why it is like that. The behaviour of windows close button for JFrame, unlike Frame,is to hide the window. Therefore, it will hide/close the window anyway. But when you specify that it must also exit the program, when the user click yes. Then, besides closing the window, it also exits the program. And when user clicks no, it does nothing but closes the window anyway. Hence, you must tell it explicitly that DO_NOTHING_ON_CLOSE.

[文档]


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

Unlike a Frame, a JFrame has some notion of how to respond when the user attempts to close the window. The default behavior is to simply hide the JFrame when the user closes the window. To change the default behavior, you invoke the method setDefaultCloseOperation(int). To make the JFrame behave the same as a Frame instance, use setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE).

参考: JFrame文档

这篇关于JoptionPane ShowConfirmDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆