2013-03-23 152 views
1

首先抱歉我的英語不好。如何防止窗戶關閉?

嗨,我想與YES_NO_OPTION一起使用confirmDialog。 我想要的是,當我關閉一個框架confimDialog將顯示問我是否要關閉。

如果我按是一切最被關閉,如果我按無confirmDialog將會消失

但問題是,即使我不按任何鍵框關閉,這是我的代碼:

final JFrame frame = new JFrame("2D Game"); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setSize(1600,600); 
frame.setResizable(false); 

    private void continuerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_continuerButtonActionPerformed 
     int level=getlevel(); 
     System.out.println(niveau); 
     if(niveau == 1) 
     { 
      this.dispose(); 
      frame.add(new Board()); 
      frame.setVisible(true); 
      frame.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) 
      { 
       doExitOption(); 
      } 
      }); 
     } 
    } 

這是doExitOption梅索德:

public void doExitOption() 
    { 
     int option=JOptionPane.showConfirmDialog(null, "do you want to quit the game", "Warnning",JOptionPane.YES_NO_OPTION); 
      if(option == JOptionPane.YES_OPTION) 
      { 
       frame.dispose(); 
      } 
    } 

回答

3

您需要更改JFrame的默認關閉操作,使您的來電dispose正在取得處置窗口的呼叫:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
+0

非常感謝你幫助我! – 2013-03-23 23:56:04

0

將JFrame的默認關閉更改爲DO_NOTHING_ON_CLOSE