Java:JDialog关闭问题

Java:JDialog关闭问题,java,swing,jdialog,Java,Swing,Jdialog,在这个程序中,当我关闭JDialog对话框时,我面临两个问题:对话框没有像关闭时那样正确关闭EXIT\u\u。以及如何给这个对话框命名。 代码 主要方法 公共班机{ public static void main(String[] args) { Dialog frame = new Dialog(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowE

在这个程序中,当我关闭
JDialog
对话框时,我面临两个问题:对话框没有像关闭时那样正确关闭
EXIT\u\u
。以及如何给这个对话框命名。
代码

主要方法

公共班机{

public static void main(String[] args) {

    Dialog frame = new Dialog();

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {

            int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to exit the application? ",
                    "EXIT Application", JOptionPane.YES_NO_OPTION);

            if (result == JOptionPane.YES_OPTION)
                System.exit(0);

            else if (result == JOptionPane.NO_OPTION) {

                frame.setDefaultCloseOperation(frame.DO_NOTHING_ON_CLOSE);
            }

        }
    });

}

}
不象在关闭时退出

JDialog不支持在关闭时退出

但是请慢慢关闭对话框

它立即关闭对话框,焦点将返回到父JFrame

有没有办法在JDialog中关闭整个程序

你需要合上相框

可能您正试图通过弹出对话框关闭应用程序?如果是,请签出

它将向您展示如何:

  • 使用WindowListener处理
    窗口关闭
    并显示弹出对话框,或
  • 使用建议的类使编码更容易

  • 1)
    setDefaultCloseOperation(JDialog.DISPOSE\u ON\u CLOSE);
    2)
    setTitle(“对话框”)
    @LuxxMiner但DISPOSE_ON_CLOSE缓慢关闭对话框不像EXIT_ON_CLOSE,这是什么意思DISPOSE_ON_CLOSE
    EXIT_ON_CLOSE
    关闭整个程序,
    DISPOSE_ON_CLOSE
    只是删除对话框,在这种情况下。这不像调用
    setVisible(false)
    ,因为您在处理完同一个对话框后不能再使用它。@LuxxMiner是否有一种方法可以在JDialogA
    WindowListener
    中关闭整个程序覆盖
    windowClosing
    方法可以通过调用
    System.exit(0)来实现这一点
    在它里面。对于JDialog没有
    退出_关闭
    。但是它显示了JFrame的窗口关闭,但我想用JDialog@JohnDoe
    对话框
    仍然扩展了
    窗口
    ,因此您也可以向对话框中添加
    窗口侦听器
    。@LuxxMiner和camickr感谢您的支持answers@JohnDoe,你错过了这个答案的全部要点。应用程序应该只有一个主JFrame。然后根据需要使用JDialog作为子窗口。普通的JDialog只会关闭自己,而不是整个应用程序。因此,我建议你尝试使用“确认对话框”要求用户确认关闭应用程序。因此,您需要一个JFrame,然后当用户单击关闭按钮时,JOptionPane(这是一个自定义JDialog)将显示。如果用户单击“是”,则对话框将关闭,应用程序将退出。@JohnDoe,我已经说过,您不应该在应用程序中使用JDialog作为单个窗口。JDialog不会出现在任务栏中,因此,如果您失去对对话框的关注,则返回对话框的唯一方法是通过我创建的所有窗口进行制表不是很好用。所以是的,我会考虑这个坏代码。而且,“对话”是一个AWT类,所以你的类名很混乱。甚至不需要创建一个类来设置几个属性。
    public static void main(String[] args) {
    
        Dialog frame = new Dialog();
    
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
    
                int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to exit the application? ",
                        "EXIT Application", JOptionPane.YES_NO_OPTION);
    
                if (result == JOptionPane.YES_OPTION)
                    System.exit(0);
    
                else if (result == JOptionPane.NO_OPTION) {
    
                    frame.setDefaultCloseOperation(frame.DO_NOTHING_ON_CLOSE);
                }
    
            }
        });
    
    }
    
    }