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

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

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

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

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


当前回答

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

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

其他回答

以编程方式关闭Swing帧的最佳方法是使它的行为与按下“X”按钮时一样。要做到这一点,你需要实现适合你的需要的WindowAdapter,并设置帧的默认关闭操作为什么都不做(DO_NOTHING_ON_CLOSE)。

像这样初始化你的框架:

private WindowAdapter windowAdapter = null;

private void initFrame() {

    this.windowAdapter = new WindowAdapter() {
        // WINDOW_CLOSING event handler
        @Override
        public void windowClosing(WindowEvent e) {
            super.windowClosing(e);
            // You can still stop closing if you want to
            int res = JOptionPane.showConfirmDialog(ClosableFrame.this, "Are you sure you want to close?", "Close?", JOptionPane.YES_NO_OPTION);
            if ( res == 0 ) {
                // dispose method issues the WINDOW_CLOSED event
                ClosableFrame.this.dispose();
            }
        }

        // WINDOW_CLOSED event handler
        @Override
        public void windowClosed(WindowEvent e) {
            super.windowClosed(e);
            // Close application if you want to with System.exit(0)
            // but don't forget to dispose of all resources 
            // like child frames, threads, ...
            // System.exit(0);
        }
    };

    // when you press "X" the WINDOW_CLOSING event is called but that is it
    // nothing else happens
    this.setDefaultCloseOperation(ClosableFrame.DO_NOTHING_ON_CLOSE);
    // don't forget this
    this.addWindowListener(this.windowAdapter);
}

你可以通过发送WINDOW_CLOSING事件来关闭帧,就像这样:

WindowEvent closingEvent = new WindowEvent(targetFrame, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closingEvent);

这将关闭框架,就像“X”按钮被按下。

如果你这样做是为了确保用户不能关闭窗口:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

然后您应该将pullThePlug()方法更改为

public void pullThePlug() {
    // this will make sure WindowListener.windowClosing() et al. will be called.
    WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);

    // this will hide and dispose the frame, so that the application quits by
    // itself if there is nothing else around. 
    setVisible(false);
    dispose();
    // if you have other similar frames around, you should dispose them, too.

    // finally, call this to really exit. 
    // i/o libraries such as WiiRemoteJ need this. 
    // also, this is what swing does for JFrame.EXIT_ON_CLOSE
    System.exit(0); 
}

我发现这是唯一的方法,发挥良好的WindowListener和JFrame.DO_NOTHING_ON_CLOSE。

如果你所说的Alt-F4或X是指“立即退出应用程序,而不考虑其他窗口或线程正在运行”,那么System.exit(…)将以一种非常突然的、暴力的、可能有问题的方式执行你想要的操作。

如果Alt-F4或X你的意思是隐藏窗口,那么frame.setVisible(false)是你“关闭”窗口的方式。该窗口将继续消耗资源/内存,但可以很快再次可见。

如果Alt-F4或X的意思是隐藏窗口并处理它所消耗的任何资源,那么frame.dispose()就是你“关闭”窗口的方式。如果该帧是最后一个可见窗口,并且没有其他非守护进程线程正在运行,则程序将退出。如果你再次显示窗口,它将不得不重新初始化所有的本机资源(图形缓冲区,窗口句柄等)。

Dispose()可能最接近于您真正想要的行为。如果你的应用程序有多个窗口打开,你想Alt-F4或X退出应用程序或只是关闭活动窗口?

关于窗口侦听器的Java Swing教程可能会帮助您澄清一些事情。

根据这里已经提供的答案,以下是我实现它的方式:

JFrame frame= new JFrame()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// frame stuffs here ...

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

JFrame获取要关闭的事件,并在关闭时退出。

以下是你的选择:

System.exit(0); // stop program
frame.dispose(); // close window
frame.setVisible(false); // hide window