2013-08-05 140 views
1

我有一個棘手的問題。當用戶點擊窗口右上角的關閉標記時,我需要彈出一個確認消息框供用戶決定是否關閉。但它不起作用。無論您在彈出消息框中選擇什麼,該對話框將始終關閉。但是當我創建一個JFrame時,同樣的邏輯工作正常。如何監視JDialog的關閉事件?

下面是我的netbeans IED中的代碼。 我只是用一個WindowAdpter來捕捉關閉事件,並且修改了由neatbean生成的虛擬代碼。

謝謝。

import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 


public class TestClosingDialog extends javax.swing.JDialog { 

    public TestClosingDialog(java.awt.Frame parent, boolean modal) { 
     super(parent, modal); 
     initComponents(); 


     this.addWindowListener(new WindowAdapter() { 
      @Override 
      public void windowClosing(WindowEvent e) { 
       int a = JOptionPane.showConfirmDialog(null,"Are you sure you need to close?", "Tip", JOptionPane.YES_NO_OPTION); 
       if (a == 0) { 
        System.out.println("yes " + a); 
        System.exit(0); //close 
       } else if (a==1) { 
        System.out.println("no " + a); 
       } 
      } 
     }); 
    } 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 400, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 300, Short.MAX_VALUE) 
    ); 

    pack(); 
}// </editor-fold>       

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the dialog */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      TestClosingDialog dialog = new TestClosingDialog(new JFrame(), true); 
      dialog.setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify      
// End of variables declaration     
} 

回答

1

變化setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING);

這將需要在對話框中手動dispose。根據您的需求,您只需將需要改變System.exit(0);dispose();

我個人而言,建議不要直接從像JDialog頂層容器延伸,但是這只是我。

我更喜歡使用類似於JPanel的東西,它具有可顯示對話框的輔助方法。這種方法會創建對話框並將其添加到它,例如

+0

非常感謝,哥們。有用。是的,我按照您的建議,使用JPanel以相同的方式執行相同的操作。這只是一個測試演示。真的很感激,你節省了我的時間。 – xuqin1019

+0

順便說一句,你有關於如何打包和分發擺動桌面應用程序的任何想法。我已經在NetBeans中完成了一個swing項目,現在我需要打包並分發它。我使用mysql數據庫。我可以將它作爲一個exe安裝文件打包嗎?因此,即使用戶的計算機上沒有JRE環境,用戶也可以像普通的Windows軟件一樣安裝它。提前致謝。 – xuqin1019

+1

很明顯,有很多種方式,從一個乾淨的構建開始。它會輸出dist目錄中的二進制文件。你可以創建一個安裝程序,像[izPack](http://izpack.org/),它是開源的,可能適合初學者。你也可以看看[JNLP](http://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/jnlp.html)。創建'exe'文件並不是絕對的要求,我個人大部分時間都是這樣做的,因爲默認情況下我傾向於通過'7Zip'打開'jar'設置。我使用[exe4j](http://www.ej-technologies.com/products/exe4j/overview.html) – MadProgrammer