In swing, business-based considerations include a dialog box that restricts the user's behavior and prompts the user for action. The Joptionpane class is provided in swing to implement the functionality of the MessageBox like the Windows platform, as well as in Java, using the various static methods in the Joptionpane class to generate a variety of standard dialogs for displaying information, Ask questions, warnings, user input parameters and other functions. These dialog boxes are modal dialogs. Confirmdialog---Confirm the dialog box, ask the question, then confirm by the user (press "Yes" or "No" button) INputdialog---Prompt to enter text Messagedialog---display information Optiondialog--Combine the other three types of dialog boxes. These four dialogs can be displayed using Showxxxdialog (), such as Showconfirmdialog () display confirmation dialog, Showinputdialog () Display Input Text dialog box, Showmessagedialog () Display Information dialog box , Showoptiondialog () Displays the Selective dialog box. The parameters they use are described below: ①parentcomponent: Indicates the parent window object of the dialog box, typically the current window. You can also null to use the default frame as the parent window, at which point the dialog box is set to the center of the screen. ②message:Indicates the descriptive text to display in the dialog box ③string title: Title bar text string. ④component:Components to display in the dialog box (such as buttons) ⑤icon: The icon to be displayed in the dialog box ⑥messagetype:The following values can generally be 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 default_option, yes_no_option, Yes_no_cancel_option, Ok_cancel_option. Usage examples: (1) Display Messagedialog Joptionpane.showmessagedialog (NULL, "Descriptive text displayed in dialog box", "title bar text string", joptionpane.error_message); (2) Display Confirmdialog Joptionpane.showconfirmdialog (NULL, "Choose one", "Choose One", joptionpane.yes_no_option); (3) Display Optiondialog: This dialog box can be set by the user to the number of individual buttons and return the user to click the number of buttons (starting from 0 count) object[] options = {"OK", "Cancel", "Help"}; int Response=joptionpane.showoptiondialog (This, "This is an option dialog box, the user can choose their own number of 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 Cancel button"); } else if (response==2) {This.settitle ("You pressed the Help button"); } (4) Display inputdialog to allow user input String Inputvalue = joptionpane.showinputdialog ("Please input a value"); (5) Display Inputdialog to allow user to enter selectively Object[] PossibleValues = {"First", "Second", "third"}; User's selection item Object SelectedValue = Joptionpane.showinputdialog (null, "Choose One", "Input", joptionpane.information_message, NULL, PossibleValues, Possiblevalues[0]); Settitle ("You pressed" + (String) selectedvalue+ "project"); |