(Java) several methods of the pop-up dialog box in swing _ joptionpane. showmessagedialog

Source: Internet
Author: User
In swing, based on business considerations, a dialog box is displayed to restrict user behaviors and prompt user actions.

Swing provides the joptionpane class to implement the MessageBox function similar to the Windows platform. It is also available in Java. Various static methods in the joptionpane class are used to generate various standard dialogs, this function displays information, raises questions, warns, and user input parameters. These dialogs are all mode dialogs.
Confirmdialog--- Confirmation dialog box, ask a question, and then confirm it by the user (Press "yes" or "no)
INputdialog--- Prompt for text input
Messagedialog--- Display information
Optiondialog-- Combine the other three dialog box types.
The four dialogs can be displayed using showxxxdialog (), such as the showconfirmdialog () display confirmation dialog box, showinputdialog () display input text dialog box, showmessagedialog () display information dialog box, showoptiondialog () the Select dialog box is displayed. The parameters used are described as follows:
① Parentcomponent: Indicates the parent window object of the dialog box, which is generally the current window. You can also use the default frame as the parent window if it is null. In this case, the dialog box is set in the middle of the screen.
② Message:Indicates the descriptive text to be displayed in the dialog box.
③ String title: Title clause string.
④ Component:Components to be displayed in the dialog box (such as buttons)
⑤ Icon: Icon to be displayed in the dialog box
⑥ Messagetype:Generally, it can be error_message, information_message, warning_message, question_message, plain_message,
7. optiontype:It determines the button options to be displayed at the bottom of the dialog box. The options include default_option, yes_no_option, yes_no_cancel_option, and OK _cancel_option.
Instance used:
(1) display messagedialog
Joptionpane. showmessagedialog (null, "descriptive text displayed in the dialog box", "title clause string", joptionpane. error_message );
(2) display confirmdialog
Joptionpane. showconfirmdialog (null, "Choose one", "Choose one", joptionpane. yes_no_option );
(3) display optiondialog: In this dialog box, you can set the number of buttons and return the serial number of each button you click (counting starts from 0)
Object [] Options = {"OK", "cancel", "help "};
Int response = joptionpane. showoptiondialog (this, "This is an option dialog box. You can select the number of your buttons.", "option 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 have pressed the cancel button ");
}
Else if (response = 2)
{This. settitle ("You have pressed the Help button ");
}
(4) display inputdialog to allow users to enter
String inputvalue = joptionpane. showinputdialog ("Please input a value ");
(5) display inputdialog to allow users to selectively Input
Object [] possiblevalues = {"first", "second", "third"}; // select a project for the user
Object selectedvalue = joptionpane. showinputdialog (null, "Choose one", "input", joptionpane. information_message, null, possiblevalues, possiblevalues [0]);
Settitle ("You have pressed" + (string) selectedvalue + "project ");

 

//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////////

PackageOrg. Angus. Yang;

 

ImportJava. AWT. component;

 

ImportJavax. Swing. joptionpane;

 

Public classTestdialogExtendsComponent {

 

/**

In swing, based on business considerations, there will be a dialog box to restrict user behavior and prompt user actions. The following is the response content from alickman in csdn.

 

Swing provides the joptionpane class to implement the MessageBox function similar to the Windows platform. It is also available in Java. Various static methods in the joptionpane class are used to generate various standard dialogs, this function displays information, raises questions, warns, and user input parameters. These dialogs are all mode dialogs.

Confirmdialog --- in the confirmation dialog box, raise the question, and then confirm it by the user (press the "yes" or "no" button)

Inputdialog --- prompt for text input

Messagedialog --- display information

Optiondialog -- combine the other three dialog box types.

The four dialogs can be displayed using showxxxdialog (), such as the showconfirmdialog () display confirmation dialog box, showinputdialog () display input text dialog box, showmessagedialog () display information dialog box, showoptiondialog () the Select dialog box is displayed. The parameters used are described as follows:

① Parentcomponent: indicates the parent window object of the dialog box, which is generally the current window. You can also use the default frame as the parent window if it is null. In this case, the dialog box is set in the middle of the screen.

② Message: indicates the descriptive text to be displayed in the dialog box.

③ String title: The title clause string.

④ Component: components to be displayed in the dialog box (such as buttons)

⑤ Icon: the icon to be displayed in the dialog box

⑥ Messagetype: Generally, it can be error_message, information_message, warning_message, question_message, plain_message,

7. optiontype: it determines the button options to be displayed at the bottom of the dialog box. The options include default_option, yes_no_option, yes_no_cancel_option, and OK _cancel_option.

*/

Public voidSS (){

// Instance used:

// (1) display messagedialog

Joptionpane.Showmessagedialog(Null, "Descriptive text displayed in the dialog box", "title clause string ",

Joptionpane.Error_message);

 

// (2) display confirmdialog

Joptionpane.Showconfirmdialog(Null, "Choose one", "Choose one ",

Joptionpane.Yes_no_option);

 

// (3) display optiondialog: In this dialog box, you can set the number of buttons and return the number of buttons you click (counting starts from 0)

Object [] Options = {"OK", "cancel", "help "};

IntResponse = joptionpane.Showoptiondialog(This,

"This is an option dialog box. You can select the number of your buttons.", "option dialog box title", joptionpane.Yes_option,

Joptionpane.Question_message,Null, Options, options [0]);

If(Response = 0 ){

System.Out. Println ("you pressed the OK button ");

}Else if(Response = 1 ){

System.Out. Println ("You have pressed the cancel button ");

}Else if(Response = 2 ){

System.Out. Println ("You have pressed the Help button ");

}

 

// (4) display inputdialog for user input

String inputvalue = joptionpane.Showinputdialog("Please input a value ");

System.Out. Println ("the data you entered is:" + inputvalue );

// (5) display inputdialog to allow users to selectively Input

Object [] possiblevalues = {"first", "second", "third"}; // select a project for the user

Object selectedvalue = joptionpane.Showinputdialog(Null, "Choose one ",

"Input", joptionpane.Information_message,
Null
, Possiblevalues,

Possiblevalues [0]);

System.Out. Println ("You have pressed" + (string) selectedvalue + "project ");

}

 

Public static voidMain (string [] ARGs ){

Testdialog =NewTestdialog ();

Testdialog. SS ();

}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.