让JFrame关闭的正确方法是什么,就像用户按下X关闭按钮,或按下Alt+F4(在Windows上)一样?

我的默认关闭操作设置为我想要的方式,通过:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

它完全符合我对上述控件的要求。这个问题与此无关。

我真正想做的是使GUI的行为与按下X关闭按钮所引起的行为相同。

假设我要扩展WindowAdaptor,然后通过addWindowListener()将适配器的一个实例添加为侦听器。我希望看到通过windowDeactivated()、windowClosing()和windowClosed()的调用序列与X close按钮相同。可以说,与其说是撕破窗户,不如说是让它自己撕毁。


当前回答

如果你真的不希望你的应用程序在JFrame关闭时终止,那么,

使用:setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

下面是解决方案的概要,

 myFrame.dispatchEvent(new WindowEvent(myFrame, WindowEvent.WINDOW_CLOSING));

其他回答

您必须将调用插入到AWT消息队列中,以便所有计时都正确发生,否则它将无法分派正确的事件序列,特别是在多线程程序中。当这一步完成后,你可以处理结果事件序列,就像用户点击操作系统提供的装饰JFrame的[x]按钮一样。

public void closeWindow()
{
    if(awtWindow_ != null) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                awtWindow_.dispatchEvent(new WindowEvent(awtWindow_, WindowEvent.WINDOW_CLOSING));
            }
        });
    }
}
 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

我已经尝试过了,为formWindowClosing()事件编写自己的代码。

 private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
    int selectedOption = JOptionPane.showConfirmDialog(null,
            "Do you want to exit?",
            "FrameToClose",
            JOptionPane.YES_NO_OPTION);
    if (selectedOption == JOptionPane.YES_OPTION) {
        setVisible(false);
        dispose();
    } else {
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    }
}    

这将询问用户是否要退出框架或应用程序。

如果您不希望您的应用程序在JFrame关闭时终止, 使用: setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE)

而不是: setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

从文档中可以看到:

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

HIDE_ON_CLOSE(在WindowConstants中定义):在调用任何注册的WindowListener对象后自动隐藏帧。

DISPOSE_ON_CLOSE(在WindowConstants中定义):在调用任何注册的WindowListener对象后自动隐藏和释放帧。

EXIT_ON_CLOSE(在JFrame中定义):使用系统退出方法退出应用程序。仅在应用程序中使用此选项。

可能仍然有用: 如果你想再次显示相同的帧,你可以在JFrame上使用setVisible(false)。 否则调用dispose()删除所有本机屏幕资源。

抄自彼得·朗

https://stackoverflow.com/a/1944474/3782247

本示例演示了如何实现确认窗口关闭操作。

窗口有一个窗口适配器,它将默认关闭操作切换为exit_on_close或DO_NOTHING_ON_CLOSE,这取决于您在OptionDialog中的答案。

ConfirmedCloseWindow的closeWindow方法触发关闭窗口事件,可以在任何地方使用,即作为菜单项的动作

public class WindowConfirmedCloseAdapter extends WindowAdapter {

    public void windowClosing(WindowEvent e) {

        Object options[] = {"Yes", "No"};

        int close = JOptionPane.showOptionDialog(e.getComponent(),
                "Really want to close this application?\n", "Attention",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.INFORMATION_MESSAGE,
                null,
                options,
                null);

        if(close == JOptionPane.YES_OPTION) {
           ((JFrame)e.getSource()).setDefaultCloseOperation(
                   JFrame.EXIT_ON_CLOSE);
        } else {
           ((JFrame)e.getSource()).setDefaultCloseOperation(
                   JFrame.DO_NOTHING_ON_CLOSE);
        }
    }
}

public class ConfirmedCloseWindow extends JFrame {

    public ConfirmedCloseWindow() {

        addWindowListener(new WindowConfirmedCloseAdapter());
    }

    private void closeWindow() {
        processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
    }
}