2013-03-09 45 views
0

我目前正在學習Java,並且正在教授對話框。 目前我知道如何更改圖標選項,但如何添加一些按鈕?Java Swing對話框按鈕和圖標

JOptionPane.showMessageDialog(null, "Text to display", "Title", JOptionPane.WARNING_MESSAGE); 

^顯示警告錯誤,因爲它應該和OK按鈕,但我想也有一個取消按鈕

不幸的是

JOptionPane.showMessageDialog(null, "Text to display", "Title", JOptionPane.WARNING_MESSAGE, JOptionPane.OK_CANCEL_OPTION); 

返回一個錯誤。基本上,因爲我是一個初學者,我沒有ideea把ok_cancel_option部分放在哪裏。謝謝 ! :d

回答

4
JOptionPane.showConfirmDialog(parent, 
           "message", 
           "title", 
           JOptionPane.YES_NO_OPTION); 

showMessageDialog()用於提醒,如果你願意,你必須使用showConfirmDialog()如上圖所示

PS一個confimation對話框:忘了提showConfirmDialog()返回結果

int result = JOptionPane.showConfirmDialog(parent, 
              "message", 
              "title", 
              JOptionPane.YES_NO_OPTION); 

if (result == JOptionPane.YES_OPTION){ 
    //stuff to do if yes 
} 
if (result == JOptionPane.NO_OPTION){ 
    //stuff to do if no 
} 
+0

Multumesc MULT pentru raspuns! – erasmus77 2013-03-09 15:28:55

+0

Cu placere,vezi completarea cum sa primesti raspunsul – 2013-03-09 15:32:47