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

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

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

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

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


当前回答

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

窗口有一个窗口适配器,它将默认关闭操作切换为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));
    }
}

其他回答

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

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。

将问题正文中的内容作为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();

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

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

// frame stuffs here ...

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

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

setVisible(false); //you can't see me!
dispose(); //Destroy the JFrame object

不太棘手。

以下是你的选择:

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