JOptionPane.showConfirmDialog中的错误?

pg-*_*ban 1 java swing joptionpane

我想在用户关闭框架窗口时提示用户进行确认,如下所示:

addWindowListener(new WindowAdapter()
{
    public void windowClosing(WindowEvent e)
    {
        if (JOptionPane.showConfirmDialog(null, "Are you sure you want to abort the game?",
            "Really quit?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
        dispose();
    }
});
Run Code Online (Sandbox Code Playgroud)

对话框显示,但无论是否单击是,否,或关闭窗口,窗口都会关闭.这是某种错误还是我真的在这里错过了一些简单的东西?我在OS X上使用Eclipse和Java 1.6.0.13.

Yis*_*hai 5

您必须将框架设置为在关闭时不执行任何操作以控制关闭:

 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Run Code Online (Sandbox Code Playgroud)