Java know how much (91) dialog box

Source: Internet
Author: User
Tags gettext

Dialogs are tools that provide interactive patterns for the man-machine conversation. The application passes the dialog box, or provides information to the user, or obtains information from the user. A dialog box is a temporary window in which you can place controls to get user input. In swing, there are two dialog box classes, which are the JDialog class and the Joptionpane class. The JDialog class provides constructs and manages common dialog boxes, and the Joptionpane class provides many easy-to-use options for common dialog boxes, such as the Simple "yes-no" dialog box.

JDialog class

The JDialog class is used as the base class for dialog boxes. The dialog box is different from the normal window, and the dialog box depends on other windows, and when the window on which it depends disappears or minimizes, the dialog box disappears, and the dialog box resumes automatically when the window is restored.

dialog boxes are divided into both mandatory and non-mandatory types. A mandatory dialog box cannot interrupt a dialog until the dialog box finishes, allowing the program to respond to events outside of the dialog box. The non-Mandatory dialog box can interrupt the dialog to respond to events other than the dialog box. Coercion is also called a modal dialog box, and a non-mandatory dialog box is also called a modeless dialog box.

The JDialog object is also a container, so you can 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 also contains a content panel, which should be added to the content panel of the JDialog object. Because the dialog box relies on the window, you must first create a window to establish the 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 a non-mandatory dialog box that initializes the invisible, parameter F sets the window on which the dialog depends, and the parameter s is used to set the caption. Usually a subclass of the JDialog class is declared first, and then an object of that subclass is created, and a dialog box is established.
    • JDialog (JFrame f,string S,boolean b), constructs a dialog box titled S, which initializes the invisible. Parameter F sets the window on which the dialog box depends, and parameter B determines whether the dialog box is mandatory or non-mandatory.


Other common methods of the JDialog class include the following:

    • GetTitle () to get the caption of the dialog box.
    • Settitle (String s), sets the caption of the dialog box.
    • Setmodal (Boolean B), sets 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 11-16" applet declares a user window class and a dialog box class, the User window has two buttons and two text boxes, and when a button is clicked, the corresponding dialog box is activated. Enter the appropriate information in the dialog box and press the OK button of 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 window.

1 Importjava.applet.*2 Importjavax.swing.*;3 Importjava.awt.*;4 Importjava.awt.event.*;5 classMywindowextendsJFrameImplementsactionlistener{6     PrivateJButton Button1,button2;7     Private Static intFlg=0;8     Private StaticJTextField text1,text2;9 Mywindow (String s) {Ten         Super(s); OneContainer con = This. Getcontentpane (); ACon.setlayout (NewGridLayout (2,2)); -          This. SetSize (200,100); -          ThisSetLocation (100,100); theButton1 =NewJButton ("Select Fruit"); -Button2 =NewJButton ("Select Food"); -Button1.addactionlistener ( This); -Button2.addactionlistener ( This); +Text1 =NewJTextField (20); -Text2 =NewJTextField (20); + Con.add (button1); A Con.add (button2); at Con.add (Text1); - Con.add (text2); -          This. setvisible (true); -          This. Pack (); -     } -      Public Static voidReturnname (String s) { in         if(FLG ==1) -Text1.settext ("Selected fruit is:" +s); to         Else if(FLG = = 2) +Text2.settext ("Selected food is:" +s); -     } the      Public voidactionperformed (ActionEvent e) { * Mydialog Dialog; $         if(E.getsource () = =button1) {Panax NotoginsengDialog =NewMydialog ( This, "Fruit"); -Dialog.setvisible (true); theFLG =1; +         } A         Else if(E.getsource () = =button2) { theDialog =NewMydialog ( This, "Food"); +Dialog.setvisible (true); -flg=2; $         } $     } - } - classMydialogextendsJDialogImplementsactionlistener{ the JLabel title; - JTextField text;Wuyi JButton done; the Mydialog (JFrame f,string s) { -         Super(F,s,true);//modal WuContainer con = This. Getcontentpane (); -title =NewJLabel ("Input" +s+ "name"); AboutText =NewJTextField (10); $Text.seteditable (true); -Con.setlayout (NewFlowLayout ()); -Con.setsize (200,100); -Setmodal (false); ADone =NewJButton ("OK"); +Done.addactionlistener ( This); theCon.setvisible (true); -          This. Pack (); $     } the      Public voidactionperformed (ActionEvent e) { the Mywindow.returnname (Text.gettext ()); theSetVisible (false); the Dispose (); -     } in } the  Public classExample6_6extendsapplet{ the Mywindow window; About Mydialog Dialog; the      Public voidinit () { thewindow =NewMywindow ("With dialog Window"); the     } +}

The above example creates a mandatory dialog box, which allows the user to pause during a conversation and interact with other parts of the program by changing to a non-mandatory dialog box. In this way, you can see the effect of some conversations in the interface.


Change the above example to a non-mandatory dialog box with only a few changes. The first is the Code "super (F,s,true)" in the method of constructing the dialog box; Change to "super (F,s,false);".

The second change: The Method Returnname () is called when the response determines the button event, and the string that gets the dialog box is returned to the program. The method should now be called immediately after the text box input selection string has ended. To do this, you need to monitor the input events for the text box and register the monitor for the text:

1  Public voidactionperformed (ActionEvent e) {2     if(E.getsource () = =text) {3 Mywindow.returnname (Text.gettext ());4     }5     Else if(E.getsource () = =Done ) {6 Mywindow.returnname (Text.gettext ());7SetVisible (false);8Dispose ();//Clear Resources9     }Ten}

Joptionpane class

Often encounter very simple dialog situations, in order to simplify the programming of common dialog boxes, the Joptionpane class defines four simple dialog box types, see table 11-4. The Joptionpane class provides a set of static methods that allow the user to select a type of dialog box. The following code is the Selection confirmation dialog box:
int result = Joptionpane.showconfirmdialog (parent, "Do you want to exit", "Exit Confirmation", joptionpane.yes_no_cancel_option);
Where the middle part of the method name "Confirm" is the type of dialog box created, the text Confirm indicates that the confirmation dialog box is selected. Changing the text confirm to one of the other three types becomes a dialog box of the corresponding type. The meaning of the four parameters of the above code is: the first parameter specifies the parent window of the dialog box, the second argument is the text displayed in the dialog box, the third parameter is the caption of the dialog box, and the last parameter indicates that the dialog has three buttons, namely "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 possible answers.

The input dialog box requests the user to enter selection information in a list or text box, and the user can select an option from the list or enter information from the text box. Here is a schematic code that selects the Input dialog box from the list to run the project:
String result = (string) joptionpane.showinputdialog (parent,
"Please select a sports item", "This is the sport selection dialog",
Joptionpane.question_message,null,
New object[]{"Playing football", "playing basketball", "running", "skipping"}, "Running";
The fourth parameter is the information type, see 11-6, the fifth parameter does not have a special effect here, always use NULL; The sixth parameter defines an array of strings to choose from, and the seventh parameter is the default value of the selection. The dialog box also includes the OK and Undo buttons.

Table 11-4 Joptionpane dialog box type
Input Enter by text box, list, or other means, plus OK and Undo buttons
Confirm Ask a question for user confirmation, plus "Yes (Y)", "No (N)" and "Undo" buttons
Information Displays a simple message, plus OK and Undo buttons
Options Displays a list of options for the user to select
Table 11-5 results returned by the Joptionpane dialog box
Yes_option The user pressed the Yes (Y) button
No_option The user pressed the "no (N)" button
Cancel_option The user pressed the Undo button
Ok_option The user pressed the OK button
Closed_option The user does not press any button to close the dialog box window
Table 11-6 Information Type options for Joptionpane dialog box
Plain_message Do not include any icons
Warning_message Include a warning icon
Question_message Include an issue icon
Informatin_message Include an informational icon
Error_message Include an error icon


Sometimes, a program simply outputs some information and does not require feedback from the user. Such dialogs can be created 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 previously described, and the last parameter is that the specified information type is not including any icons, see table 11-6.

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttonsJava know how many (88) lists and combo boxesJava know how many (89) lists and combo boxesJava know how much (90) menu

Java know how much (91) dialog box

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.