JDialog windowClosed无限期解雇

时间:2016-11-17 21:04:50

标签: java swing jdialog

我遇到的问题是关闭JDialog会导致windowClosed事件无限期地触发,直到我强行关闭应用程序。每当配置对话框时(通过窗口的X按钮或自定义JButton),对话框将关闭,windowClosed事件将不断触发。这是一个简短的完整类,用于演示行为:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class DialogEx extends JOptionPane {

    public static void main(String[] args) {
        final JOptionPane pane;
        final JDialog dialog;
        JButton exitButton = new JButton("Exit");

        pane = new JOptionPane(exitButton, JOptionPane.ERROR_MESSAGE, JOptionPane.PLAIN_MESSAGE);
        pane.setComponentOrientation((getRootFrame()).getComponentOrientation());
        pane.setMessageType(PLAIN_MESSAGE);
        pane.setOptions(new Object[] {});
        dialog = pane.createDialog(null, "Dialog");
        dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        dialog.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent wE) {
                System.out.println("windowClosing fired");
                dialog.dispose();
            }
            public void windowClosed(WindowEvent wE) {
                System.out.println("windowClosed fired");
                dialog.dispose();
            }
        });

        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        });

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

}

这里发生了什么? 我发现了类似的问题`WindowListener` acting up, perpetual firing。症状是一样的,但我不太确定原因是否相同,所提供的答案不适用于我的问题。

0 个答案:

没有答案