Design of Java dialog box

Source: Internet
Author: User
Tags exit final gettext integer

The

dialog box is a window that pops up from another window. Its purpose is to deal with special controversies and their details without confusing the original window. dialog boxes are used in a large number of settings in a programming environment, but as mentioned earlier, Sainus is used in a program slice.
We need to inherit from the dialog class to create other types of Windows, frame-like dialog boxes. Unlike window frames, dialog boxes cannot have menu bars or change cursors, but they are very similar. A dialog box has a layout manager (the default is BorderLayout layout manager) and overload action (), and so on, or handleevent () to handle the event. We will notice an important difference between handleevent (): When the Window_destory event occurs, we do not want to close the running application!
Instead, we can use the dialog window to free resources by calling Dispace (). In the following example, the dialog box is made up of a grid of special buttons defined there as the Toebutton of the class (using the GridLayout layout manager). The Toebutton button has drawn a frame around it and relies on its state: "X" or "O" in the void. It starts with a blank and then converts to "X" or "O" depending on the user's choice. However, when we click on the button, it switches back and forth between "X" and "O". (This creates a feeling like a crossword puzzle, which is certainly more annoying than it is.) In addition, this dialog box can be set to change numbers for many rows and columns in the main application window.
 

: Toetest.java//demonstration of dialog boxes//and creating your, own components import java.awt.*;
  Class Toebutton extends Canvas {int state = Toedialog.blank;
  Toedialog parent;
  Toebutton (toedialog parent) {this.parent = parent;
    } public void Paint (Graphics g) {int x1 = 0;
    int y1 = 0;
    int x2 = Size (). width-1;
    int y2 = size (). height-1;
    G.drawrect (x1, y1, x2, y2);
    x1 = X2/4;
    y1 = Y2/4;
    int wide = X2/2;
    int high = Y2/2;
      if (state = = toedialog.xx) {g.drawline (x1, y1, x1 + wide, y1 + high);
    G.drawline (x1, y1 + high, x1 + wide, y1);
    } if (state = = toedialog.oo) {g.drawoval (x1, y1, X1+WIDE/2, Y1+HIGH/2); } public boolean MouseDown (Event evt, int x, int y) {if (state = = Toedialog.blank) {state = Parent.tur
      N
        parent.turn= (Parent.turn = = toedialog.xx?)
    ToeDialog.OO:ToeDialog.XX); 
        else state = (state = = toedialog.xx?) ToeDialog.OO:ToeDIALOG.XX);
    Repaint ();
  return true;  } class Toedialog extends Dialog {//w = number of cells wide//h = number of cells high static final int BLANK
  = 0;
  static final int XX = 1;
  static final int OO = 2; int turn = XX;
    Start with X ' s turn public toedialog (Frame parent, int w, int h) {Super (parent, "The game itself", false);
    SetLayout (New GridLayout (W, h));
    for (int i = 0; i < w * H; i++) Add (new Toebutton (this));
  Resize (w *, H * 50);
    public boolean handleevent (Event evt) {if (evt.id = = Event.window_destroy) Dispose ();
    else return super.handleevent (EVT);
  return true;
  } public class Toetest extends Frame {TextField rows = new TextField ("3");
  TextField cols = new TextField ("3");
    Public Toetest () {settitle ("Toe Test");
    panel p = new Panel ();
    P.setlayout (New GridLayout (2,2));
    P.add (New Label ("Rows", Label.center));
    P.add (rows); P.add (New Label ("Columns", label.centER));
    P.add (cols);
    Add ("North", p);
  Add ("South", New button ("Go"));
    public boolean handleevent (Event evt) {if (evt.id = = Event.window_destroy) system.exit (0);
    else return super.handleevent (EVT);
  return true; Public boolean action (Event evt, Object Arg) {if (Arg.equals (' Go ')) {Dialog d = new Toedialog (this
      , Integer.parseint (Rows.gettext ()), Integer.parseint (Cols.gettext ()));
    D.show ();
    else return super.action (EVT, ARG);
  return true;
    public static void Main (string[] args) {Frame f = new toetest ();
    F.resize (200,100);
  F.show (); }
} ///:~

The Toebutton class retains a handle to the parent class of its toedialog type. As mentioned earlier, the combination of Toebutton and toedialog because a Toebutton can only be used by one toedialog, but it solves a series of problems, In fact, it's really not a bad solution because there is no other conversation class that can record the user's choice. Of course we can use other methods of manufacturing Toedialog.turn (Toebutton's static part). This approach eliminates their close connection, but it prevents us from having multiple toedialog at once (at least one of them is working properly anyway).
Paint () is a graph-related method: It draws a rectangle around the button and draws an "x" or "O". This is a complete, lengthy calculation, but it is very intuitive.
A mouse click is captured by an overloaded MouseDown () method, and the most important thing is to check if any events are written on the button. If not, the parent window is queried to find out who chose it and use it to determine the state of the button. It is worth noting that the button is then returned to the parent class and changes its selection. If the buttons already show this as "X" and "O", they will be changed. We can take note of the three groups of if-else that are conveniently used in these calculations as described in chapter III of this book. When a button's state changes, the button is repaint.
The Toedialog Builder is very simple: it adds some buttons to the GridLayout layout manager as we need them, and then adjusts each button to 50 pixels per side (if we don't adjust the window, it won't show up). Note that handleevent () calls Dispose () exactly for Window_destroy, so the entire application is not closed.
Toetest sets the entire application to create TextField (rows and columns for the input button grid) and the Go button. We will understand that the action () uses the less satisfying "string matching" technique in this program to test the press of the button (make sure we are correct in spelling and capitalization!). )。 When the button is pressed, the data in the TextField is removed and, because they are in the string structure, the static Integer.paresint () method needs to be used to transform the interrupt. Once the conversation class is established, we must call the show () method to display and activate it.
We will notice that the Toedialog object is assigned to a dialog handle D. This is an example of tracing back, although it does not really produce significant differences, because all events are called by Show (). However, if we want to invoke some of the methods that already exist in Toedialog, we need to assign a value to the Toedialog handle and we will not lose the information in a trace.

1. File Dialog class
In some operating systems, there are many special built-in dialogs to handle selected events, such as fonts, colors, printers, and similar events. Almost all operating systems support opening and saving files, but Java FileDialog packages are easier to use. Of course, this will no longer detect all the pieces that are used, because the patches are neither readable nor writable on the local disk. (This will swap the trust relationship for the program slice in the new browser.) )
The following application uses a two-file dialog class form, one is open and one is saved. Most of the code is now familiar to us, and all of these interesting activities occur in the action () method of two different button click events.

: Filedialogtest.java//Demonstration of File dialog boxes import java.awt.*;
  public class Filedialogtest extends Frame {TextField filename = new TextField ();
  TextField directory = new TextField ();
  Button open = New button ("open");
  Button save = New Button ("Save");
    Public Filedialogtest () {settitle ("File Dialog Test");
    panel p = new Panel ();
    P.setlayout (New FlowLayout ());
    P.add (open);
    P.add (save);
    Add ("South", p);
    Directory.seteditable (FALSE);
    Filename.seteditable (FALSE);
    p = new Panel ();
    P.setlayout (New GridLayout (2,1));
    P.add (filename);
    P.add (directory);
  Add ("North", p);
    public boolean handleevent (Event evt) {if (evt.id = = Event.window_destroy) system.exit (0);
    else return super.handleevent (EVT);
  return true; Public boolean action (Event evt, Object Arg) {if (evt.target.equals (open) {//two arguments, defaults to O
   Pen File:filedialog d = new FileDialog (This,     "What file do your want to open"); D.setfile ("*.java"); Filename filter D.setdirectory (".");
      Current directory d.show ();
      String OpenFile;
        if ((OpenFile = D.getfile ())!= null) {Filename.settext (openfile);
      Directory.settext (D.getdirectory ());
        else {filename.settext ("you pressed Cancel");
      Directory.settext (""); } else if (Evt.target.equals (save) {FileDialog d = new FileDialog (this, "What file does you want T
      o save? ", Filedialog.save);
      D.setfile ("*.java");
      D.setdirectory (".");
      D.show ();
      String SaveFile;
        if ((SaveFile = D.getfile ())!= null) {Filename.settext (savefile);
      Directory.settext (D.getdirectory ());
        else {filename.settext ("you pressed Cancel");
      Directory.settext ("");
    else return super.action (EVT, ARG);
  return true;
    public static void Main (string[] args) {Frame f = new filedialogtest ();
    F.resize (250,110);
  F.show (); }
} ///:~

For an Open File dialog box, we use the builder to set two arguments, first the parent window handle, followed by the caption of the FileDialog title bar. The Setfile () method provides an initial filename-perhaps the local operating system supports wildcard characters, so all the. java files in this example will be displayed at the very beginning. The Setdirectory () method selects the directory from which the file is determined (generally, the operating system allows the user to change the directory).
The show () command is not returned until the conversation class is closed. The FileDialog object always exists, so we can read the data from it. If we call GetFile () and it returns NULL, this means that the user exits the conversation class. The file name and the result of the call Getdirectory () method are displayed in the Textfields.
The same method is used to save the button, except for the FileDialog, which uses a different builder. This builder sets three arguments and a third argument must be Filedialog.save or Filedialog.open.

Related Article

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.