是没有确认和窗口事件

时间:2011-10-16 11:32:46

标签: swing

我有以下代码段:

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]按钮,无论我选择何种选项,帧都会消失。在这种情况下,如果我选择NO选项,我想知道如何保留框架窗口。谢谢!

1 个答案:

答案 0 :(得分:1)

使用此setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)作为JFrame子类的默认关闭操作(如果您没有明确地将setDefaultCloseOperation(...)添加到另一个值,我不记得这是否为默认值)

  

DO_NOTHING_ON_CLOSE(在WindowConstants中定义):不要做任何事情;   要求程序在windowClosing中处理操作   已注册的WindowListener对象的方法。

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