Recently I encountered a message prompt box in a swing program. The joptionpane class encapsulates many methods.
It was very convenient, So I simply sorted it out.
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:
Source: http://847353020-qq-com.iteye.com/blog/954532>
From Weizhi note (wiz)
Use of joptionpane