The Joptionpane class encapsulates a number of methods.
Very convenient, so simple to tidy up a bit.
1.1 Showmessagedialog
Displays a modal dialog box with an OK button.
Here are a few examples of using Showmessagedialog:
Java code
- Joptionpane.showmessagedialog (NULL, "friendly hints");
The effect is as follows:
Java code
- Joptionpane.showmessagedialog (JPanel, "hint message", "title", Joptionpane.warning_message);
The effect is as follows:
Java code
- Joptionpane.showmessagedialog (NULL, "prompt message.", "title", Joptionpane.error_message);
Java code
- Joptionpane.showmessagedialog (NULL, "prompt message.", "title", Joptionpane.plain_message);
1.2 Showoptiondialog
This function can change the text displayed on the button. You can also perform more personalized actions.
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:
Statement message box:
Java code
- object[] Options ={"Good!" "Go aside!" " };
- int m = Joptionpane.showoptiondialog (null, "Can I ask you out?") "," title ", Joptionpane.yes_no_option, Joptionpane.question_message, NULL, Options, options[0]);
The effect is as follows:
1.3 Showinoutdialog
The method returns an object type. This type of object is typically a string type that reflects the user's input.
Example of the form of a drop-down list:
Java code
- Object[] Obj2 ={"Football", "basketball", "Table tennis"};
- string s = (string) Joptionpane.showinputdialog (NULL, "Please choose your hobby: \ n", "hobby", Joptionpane.plain_message, New ImageIcon (" Icon.png "), Obj2," football ");
The effect is as follows:
Examples of text box forms:
Java code
- Joptionpane.showinputdialog (NULL, "Please enter your hobby: \ n", "title", Joptionpane.plain_message,icon,null, "Enter in this");
The effect is as follows:
The corresponding small icon can be referenced by:
For detailed explanations, refer to the Oracle website
Some common methods of the Joptionpane class hint box