首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JoptionPane ShowConfirmDialog

JoptionPane ShowConfirmDialog
EN

Stack Overflow用户
提问于 2012-05-23 17:12:04
回答 5查看 46.7K关注 0票数 3

我有一个Java程序。当我运行程序时,它会给我一个图形用户界面,因为我附加了。

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

代码语言:javascript
复制
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()

EN

回答 5

Stack Overflow用户

发布于 2012-05-23 17:14:44

在这种情况下,您不需要使用else

票数 3
EN

Stack Overflow用户

发布于 2012-05-23 17:15:19

试着设置这个,

代码语言:javascript
复制
app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)

编辑的

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

代码语言:javascript
复制
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的窗口关闭按钮的行为是隐藏窗口。因此,它无论如何都会隐藏/关闭窗口。但是当您指定它还必须退出程序时,当用户单击yes时。然后,除了关闭窗口之外,它还会退出程序。当用户单击no时,它什么也不做,只是关闭窗口。因此,您必须显式地告诉它DO_NOTHING_ON_CLOSE

文档

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

参考:JFrame docs

票数 3
EN

Stack Overflow用户

发布于 2012-05-23 21:20:27

如果你会问我,我会说,在YES SELECTION上,我不会突然用System.exit(0)关闭我的应用程序,我会选择亲切的方式关闭我的应用程序,通过使用frameObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE),在NO SELECTION上,我会选择frameObject.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)。下面是一个帮助您的示例程序:

代码语言:javascript
复制
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();
            }
        });
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10716828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档