Introduction to the Java JOptionPane class

  • 2020-04-01 01:44:03
  • OfStack

1. Belongs to the javax.swing package.

2. Function: customize four different types of standard dialog boxes.

ConfirmDialog confirmation dialog box. Ask questions and let the user confirm (press the "Yes" or "No" button)

InputDialog prompts for text

The MessageDialog displays the information

OptionDialog combines the other three dialog box types.

3. These four dialogs can be displayed using showXXXDialog(). Such as:

ShowConfirmDialog () shows the confirmation dialog box,

ShowInputDialog () displays the input text dialog box,

ShowMessageDialog () display information dialog box,

ShowOptionDialog () displays an optional dialog box.

4. Parameter description.

(1) ParentComponent: indicates the parent window object of the dialog box, which is generally the current window.

It can also be null with the default Frame as the parent window, where the dialog box is set in the center of the screen. (2) message: indicates the descriptive text to be displayed in the dialog box (3) String title: title bar text String. (4) Component: the Component (such as button) to be displayed in the dialog box (5) Icon: the Icon to be displayed in the dialog box (6) messageType (Icon) :

ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE,

QUESTION_MESSAGE, PLAIN_MESSAGE, (7) optionType: the button option shown at the bottom of the dialog box.

DEFAULT_OPTION, YES_NO_OPTION, YES_NO_CANCEL_OPTION, OK_CANCEL_OPTION.

5. Use example: (1) display MessageDialog

JOptionPane. ShowMessageDialog (null, "to display the message content", "title", JOptionPane. ERROR_MESSAGE);

(2) shows ConfirmDialog

JOptionPane. ShowConfirmDialog (null, "message", "title", OptionPane. YES_NO_OPTION);

(3) display OptionDialog:

This dialog box can be set by the user to the number of each button and return the number of each button clicked by the user (counting from 0).

Object[] options = {" query "," deposit "," withdraw "," exit "};

Int the response = JOptionPane. ShowOptionDialog (null, "business type", "ATM", JOptionPane. YES_OPTION, JOptionPane. PLAIN_MESSAGE,

Null, options, options[0]);

If (the response = = 0)

{JOptionPane. ShowMessageDialog (null, "you press the query button"); }

Else if (response = = 1)

{JOptionPane. ShowMessageDialog (null, "you press the deposit button"); }

Else if (response = = 2)

{JOptionPane. ShowMessageDialog (null, "you press the button"); }

Else if (response = = 3)

{JOptionPane. ShowMessageDialog (null, "you press the exit button"); }

(4) display InputDialog for user input

String inputValue = JOptionPane. ShowInputDialog (" both Please input a value ");

(5) display the InputDialog so that the user can type selectively

Object[] possibleValues = {"First", "Second", "Third"};

// user's choice of items

The Object selectedValue = JOptionPane. ShowInputDialog (null,

"Choose one, ""Input," JOptionPane.INFORMATION_MESSAGE,

Null, possibleValues, possibleValues [0]);

SetTitle (" you pressed "+ (String)selectedValue+" item "); }


Related articles: