2016-02-17 70 views
-2

如果我有一個JOptionPane.showMessageDialog,當用戶按下紅色的x時如何退出程序?JOptionPane.showMessageDialog退出按鈕

JOptionPane.showMessageDialog(null, "Hello, World!"); 
+1

簡短的回答是,你不能。你必須使用showOptionDialog並以你想要的方式自定義它(例如,只顯示OK按鈕),然後檢查返回的狀態,如果它是CLOSED_OPTION,那麼通過不使用其中一個選項關閉對話框;) – MadProgrammer

+0

謝謝你你的想法,我會研究它! – jkjk

回答

1

下面是部分代碼應該是有用的:

   JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 

       frame.addWindowListener(new WindowAdapter() { 
        public void windowClosing(WindowEvent we) { 
         int result = JOptionPane.showConfirmDialog(null, "Exit?", "Confirm Exit", 
           JOptionPane.OK_CANCEL_OPTION); 
         if (result == JOptionPane.OK_OPTION) 
          System.exit(0); 

        } 
       }); 

您還可以使用JOptionPane.YES_NO_OPTION等

+0

謝謝你的幫助! – jkjk