简体   繁体   中英

Java Swing dialog buttons and icons

I am currently learning Java and i'm on a lesson that teaches Dialog boxes. Currently I know how to change the Icon option but how about adding some buttons?

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

^ shows the warning error as it should and the ok button but i'd like to also have a cancel button

Unfortunately

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

returns an error. Basically since i'm a beginner I have no ideea where to put the ok_cancel_option part. Thanks ! :D

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

showMessageDialog() is used for alerts if you want a confimation dialog you have to use showConfirmDialog() as shown above

PS: forgot to mention showConfirmDialog() returns a result

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM