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

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

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

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

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


当前回答

以编程方式关闭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”按钮被按下。

其他回答

 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

以编程方式关闭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”按钮被按下。

如果您不希望您的应用程序在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

不仅要关闭JFrame,还要触发WindowListener事件,试试这个:

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

将问题正文中的内容作为CW答案。

想分享的成果,主要来源于关注camickr的链接。基本上我需要抛出一个WindowEvent。应用程序事件队列上的WINDOW_CLOSING。下面是解决方案的概要

// closing down the window makes sense as a method, so here are
// the salient parts of what happens with the JFrame extending class ..

    public class FooWindow extends JFrame {
        public FooWindow() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(5, 5, 400, 300);  // yeah yeah, this is an example ;P
            setVisible(true);
        }
        public void pullThePlug() {
                WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
        }
    }

// Here's how that would be employed from elsewhere -

    // someplace the window gets created ..
    FooWindow fooey = new FooWindow();
    ...
    // and someplace else, you can close it thusly
    fooey.pullThePlug();