Java Swing对话框按钮和图标

时间:2013-03-09 15:20:00

标签: java swing button dialog icons

我目前正在学习Java,而且我正在上课,教授Dialog盒子。 目前我知道如何更改图标选项但是如何添加一些按钮?

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

^显示警告错误,然后显示确定按钮,但我还想要一个取消按钮

不幸的是

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

返回错误。基本上因为我是初学者,所以我没有把ok_cancel_option部分放在哪里。谢谢 ! :d

1 个答案:

答案 0 :(得分:4)

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

showMessageDialog()用于提醒,如果您需要使用showConfirmDialog()确认对话框,如上所示

PS:忘了提及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
}
相关问题