Swing provides a Joptionpane class to implement the MessageBox functionality of a Windows platform, using the static methods in the Joptionpane class to generate standard dialog boxes that display information, ask questions, Warn, and User input parameters and other functions. these dialog boxes are modal dialogs:
Confirmdialog---Confirmation dialog box, ask the question, and then confirm by the user (press "Yes" or "No" button)
Inputdialog---Prompt to enter text
Messagedialog---display information
Optiondialog--Combines the other three dialog box types.
these four dialog boxes can be displayed in Showxxxdialog (), such as Showconfirmdialog () Display confirmation dialog box, Showinputdialog () Display Input Text dialog box, Showmessagedialog () The Display Information dialog box, Showoptiondialog () Displays the optional dialog box. The parameters they use are described below:
①parentcomponent: Indicates the parent window object for the dialog box, typically the current window. You can also use the default frame as the parent window, and the dialog box will be set to the center of the screen.
②message: Indicates the descriptive text to display in the dialog box
③string Title: Header Bar text string.
④component: Components to display in a dialog box (such as a button)
⑤icon: The icon to display in the dialog box
⑥messagetype: Generally can be for the following values Error_message, Information_message, Warning_message, Question_message, PLAIN_MESSAGE,
⑦optiontype: It determines the button options to be displayed at the bottom of the dialog box. Generally can be for default_option, Yes_no_option, Yes_no_cancel_option, Ok_cancel_option.
Use instance:
(1) Display Messagedialog
Joptionpane.showmessagedialog (NULL, "Descriptive text to be displayed in a dialog box", "caption string", joptionpane.error_message);
(2) Display Confirmdialog
Java code [Java] view plain copy joptionpane.showconfirmdialog (null, "Choose one", "Choose one", Joptionpane.yes _no_option); Joptionpane.showconfirmdialog (NULL, "Choose one", "Choose One", joptionpane.yes_no_option);
(3) Display Optiondialog: This kind of dialog box can be set by the user to the number of individual buttons and return the user clicks the number of buttons (starting from 0 count)
Java code[Java] View plain copy object[] options = {" OK ", " cancellation ", " help " }; int response= Joptionpane.showoptiondialog (this, " This is an option dialog box where users can select the number of their own buttons ", " Options Dialog box title ", joptionpane.yes_option, Joptionpane.question_ Message, null, options, options[0]); if ( response==0) { this.settitle (" You have pressed the OK button "); } else if (response==1) { this.settitle (" You have pressed Cancel button "); } Else&nbsP;if (response==2) { this.settitle (" You have pressed Help button "); } object[] options = {" OK ", " Cancel ", help "}; int response= Joptionpane.showoptiondialog (this, " This is an option dialog box where users can select the number of their own buttons ", " Options Dialog box title ", Joptionpane.yes_option, joptionpane.question_message, null, options, options[0]); if (response==0) { This.settitle (" You pressed the OK button "); }else if (response==1) { This.settitle (" You pressed the first cancel button "); } else