JDialog关闭按钮事件

时间:2015-06-12 05:34:24

标签: java swing jdialog

对于我的自定义JDialog

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

已经确定。

JDialog上有一个按钮。根据条件,它会调用dialog.dispose()

这两个操作都会触发windowClosed事件。但是我想确定它到达那里因为点击了关闭按钮或者因为调用了dispose方法。

1 个答案:

答案 0 :(得分:0)

WindowListener添加JDialog,并在windowClosed上设置布尔值或其他内容。还有一个buttonClicked布尔值,如果他们点击按钮就行了,如果他们点击窗口顶部的退出按钮就会出错。

boolean closed;
boolean buttonClicked;
JButton exitbutton;
JDialog dialog;
...
public void actionPerformed(ActionEvent arg0) {
    buttonClicked = true;
    dialog.dispose();
}
...
dialog.addWindowListener(new WindowListener() {

                public void windowActivated(WindowEvent arg0) {
                    // Do nothing
                }
                public void windowClosed(WindowEvent arg0) {
                    closed = true;
                    if(buttonClicked) {
                    //They cliked the button to close it.
                    } else {
                    // They didn't click the button, they clicked exit in the top right corner of screen.
                    }
                }
                public void windowClosing(WindowEvent arg0) {
                    // Do nothing
                }
                public void windowDeactivated(WindowEvent arg0) {
                    // Do nothing
                }
                public void windowDeiconified(WindowEvent arg0) {
                    // Do nothing
                }
                public void windowIconified(WindowEvent arg0) {
                    // Do nothing
                }
                public void windowOpened(WindowEvent arg0) {
                    // Do nothing
                }

            });