2012-05-23 51 views
2

我有一個Java程序。當我運行這個程序時,它會給我一個GUI,就像我附上的那樣。JoptionPane ShowConfirmDialog

當我想關閉它時,它會提示出一個確認對話框。如果我按下「是」按鈕,它將使用System.exit()退出程序。

public static void main(String args[]) 
{ 
    ButtonTest app = new ButtonTest(); 
    app.addWindowListener( 
     new WindowAdapter() 
     { 
      public void windowClosing (WindowEvent e) 
      { 
       String message = " Really Quit ? "; 
       String title = "Quit"; 
       int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION); 
       if (reply == JOptionPane.YES_OPTION) 
       { 
        System.exit(0); 
       } 

      } 
     } 
    ); 
} 

如果我不想退出程序,該怎麼辦? System.continued()

+1

刪除'else {}'部分。 –

+0

但是當我點擊沒有,整個程序不見了,但我可以看到在命令提示符下,它仍然工作。 – Eric

+1

嘗試添加'app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);'。 –

回答

2

你不需要別人在這種情況下

+0

我有刪除,我已經把上面的代碼。當點擊「否」時,程序也消失了,但在命令提示符下,它仍在運行。 – Eric

2

嘗試設置這樣,

app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE) 

[編輯]

所以,你的代碼會變得這樣的事情,

public static void main(String args[]) { 
    ButtonTest app = new ButtonTest(); 
    app.addWindowListener(new WindowAdapter() { 
     public void windowClosing(WindowEvent e) { 
      int reply = JOptionPane.showConfirmDialog(null, 
        "Really Quit ?", "Quit", JOptionPane.YES_NO_OPTION); 
      if (reply == JOptionPane.YES_OPTION) 
       System.exit(0); 
     } 
    }); 
    app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 
    app.setSize(640, 480); 
    app.setVisible(true); 
} 

[Expl anation]

你可能會想,爲什麼它是這樣的。與Frame不同,JFrame的窗口關閉按鈕的行爲是隱藏窗口。因此,它會隱藏/關閉窗口。但是當你指定它還必須退出程序時,當用戶點擊yes。然後,除了關閉窗口,它也退出程序。當用戶點擊no時,它什麼都不做,只是關閉了窗口。因此,您必須明確告知DO_NOTHING_ON_CLOSE

[文件]

與Frame不同,一個JFrame對如何當 用戶試圖關閉窗口應對一些概念。默認行爲是當用戶關閉窗口時簡單地 隱藏JFrame。要更改默認的 行爲,請調用setDefaultCloseOperation(int)方法。要製作 ,JFrame的行爲與Frame實例相同,請使用 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)。

編號:JFrame docs

+0

但是當我點擊沒有,整個程序不見了,但我可以看到在命令提示符下,它仍然工作。 – Eric

+0

@Eric:相應地更新了我的答案。 –

+0

@Eric:添加了解釋和文檔參考。 –

1

如果你問我,我會去,對YES SELECTION而不是突然關閉我的應用與System.exit(0),我會選擇關閉我的應用程序,通過使用frameObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)的親切方式在NO SELECTION,我會去frameObject.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)。這是給你的幫助一個示例程序:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class ApplicationCloseExample 
{ 
    private void displayGUI() 
    { 
     final JFrame frame = new JFrame("Application Close Example"); 

     frame.addWindowListener(new WindowAdapter() 
     { 
      public void windowClosing(WindowEvent we) 
      { 
       int result = JOptionPane.showConfirmDialog(
           frame, "Do you want to Exit ?" 
           , "Exit Confirmation : ", JOptionPane.YES_NO_OPTION); 
       if (result == JOptionPane.YES_OPTION)    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       else if (result == JOptionPane.NO_OPTION) 
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
      } 
     }); 

     frame.setSize(300, 300); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new ApplicationCloseExample().displayGUI(); 
      } 
     }); 
    } 
} 
+0

我剛剛意識到使用〜JFrame.EXIT_ON_CLOSE〜與使用〜System.exit(0)〜相同,因此您可以使用〜JFrame.DISPOSE_ON_CLOSE〜作爲替代方案:-) –

0

如果你想,當你按下NO,把你的代碼的其餘部分中的其他塊或撥打你放置你的代碼的其餘部分的功能的程序繼續。

如果因爲JOptionPane.showConfirmDialog()會關閉而不想在NO按鈕上放置任何操作,所以刪除else塊也是一個選項。在if語句之後,您可以繼續使用其餘的代碼。

僅供參考 - 沒有System.continue()。該程序幾乎完成它自己的。

0

您可以添加一個else塊。如果你想再次運行main方法(我假設你這樣做),它應該看起來像這樣。如果用戶選擇否,您是否應該運行一些方法,無論是主要方法main(null)還是其他方法。

public static void main(String args[]) 
{ 
ButtonTest app = new ButtonTest(); 
app.addWindowListener( 
     new WindowAdapter() 
     { 
      public void windowClosing (WindowEvent e) 
      { 
       String message = " Really Quit ? "; 
       String title = "Quit"; 
       int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION); 
       if (reply == JOptionPane.YES_OPTION) 
       { 
        System.exit(0); 
       } 
       else 
       { 
        //whatever you plan on running instead here, instead of quitting, 
        //main(null) to run the main method, or put another method if you want 
       } 
      } 
     } 
    ); 
}