Home>

Dialogs are tools that provide interactive modes for human-computer conversation.Application through dialog box,Or provide users with information,Or get information from users.The dialog is a temporary window,You can place controls in it to get user input.In swing, there are two dialog classes,They are the jdialog class and the joptionpane class. The jdialog class provides construction and management of common dialogs;The joptionpane class provides many easy-to-use options for some common dialog boxes.For example, simple "yes-no" dialogs.

jdialog class

The jdialog class is used as the base class for dialog boxes.Dialogs are different from normal windows,Dialogs depend on other windows,When the window it depends on disappears or is minimized,The dialog box will also disappear;When the window is restored,The dialog box will resume automatically.

There are two types of dialogs:mandatory and non-mandatory.Mandatory dialogs cannot interrupt the dialog process,Until the end of the dialog,Only let the program respond to events outside the dialog box.Non-mandatory dialogs can interrupt the dialogue process,To respond to events outside the dialog.The mandatory type is also called a modal dialog box.Non-mandatory dialogs are also called non-modal dialogs.

jdialog object is also a container,So you can also assign a layout manager to the jdialog dialog,The default layout of the dialog is boarderlayout. But components cannot be added directly to the dialog,The dialog also contains a content panel,The component should be added to the content panel of the jdialog object.Since dialogs depend on windows,So to create a dialog,You must first create a window.

There are three common construction methods of the jdialog class:

jdialog (), constructs a non-mandatory dialog that is initially invisible. jdialog (jframef, string s), to construct a non-mandatory dialog that is not visible at initialization,The parameter f sets the window on which the dialog box depends,Parameter s is used to set the title.Usually first declare a subclass of the jdialog class,Then create an object of this subclass,A dialog box is created. jdialog (jframe f, string s, boolean b), constructs a titled s, and initializes the invisible dialog box.The parameter f sets the window on which the dialog box depends,Parameter b determines whether the dialog is mandatory or non-mandatory.

Other common methods of the jdialog class are the following:

gettitle (), Get the title of the dialog. settitle (string s), set the title of the dialog. setmodal (boolean b), set the modal of the dialog. setsize (), Set the size of the box. setvisible (boolean b), show or hide the dialog box.

[Example] The applet declares a user window class and dialog box class.The user window has two buttons and two text boxes,When a button is clicked,The corresponding dialog is activated.Enter the appropriate information in the dialog box,Press the OK button of the dialog.Determine how the buttons are monitored,Transfer the information entered in the dialog to the user window,The selection information is displayed in the corresponding text box of the user 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 ();
  }
  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 ("Window with dialog box");
  }
}

The above example creates a mandatory dialog,Changing to a non-mandatory dialog allows the user to pause during the conversation,Interact with other parts of the program.In this way, the effect of part of the dialog can be seen in the interface.

Changing the above example to a non-mandatory dialog requires only a few changes.The first is to change the code in the dialog constructor "super (f, s, true);" to "super (f, s, false);".

The second change:It turned out that in response to the OK button event,Only then call the method returnname () to return the string obtained by the dialog box to the program.Now when the text box enters the selection string,This method should be called immediately.To do this, you need to monitor the input events of the text box.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 resources
  }
}

joptionpane class

Often encounter very simple conversation situations,To simplify programming of common dialog boxes,The joptionpane class defines four simple dialog types,See Table 11-4. The joptionpane class provides a set of static methods,Let the user choose some type of dialog box.The following code is the selection confirmation dialog:

 int result=joptionpane.showconfirmdialog (parent, "Are you sure i want to exit", "Exit confirmation", joptionpane.yes_no_cancel_option);

The text "confirm" in the middle of the method name is the type of the create dialog,The text confirm indicates that the selection confirmation dialog is used.Change the word confirm to one of the other three types,It becomes the corresponding type of dialog box.The meaning of the four parameters of the above code is:the first parameter specifies the parent window of this dialog box;The second parameter is the text displayed in the dialog;The third parameter is the title of the dialog;The last parameter indicates that the dialog has three buttons,They are "Yes (y)", "No (n)", and "Undo". The return result of the method is the result after the user responded to this dialog box.See Table 11-5 for possible answers.

The input dialog requests the user to enter selection information as a list or text box.Users can select options from a list or enter information from a text box.The following is a schematic code of an input dialog for selecting a running project from the list:

 string result=(string) joptionpane.showinputdialog (parent,    "Please select a sport", "This is the sport selection dialog",    joptionpane.question_message, null,    new object [] {"play football", "play basketball", "run", "jump rope"}, "run");

The fourth parameter is the information type,The fifth parameter has no special effect here,Always use null;the sixth parameter defines an array of strings to choose from,The seventh parameter is the default value selected.The dialog box also includes "OK" and "Undo" buttons.

joptionpane dialog type

Results returned by the joptionpane dialog

Infotype options for joptionpane dialog

Sometimes, the program simply outputs some information,Users are not required to have feedback.Such a dialog box can be created with the following form of code:

 joptionpane.showmessagedialog (parent, "This is a java program",    "I am the output message dialog", joptionpane.plain_message);

The meaning of the first three parameters in the above code is the same as described above.The last parameter is to specify that the message type does not include any icons.

  • Previous Java implementation of excel table
  • Next Page jump and pop-up modal of ViewController in iOS development