catalogue
Four message prompt box methods commonly used by JOptionPane class
Parameters of the JOptionPane class
Four message prompt box methods commonly used by JOptionPane class
showMessageDialog(); | Message dialog box |
showConfirmDialog(); | Option Dialog |
showOptionDialog(); | Custom selection dialog box |
showInputDialog(); | Input Dialog |
Introduction to parameters of JOptionPane class
parentComponent | Parent container (component) of settings dialog box |
message | Message content |
title | Dialog Title |
messageType | Message type. Each message type provides a default image |
icon | Sets the image of the custom dialog box |
optionType | Button type |
options | Custom button array, custom option button text |
initalValue | Sets the default selected button |
selectionValues | Custom options array, used to define drop-down boxes |
initialselectionValue | Sets the default selected option |
parentComponent: the dialog box will be displayed in the center of the container. If null is not specified and passed in, the default Frame will be used as the parent window of the dialog box, that is, the dialog box will be displayed in the center of the screen
showMessageDialog();
Parameter format:
JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);
1. Text box
JOptionPane.showMessageDialog(null,"Basic box","Title",JOptionPane.PLAIN_MESSAGE);
2. General prompt box
JOptionPane.showMessageDialog(null,"General prompt box"); JOptionPane.showMessageDialog(null,"General prompt box","Title",JOptionPane.INFORMATION_MESSAGE);
From the execution results of the above two codes, we can know that the default value of messageType is joptionpane.information_ The default value of message and title is "message"
This method can be used when you want to prompt the information of the dynamic array
3. Warning box
JOptionPane.showMessageDialog(null,"Warning box","Title",JOptionPane.WARNING_MESSAGE);
4. Error prompt box
JOptionPane.showMessageDialog(null,"Error prompt box","Title",JOptionPane.ERROR_MESSAGE);
5. Question box (question box)
JOptionPane.showMessageDialog(null,"Question box","Title",JOptionPane.QUESTION_MESSAGE);
showConfirmDialog();
Select the dialog box to set the buttons of the dialog box. Usually, the buttons are a combination of yes, no, OK and cancel
Parameter format:
JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType, icon)
1. Dialog box
int n = JOptionPane.showConfirmDialog(null,"Do you like it java?","Title",JOptionPane.YES_NO_CANCEL_OPTION); System.out.println(n);
Through the test, we can find that the return value of this method is int type. Select the first button to return 0, the second button to return 1, and so on
showOptionDialog();
Parameter format:
JOptionPane.showOptionDialog(parentComponent, message, title, optionType, messageType, icon, options, initialValue)
Object[] options = {"like", "dislike"}; //Defines the text on the button int n = JOptionPane.showOptionDialog(null,"do you like me?","Title",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options);
Object[] options = {"like", "dislike"}; //Defines the text on the button int n = JOptionPane.showOptionDialog(null,"do you like me?","Title",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[1]);
The first option is the name of the custom Object array, and the last option is the specified default option. If the subscript is not specified, it is equivalent to selecting the first option options[0]
showInputDialog();
Parameter format:
JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, initialSelectionValue)
1. Input box
String name = JOptionPane.showInputDialog(null,"Please enter your account\n","Title",JOptionPane.PLAIN_MESSAGE); System.out.println(name);
The return value type is String
2. Drop down box
Object[] options = {"China", "U.S.A","Germany",4}; //Define drop-down box options. When the array length is greater than or equal to 20, it will become a list box Object object = JOptionPane.showInputDialog(null,"Please choose your nationality\n","Title",JOptionPane.QUESTION_MESSAGE,new ImageIcon("D://Earth. png"), options," China "); System.out.println(object); System.out.println(object instanceof String);
Note here: why should the return value type be received with a variable of type Object?
Because this method returns the contents of the drop-down box you selected, it is received by applying the Object type. When "4" is selected, the return value type is int, and when "China" is selected, the return value type is String
3. List box
Object[] options = {"China", "U.S.A","Germany",4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; //Define drop-down box options. When the array length is greater than or equal to 20, it will become a list box Object object = JOptionPane.showInputDialog(null,"Please choose your nationality\n","Title",JOptionPane.QUESTION_MESSAGE,new ImageIcon("D://Earth. png"),options," China "); System.out.println(object instanceof String);
When the length of the options array exceeds 20, the dialog box will be presented as a list box