2010-11-26 72 views
0

下面是我的代碼爲什麼JOptionPane.getValue()繼續返回uninitializedValue

public static void main(String args[]){ 
    JOptionPane pane = new JOptionPane(); 
    pane.showInputDialog(null, "Question"); 
    Object value = value.getValue(); 
    System.out.println(value.toString()); --> this will print out uninitializedValue 

} 

我基本上要檢測,當用戶點擊的JOptionPane取消當用戶關閉的JOptionPane

回答

3

你應該這樣做:

String s = JOptionPane.showInputDialog(null, "Question"); 
    System.out.println(s); 

如果窗格被關閉,這將返回一個字符串null或按下取消。

+0

非常感謝你:d +1 – 2010-11-26 16:44:28

2

showInputDialog是一種靜態方法,它不會修改JOptionPane。正如dogbane指出你應該檢查返回值showInputDialog

如果您對實例調用靜態方法,某些編譯器會生成警告,因此請始終檢查編譯器警告。你的情況調用的方法是這樣的:

String result = JOptionPane.showInputDialog(null, "Question"); 
if(result == null){ 
//chancel pressed 
}else{ 
//normal code 
} 
+0

非常感謝你,但是羅布麻回答第一,所以我將迎來他的回答是正確的答案。再次感謝您+1 – 2010-11-26 16:45:06