2012-07-15 137 views
8

我創建了一個JOptionPane類型爲showInputDialog。當它打開它時,會顯示兩個按鈕:確定取消。我想處理這個動作時,我推動取消按鈕,但我不知道如何達到它。我怎麼才能得到它?如何處理JOptionPane中的取消按鈕

+0

http://stackoverflow.com/a/10966330/829571 – assylias 2012-07-15 17:55:04

回答

21

例如:

int n = JOptionPane.showConfirmDialog(
          frame, "Would you like green eggs and ham?", 
          "An Inane Question", 
          JOptionPane.YES_NO_OPTION); 
if (n == JOptionPane.YES_OPTION) { 

} else if (n == JOptionPane.NO_OPTION) { 

} else { 

} 

可選地與showOptionDialog

Object[] options = {"Yes, please", "No way!"}; 
int n = JOptionPane.showOptionDialog(frame, 
       "Would you like green eggs and ham?", 
       "A Silly Question", 
       JOptionPane.YES_NO_OPTION, 
       JOptionPane.QUESTION_MESSAGE, 
       null, 
       options, 
       options[0]); 
if (n == JOptionPane.YES_OPTION) { 

} else if (n == JOptionPane.NO_OPTION) { 

} else { 

} 

詳情請參閱How to Make Dialogs

編輯:showInputDialog

String response = JOptionPane.showInputDialog(owner, "Input:", ""); 
if ((response != null) && (response.length() > 0)) { 

} 
+1

我需要使用showInputDialog返回一個字符串對象 – Mazzy 2012-07-15 18:02:32

6

showMessageDialog不應該顯示兩個按鈕,所以某些東西與您的代碼或您的解釋有關。無論如何,如果你想給用戶一個選擇並且想要檢測這個選擇,不要使用showMessageDialog而是使用showConfirmDialog,並且返回int並測試它是否是JOptoinPane.OK_OPTION。

+1

我犯了一個錯誤。 ..我的意思是showInputDialog。我有返回一個字符串對象 – Mazzy 2012-07-15 18:02:13

1

這是一個老問題了,我是一個Java的新手,所以有可能是更好的解決方案,但我想知道同樣的,也許它可以幫助別人,我確實是檢查響應是否爲空。

這爲我工作:

//inputdialog 
    JOptionPane inpOption = new JOptionPane(); 

    //Shows a inputdialog 
    String strDialogResponse = inpOption.showInputDialog("Enter a number: "); 

    //if OK is pushed then (if not strDialogResponse is null) 
    if (strDialogResponse != null){ 

     (Code to do something if the user push OK) 

    } 
    //If cancel button is pressed 
    else{ 

     (Code to do something if the user push Cancel) 

    } 
+0

但是,如果您的輸入是空的,並且您點擊確定按鈕,您的代碼不處理情況。你會得到錯誤。 – frank17 2016-04-02 20:57:35

-1

這可能是你的答案:

package Joptionpane; 

import javax.swing.JOptionPane; 

public class Cancle_on_JOptionPane { 

    public static void main(String[] args) { 
     String s; 
     int i; 
     for(i=0;i<100;i++){ 
     s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?"); 
         try{ 
           if(s.equals("")) { 
                  JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
                  i=2; 
                 } 
           else { 
              JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
              i=100; 
             } 
          } 
     catch(Exception e) 
       { 
       JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
       i=100; 
       } 
    } 
    } 
} 
+0

雖然這個代碼塊可能會回答這個問題,但最好能提供一些解釋。請[編輯]你的答案,包括這樣的描述。 – 2015-07-04 10:24:12

0
package Joptionpane; 

import javax.swing.JOptionPane; 

public class Cancle_on_JOptionPane { 

    public static void main(String[] args) { 
     String s; 
     int i; 
     for (i=0;i<100;i++){ 
      s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?"); 
      try { 
       if (s.equals("")) { 
        JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
        i=2; 
       } else { 
        JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
        i=100; 
       } 
      } 
      catch (Exception e) { 
       JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); 
       i=100; 
      } 
     } 
    } 
} 
+0

請添加關於代碼如何工作的說明。 – 2017-12-03 05:05:33