简体   繁体   English

所有者JFrame关闭时调用JDialog windowClosed事件

[英]JDialog windowClosed event called when owner JFrame close

I have the test code below. 我有下面的测试代码。 After opening the dialog when I close the it the windowClosed method is called, but it's called again when I close the JFrame and the dialog was just closed, why? 在关闭对话框后打开对话框后,将调用windowClosed方法,但是当我关闭JFrame并关闭对话框时又再次调用它,为什么? I am not closing the dialog in this case. 在这种情况下,我不会关闭对话框。 So I see: 所以我看到:

closed
closed

How can I prevent it? 我该如何预防?

I don't want to set EXIT_ON_CLOSE on JFrame (this will not call the windowClosed again) because this terminate the application and in the real case I can't do this. 我不想在JFrame上设置EXIT_ON_CLOSE(这不会再次调用windowClosed),因为这会终止应用程序,实际上,我不能这样做。

Thanks for the help 谢谢您的帮助

public final class Test4 {

    public static void main(String[] args) {

        Runnable runnable = new Runnable(){

            @Override
            public void run() {

                final JFrame frame = new JFrame("test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.setSize(300, 300);
                JButton btn = new JButton("test");
                frame.add(btn);
                frame.setVisible(true);
                btn.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent evt) {

                        openDialog(frame);
                    }
                });
            }
        };
        SwingUtilities.invokeLater(runnable);
    }

    private static void openDialog(Frame owner) {

        JDialog testDialog = new JDialog(owner);
        testDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        testDialog.setSize(100,100);
        testDialog.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosed(WindowEvent e) {

                System.out.println("closed");
            }
        });
        testDialog.setVisible(true);
    }
}

从注释和测试来看,这可能是Java 8中解决的一个错误,因为使用Java 8可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM