是 否 确认和窗口事件

发布于 2024-12-10 14:16:26 字数 858 浏览 0 评论 0原文

我有以下代码片段:

addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            int result = JOptionPane.showConfirmDialog(Bookstore.this, "Are you sure to quit?", "Confirm", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                // release connection resource
                if (bookstoreConnection != null) {
                    bookstoreConnection.closeConnection();
                }
                // JFrame handles close request based on the property
                // set by invoking the setDefaultCloseOperation(...)
                Bookstore.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }                
        }
    });

每当我按下右上角的 [x] 按钮时,无论我选择什么选项,框架都会消失。在这种情况下,我想知道如果我选择“否”选项如何保留框架窗口。谢谢你!

I have following code snippet:

addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            int result = JOptionPane.showConfirmDialog(Bookstore.this, "Are you sure to quit?", "Confirm", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                // release connection resource
                if (bookstoreConnection != null) {
                    bookstoreConnection.closeConnection();
                }
                // JFrame handles close request based on the property
                // set by invoking the setDefaultCloseOperation(...)
                Bookstore.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }                
        }
    });

and whenever i press [x] button at upper right corner the frame disappears regardless of whatever option i chose. In this case I want to know how to retain frame window if i choose NO option. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总攻大人 2024-12-17 14:16:26

使用这个 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) 作为 JFrame 子类的默认关闭操作(如果您没有显式添加 setDefaultCloseOperation(.. .) 与另一个值)

DO_NOTHING_ON_CLOSE(在WindowConstants中定义):不执行任何操作;
要求程序处理窗口中的操作关闭
已注册的 WindowListener 对象的方法。

http://download.oracle.com /javase/1.3/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)

Use this setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) as the default close operation on your JFrame subclass (I don't remember if this is the default if you don't explicitly add the setDefaultCloseOperation(...) with another value)

DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything;
require the program to handle the operation in the windowClosing
method of a registered WindowListener object.

http://download.oracle.com/javase/1.3/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation(int)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文