1.1 showMessageDialog
A modal dialog box with the OK button is displayed.
The following are several examples of using showMessageDialog:
Java code
- JOptionPane. showMessageDialog (null, "tip ");
The effect is as follows:
Java code
- JOptionPane. showMessageDialog (jPanel, "message", "title", JOptionPane. WARNING_MESSAGE );
The effect is as follows:
Java code
- JOptionPane. showMessageDialog (null, "message.", "title", JOptionPane. ERROR_MESSAGE );
Java code
- JOptionPane. showMessageDialog (null, "message.", "title", JOptionPane. PLAIN_MESSAGE );
1.2 showOptionDialog
This function can change the text displayed on the button. You can also perform more personalized operations.
General message box:
Java code
- Int n = JOptionPane. showConfirmDialog (null, "are you happy? "," Title ", JOptionPane. YES_NO_OPTION); // I = 0/1
The effect is as follows:
Personalized message box:
Java code
- Object [] options = {"OK! "," Go to the other side! "};
- Int m = JOptionPane. showOptionDialog (null, "can I ask you? "," Title ", JOptionPane. YES_NO_OPTION, JOptionPane. QUESTION_MESSAGE, null, options, options [0]);
The effect is as follows:
1.3ShowInoutDialog
This method returns an Object type. This Object type is generally a String type, reflecting the user input.
Example of a drop-down list:
Java code
- Object [] obj2 = {"football", "basketball", "Table Tennis "};
- String s = (String) JOptionPane. showInputDialog (null, "Please select your hobbies: \ n", "hobbies", JOptionPane. PLAIN_MESSAGE, new ImageIcon ("icon.png"), obj2, "soccer ");
The effect is as follows:
Example of text box format:
Java code
- JOptionPane. showInputDialog (null, "Enter your hobbies: \ n", "title", JOptionPane. PLAIN_MESSAGE, icon, null, "Enter here ");
The effect is as follows:
For the corresponding small icons, refer: