A dialog box is a tool that provides interactive mode for the Man-machine conversation process. The application passes through a dialog box, either providing information to the user or obtaining information from the user. A dialog box is a temporary window in which you can place controls for getting user input. In swing, there are two dialog classes, which are the JDialog class and the Joptionpane class. The JDialog class provides the construction and management of common dialog boxes; the Joptionpane class provides many handy options for common dialog boxes, such as a simple "yes-no" dialog box.
JDialog class
JDialog class as the base class for dialog boxes. Unlike a general window, a dialog box relies on other windows, and when the window it relies on disappears or minimizes, the dialog box disappears, and the dialog box resumes automatically when the window is restored.
The dialog box is divided into two types: mandatory and not mandatory. The Coercion dialog box cannot interrupt the dialog until the dialog is finished before the program responds to events outside the dialog box. A non-mandatory dialog box can interrupt the conversation to respond to events outside the dialog box. The coercion type is also called a modal dialog box, and the Non-coercion dialog box is also called the modeless dialog box.
The JDialog object is also a container, so you can also assign a layout manager to the JDialog dialog box, and the default layout for the dialog box is boarderlayout layout. However, the component cannot be added directly to the dialog box, and the dialog box contains a content panel that should be added to the JDialog object's content panel. Because the dialog box depends on the window, you must first create a window to establish a dialog box.
There are 3 common construction methods for the JDialog class:
- JDialog () constructs a non-mandatory dialog box that initializes the invisible.
- JDialog (jframef,string s), constructs an uninitialized, non-mandatory dialog box, parameter F sets the window on which the dialog box depends, and the parameter s is used to set the caption. You typically declare a subclass of a JDialog class, and then create an object of the subclass, and you build a dialog box.
- JDialog (JFrame f,string S,boolean b) Constructs an invisible dialog box with the title S. Parameter F sets the window on which the dialog box depends, and parameter B determines whether the dialog box is mandatory or not.
Other common methods for JDialog classes are as follows:
- GetTitle (), gets the title of the dialog box.
- Settitle (String s) to set the title of the dialog box.
- Setmodal (Boolean B) to set the mode of the dialog box.
- SetSize (), sets the size of the box.
- SetVisible (Boolean B) to show or hide the dialog box.
The example small application declares a user window class and a dialog box class that has two buttons and two text boxes, and the corresponding dialog box is activated when a button is clicked. Enter the appropriate information in the dialog box and press the OK button for the dialog box. Determines how the button is monitored, transmits the information entered in the dialog box to the User window, and displays the selection information in the corresponding text box in the user's window.
Import java.applet.* import javax.swing.*;
Import java.awt.*;
Import java.awt.event.*;
Class Mywindow extends JFrame implements actionlistener{private JButton button1,button2;
private static int flg=0;
private static JTextField text1,text2;
Mywindow (String s) {super (s);
Container con = This.getcontentpane ();
Con.setlayout (New GridLayout (2,2));
This.setsize (200,100);
This setlocation (100,100);
button1 = new JButton ("Select Fruit");
Button2 = new JButton ("Select Food");
Button1.addactionlistener (this);
Button2.addactionlistener (this);
Text1 = new JTextField (20);
Text2 = new JTextField (20);
Con.add (button1);
Con.add (button2);
Con.add (Text1);
Con.add (TEXT2);
This.setvisible (TRUE);
This.pack ();
The public static void Returnname (String s) {if (FLG ==1) text1.settext ("Selected fruit is:" +s);
else if (FLG = 2) text2.settext ("Selected food is:" +s);
public void actionperformed (ActionEvent e) {Mydialog dialog; if (E.getsoUrce () ==button1) {dialog = new Mydialog (This, "fruit");
Dialog.setvisible (TRUE);
FLG = 1;
else if (E.getsource () ==button2) {dialog =new Mydialog (this, "food");
Dialog.setvisible (TRUE);
flg=2;
}} class Mydialog extends JDialog implements actionlistener{JLabel title;
JTextField text;
JButton done;
Mydialog (JFrame f,string s) {super (f,s,true);//modal Container con = This.getcontentpane ();
title = new JLabel ("Enter" +s+ "name");
Text = new JTextField (10);
Text.seteditable (TRUE);
Con.setlayout (New FlowLayout ());
Con.setsize (200,100);
Setmodal (FALSE);
Done = new JButton ("OK");
Done.addactionlistener (this);
Con.setvisible (TRUE);
This.pack ();
public void actionperformed (ActionEvent e) {mywindow.returnname (Text.gettext ());
SetVisible (FALSE);
Dispose ();
} public class Example6_6 extends applet{mywindow window;
Mydialog Dialog; public void init () {window = new Mywindow ("band pairWords Box window ");
}
}
The example above creates a mandatory dialog box, which allows the user to pause during the conversation and interact with other parts of the program, instead of a forced dialog. In this way, you can see the effect of some conversations in the interface.
Change the above example to a non-mandatory dialog box as long as a small amount of changes can be made. The first is the Code "super" (F,s,true) in the dialog construction method. Change to "super (F,s,false);".
The second change is that the method Returnname () is invoked when the response determines the button event, and returns the string obtained by the dialog box to the program. Now when the text box input selection string ends, you should call the method immediately. To do this, you need to monitor the input events for the text box and register the monitor for the text:
public void actionperformed (ActionEvent e) {
if (E.getsource () ==text) {
mywindow.returnname (Text.gettext ());
}
Else if (E.getsource () ==done) {
mywindow.returnname (Text.gettext ());
SetVisible (false);
Dispose ();//Clear Resource
}
}
Joptionpane class
Often encountering very simple dialogs, the Joptionpane class defines four simple dialog box types to simplify the programming of common dialogs, see table 11-4. The Joptionpane class provides a set of static methods that allow users to select a type of dialog box. The following code is the Selection confirmation dialog box:
int result = Joptionpane.showconfirmdialog (parent, "Do you really want to quit", "Exit Confirmation", joptionpane.yes_no_cancel_option);
The middle part of the method name "Confirm" is the type that creates the dialog box, and the text Confirm indicates the selection confirmation dialog box. Changing the text confirm to one of the other three types becomes a dialog box of the appropriate type. The meaning of the four parameters of the preceding code is that the first parameter specifies the parent window of the dialog box; The second argument is the text that the dialog box displays, the third argument is the caption of the dialog box, and the last parameter indicates that the dialog box has three buttons, "Yes (Y)", "No (N)", and "undo". The return result of the method is the result of the user responding to the dialog box, as shown in table 11-5 for the possible answers.
Enter a dialog box to request user input selection information in a list or text box, where the user can select an option from the list or enter information from the text box. The following is a schematic code for the Input dialog box that selects the running project from the list:
String result = (string) joptionpane.showinputdialog (parent,
"Please select a sport item", "This is the Motion selection dialog box",
Joptionpane.question_message,null,
new object[]{"playing football", "playing basketball", "running", "Skipping Rope"}, "running");
The fourth parameter is the information type, and the fifth parameter has no special effect here, always null, and the sixth parameter defines an array of strings to select, and the seventh parameter is the default value selected. The dialog box also includes the OK and Undo buttons.
Joptionpane dialog box type
Results returned by the Joptionpane dialog box
Information type options for the Joptionpane dialog box
Sometimes, the program simply outputs some information and does not require feedback from the user. Such a dialog box can be created by using the following form of code:
Joptionpane.showmessagedialog (Parent, "This is a Java program",
"I am the Output Information dialog box", Joptionpane.plain_message);
The first three parameters in the preceding code have the same meaning as described earlier, and the last argument is that the specified information type does not include any icons.