采取以下代码:

import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class WeirdDialogShitTest implements Runnable {
    private JFrame frame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new WeirdDialogShitTest());
    }

    @Override
    public void run() {
        frame = new JFrame("Test");
        frame.add(new JButton(new AbstractAction("Show Dialog") {
            @Override
            public void actionPerformed(ActionEvent event) {
                showDialog();
            }
        }));
        frame.pack();
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

    private void showDialog() {
        JDialog dialog = new JDialog(frame, "Dialog", Dialog.ModalityType.DOCUMENT_MODAL);
        dialog.add(new JLabel("Content here"));
        dialog.pack();
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent event) {
                JOptionPane.showMessageDialog(frame, "windowClosed fired");
            }
        });
        dialog.setVisible(true);
    }
}


我希望windowClosed被调用一次-对话框关闭时。

实际发生的情况是它被两次调用-对话框关闭时一次,包含框关闭时一次。

当我跟踪它以查看发生了什么时,这是我发现的内容:


在父级上调用dispose()时,它也会处理所有子级。很公平。
尽管不再存在,所有子对话框仍保留在子列表中。这对我来说似乎很狡猾。 (他们曾经被移除吗?)
dispose()无条件触发windowClosed窗口是否已被处置。这对我来说似乎也很狡猾。


最终结果是,对话框本身一次获得windowClosed,而每个祖先一次获得。 :/

但这也许是预期的行为吗?

最佳答案

应在主机上使用frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);。这样可以解决您的问题。尝试:

public class WeirdDialogShitTest implements Runnable {
    private JFrame frame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new WeirdDialogShitTest());
    }

    @Override
    public void run() {
        frame = new JFrame("Test");
        frame.add(new JButton(new AbstractAction("Show Dialog") {
            @Override
            public void actionPerformed(ActionEvent event) {
                showDialog();
            }
        }));
        frame.pack();

        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //Use EXIT_ON_CLOSE here.
    }

    private void showDialog() {
        JDialog dialog = new JDialog(frame, "Dialog", Dialog.ModalityType.DOCUMENT_MODAL);
        dialog.add(new JLabel("Content here"));
        dialog.pack();
        dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        dialog.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent event) {
                JOptionPane.showMessageDialog(frame, "windowClosed fired");

            }
        });
        dialog.setVisible(true);
    }
}

09-05 10:35