2015-06-12 97 views
1

對於我的自定義JDialog的JDialog關閉按鈕事件

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

已設置。

JDialog上有一個按鈕。根據條件,它調用dialog.dispose()

這兩個操作都會觸發windowClosed事件。但我想確定它到達那裏,因爲關閉按鈕被點擊或因爲dispose方法被調用。

+1

難道你不能只在對話框類中設置一個標誌嗎? – KDM

+0

你從'event.getSource()'得到了什麼? – Arvind

+0

添加一個'ActionListener'到你的按鈕? –

回答

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 
       } 

      });