JAVA Tutorial Session Five AWT graphical user interface design (1)

5.1 with AWT generate a graphical user interface
package AWT (Abstract Window Toolkit) Abstract Window Toolkit is the establishment of a graphical user interface GUI (Graphics User Interface) toolkit API for the Java program provides, AWT available for Java applet and applications in which support GUI programming features include: a user interface component; event handling model; graphical images and tools, including the shape, color and font classes; layout manager, a window can be flexibly irrespective of the size and layout of the particular window screen resolution ; data transfer classes, can be cut and paste clipboard by native platform
5.1.1 java.awt package java.awt package provides classes and interfaces used by GUI design can be seen from FIG main 5.1 the relationship between class
java.awt package provides a GUI design tool substantially java program mainly includes the following three concepts:
component --Component
container --Container
layout manager --LayoutManager
5.1.2 components and containers
Java the most basic components of a graphical user interface components (component), is a component can be displayed graphically and can cross the user on the screen Mutual object, for example. Component can not be independently displayed a button, and the like out of a label, the component must be placed before a certain container can be displayed.
Class java.awt.Component many components is a parent class, Component class packaging components common methods and properties, such as a graphic component object, size, display position, foreground and background colors, borders, visibility, and so, many component class also inherits the component class member methods and member variables, the corresponding the members of the method include:
getComponentAt in (int X, int Y)
getFont ()
getForeground ()
getName ()
getSize ()
Paint (Graphics G)
the repaint ()
Update ()
setVisible (Boolean B)
the setSize (the Dimension D)
the setName (String name) and the like
of the container (Container ) is a class, a subclass of component fact, the container itself is thus a component, having all the properties of the components, but its primary function is to house other components and containers.
layout manager (LayoutManager): each container has a layout manager, when the container is required to determine its position or sizes of a component, will call its corresponding layout manager.
in order for us to generate a graphical user interface having a good platform-independent, Java language, providing this layout manager tool to manage the layout of the components in a container, without using the position and size of the components directly manner.
when the arrangement position and size of components in the program, the following points should be noted:
1. container layout Manager is responsible for the size and location of the various components, so users can not set these properties of the components in this case. If you try to use the Java language provides setLocation (), setSize (), setBounds () method and the like, the layout manager will be covered.
2. If the user does need to personally set the size or location of the components, layout manager should cancel the container, method:
setLayout (null);
5.1.3 common container
. java.awt.Container container is a subclass of Component, a container may receive a plurality of components, and make them an integral container can be simplified graphical interface design to the overall structure the arrangement of the interface for all containers can add a component to the container by an add () method.
there are three types of containers: Window, Panel, ScrollPane, commonly used Panel, Frame, the Applet.
1.Frame
the following is an example of a container:
Example 5.1
Import in java.awt *;.
public class of MyFrame the extends Frame {
public static void main (String args []) {
of MyFrame of MyFrame new new fr = ( "Out There the Hello!");
// constructor
fr.setSize (200,200);
// set the Frame size defaults to (0, 0)
fr.setBackground (Color.red);
// set the background Frame of default is red
fr.setVisible (to true);
// set Frame to be seen, missing Province invisible
}
public of MyFrame (String STR) {
super (str); // call the constructor of the superclass
}
}
see the results running
ships .Frame Window class we want to generate the appearance of a window, usually a subclass of Frame Window to be instantiated, and not used directly to like window we normally see in the windows system, there is the title, borders, menus, size, etc. after each frame object is instantiated, and size are not invisible, so you must call setSize () to set the size call setVisible (true) to set the window to be visible.
in addition, AWT in the actual operation of the process is to call the platform where the graphics system, so the same period of AWT program running under different operating system platforms have seen graphics system . is not the same window, for example, running under windows, windows style displays the window; while running under UNIX, then a UNIX-style display window.
2. Panel
example 5.2
Import in java.awt *;.
public FrameWithPanel the extends Frame {class
public FrameWithPanel (String STR) {
Super (STR);
}
public static void main (String args []) {
FrameWithPanel new new FrameWithPanel fr = ( "Frame with Panel");
Panel P an = new Panel ();
fr.setSize (200,200);
fr.setBackground (Color.red);
// frame fr background color to red
fr.setLayout (null);
// cancel layout manager
pan.setSize (100,100);
pan.setBackground ( Color.YELLOW);
// set the background color of the panel as a yellow pan
fr.add (pan); // add method to add a panel to the frame fr pan in fr.setVisible (to true);
}
}
see the results running
Usually we want generate a window, usually with a subclass of frame window to be instantiated, rather than directly use the window class .Frame window looks like we normally see in the windows system, there is the title, borders, menus, size, etc. etc. each Frame object after instantiation, and the size is not invisible, it must call the setSize () to set the size, call setVisible (true) to set the window visible.
Further, the actual running the AWT the process is a platform where the graphics system calls, so the same period of AWT program running under different operating system platforms have seen graphics system is not the same window running under windows, for example, displays a windows-style window.; and when running under UNIX, Shows the UNIX-style window.
5.1.4 the LayoutManager layout manager (1)
java To achieve cross-platform and obtain the layout of dynamic effects, java all of the components within the vessel arranged for a "Layout Manager" manages, such as: the order, the size of the component, a position, when the window is moved or resized after how components change and other functions delegated to the corresponding container layout manager to manage different layout manager uses different algorithms and strategies, the container can be determined layout by choosing a different layout manager.
layout Manager include: FlowLayout, BorderLayout, GridLayout, CardLayout, GridBagLayout
Example 5.3
Import in java.awt *;.
public class ExGui {
Private Frame F;
Private the Button B1;
Private the Button B2;
public static void main (String args []) {
ExGui that new new ExGui = ();
that .go ();
}
public void Go () {
F = new new Frame ( "the GUI Example");
f.setLayout (the FlowLayout new new ());
// Sets the layout manager is the FlowLayout
B1 = the Button new new ( "Press Me" );
display character "Press Me" button on //
the Button new new = B2 ( "the Do Not Press Me");
f.add (B1);
f.add (B2);
f.pack ();
// compact arrangement, which acts as the setSize (), i.e., so that the window
as small as possible, just to be able to accommodate small live b1, b2 two buttons
f.setVisible (to true);
}
}
view the run results
1. the FlowLayout
the FlowLayout a Panel, Applet default layout manager component is placed from the law. to bottom, left to right place, if the container is wide enough, the first component is added to the first vessel to the leftmost line, are sequentially added to the subsequent assembly of the components on a right side, if placed less than the current row components, are placed to the left of the next row.
configured mainly the following several methods:
the FlowLayout (FlowLayout.RIGHT, 20,40);
/ * the first parameter indicates alignment of the component, the component refers to the position in this line is centered, justified or right-ranking left aligned, the second parameter is the lateral spacing between the components, the third parameter is the longitudinal spacing between the components, the pixel units * /.
the FlowLayout (FlowLayout.LEFT);
// left aligned, laterally spaced and longitudinally spaced default value of five pixels are
the FlowLayout ();
// default alignment direction Centered, are laterally spaced and longitudinally spaced default value of five pixels
Example 5.4
* in java.awt Import;.
public class myButtons {
public static void main (String args [])
{
Frame Frame new new F = ();
f.setLayout (the FlowLayout new new ());
the Button Button1 the Button new new = ( "Ok") ;
the Button Button2 the Button new new = ( "the Open");
the Button new new Button3 = the Button ( "the Close");
f.add (Button1);
f.add (Button2);
f.add (Button3);
f.setSize (300, 100) ;
f.setVisible (to true);
}
}
see the results running
time is changed when the size of the container, with the assembly will change FlowLayout management, which variation is: size of the component constant, but vary the relative position of the example. FIG there are three buttons in the same row, but if the window is narrowed, just to narrow down to a button, the second button into the second fold line, the third button to fold the third row. button "Open" would have been in the "OK" button on the right side, but now went below, so that the "size of the component is unchanged, but the relative position will change."
2. BorderLayout
BorderLayout is Window, Frame Dialog, and the default layout manager layout manager .BorderLayout the container is divided into five areas:. North, South, East, West , and Center, each region can place a component of the position and size of each area as shown below:
Example 5.5
. Import in java.awt *;
public class buttonDir {
public static void main (String args []) {
Frame Frame new new F = ( "the BorderLayout");
f.setLayout (the BorderLayout new new ());
f.add ( "North", the button new new ( "North"));
// first parameter to add a button to the container North region
f.add ( "South", the button new new ( "South"));
/ / first parameter to add a button to the South region of the vessel
f.add ( "East", the button new new ( "East"));
// first parameter to add a button to the container East region
f.add ( "West", the button new new ( "West"));
// first parameter to add a button to the West region of the vessel
f.add ( "Center", the Button new new ( "Center"));
// first parameter to add a button to the container region Center
f.setSize (200,200);
f.setVisible (to true);
}
}
view the operation results
when using the BorderLayout, if the size of the container is changed, which is a variation: the relative positions of the components change, change the size of the container e.g. becomes higher, the North, South region unchanged, West, Center, East region is high; if the vessel has broadened, West, East region constant, North, Center, South widened area does not necessarily have all of the area. assembly, if the surrounding region (West, East, North, South region) has no components to supplement by the Center region, but if the Center region has no components remains empty, which results as shown in the several views:
North region missing components
North Center region and a lack of assembly
3. GridLayout
container various components like a lattice layout, average occupation space of the container.
Example 5.6
Import in java.awt *;.
public class ButtonGrid {
public static void main (String args []) {
Frame F = new new Frame ( "GridLayout");
f.setLayout (new new GridLayout (3,2-));
// containers were divided into three rows and two columns 6 cells f.add (new Button ( "1" )); / the first cell / added to the first row
f.add (new Button ( "2" )); // add to a first line of the next frame
f.add (new Button ( "3" )); // add to the first cell of the second row
f. add (new Button ( "4" )); // add to the next second line grid
f.add (new Button ( "5" )); // add to the first cell of the third row
f.add ( new Button ( "6")) ; // add to the next cell of the third row
f.setSize (200,200);
f.setVisible (to true);
}
}
see the results running
5.1.4 LayoutManager layout manager (2)
4 . a CardLayout
a CardLayout layout manager can help users deal with two or even more members share the same display space, the container into which a plurality of layers, each size of the entire display space occupied by the container, allowed to stand, but each only one component, of course, each layer can be implemented complex user interfaces using the Panel. Like brand layout manager (CardLayout) as a neatly folded cards, 54 cards, but you can only see the top of a card each card is equivalent to the card layout manager in each layer.
Example 5.7
Import the java.awt *;.
Import java.awt.event *;. content // event handling mechanism, the next section of the
public class ThreePages implements MousList ener {
CardLayout layout = new CardLayout (); // instantiate a card layout manager object
Frame Frame new new F = ( "A CardLayout");
the Button page1Button;
the Label page2Label; // the Label is the label, in fact, his string
TextArea page3Text; // multi-line text area of the plurality of columns
the Button page3Top;
the Button page3Bottom;
public static void main (String args [])
{new new ThreePages () Go ();.}
public void Go ()
{f.setLayout (layout); / / set the layout manager for the card layout
f.add (= new new page1Button the Button ( "the Button Page"), "page1Button"); / * the second parameter "page1Button" represents the brand you taken this layer name * /
page1Button.addMouseListener (the this); // register listener
f.add (= new new page2Label the Label ( "the Label Page"), "page2Label");
page2Label.addMouseLisener (the this); // Register listener
Panel panel = new Panel ();
panel.setLayout (the BorderLayout new new ());
panel.add (page3Text the TextArea new new = ( "Composite Page"), "Center");
page3Text.addMouseListener (the this);
panel.add (page3Top the Button new new = ( "Top Button" ), "North");
page3Top.addMouseListener (the this);
panel.add (page3Bottom the Button new new = ( "Bottom Button"), "South");
page3Bottom.addMouseListener (the this);
f.add (Panel, "Panel" );
f.setSize (200,200);
f.setVisible (to true);
}
HH
}
5. the container nested
in a complex graphical user interface design in order to make the layout easier to manage, with simple overall style, contains a a plurality of container assemblies themselves can be used as a component added to another vessel, and then adding the container vessel, thus forming a nested containers. the following is an example of a nested container.
Example 5.8
Import in java.awt. *;
public class ExGui3{
private Frame f;
private Panel p;
private Button bw,bc;
private Button bfile,bhelp;
public static void main(String args[])
{
ExGui3 gui = new ExGui3();
gui.go();
}
public void go(){
f = new Frame("GUI example 3");
bw=new Button("West");
bc=new Button("Work space region");
f.add(bw,"West");
f.add(bc,"Center");
p = new Panel();
f.add(p,"North");
bfile= new Button("File");
bhelp= new Button("Help");
p.add(bfile);
p.add(bhelp);
f.pack();
f.setVisible(true); summary: View run results }
}



1.Frame is a top-level window .Frame default layout manager is the BorderLayout.
2.Panel not shown separately, must be added to a container. Panel default layout manager is FlowLayout.
3. When the Panel as a after a component is added to the vessel, which may have its own still Panel layout manager. Thus, we can use the Panel that BorderLayout a plurality of region display assembly, designed to achieve the sophisticated user interfaces.
4. If no layout Manager setLayout (null), you must use setLocation (), size and position setSize (), setBounds () methods to manually set component, this method results in platform-dependent, does not encourage use.
5.2 AWT event handling model
one on how the main content is to place the various components, the graphical interface is more colorful, but it does not respond to any operation by the user to be able to make the graphical interface receives a user's operation, it is necessary to add individual components event handling mechanism. in the event during processing, mainly related to three types of objects:
Event- event, the user interface operation is described in java language, in the form of classes, such as a key Event classes corresponding to the operation is KeyEvent.
The Event Source- event source, event occurrence place, usually the individual components, such as buttons the Button.
The Event handler- event handler to receive event to its target object and processed
, for example, if the user click the button object button, the button is the button event sources, but java runtime system will generate actionE ActionEvent class of objects, the object described some of the information at the click event, then the event handler objects java executed by the receiving system to pass over the object actionE event and the corresponding process.
Due to a variety of events that may occur on the same event source, java adopted authorization processing mechanism (Delegation Model), event source can put all of its own in the event that may occur are licensed to different event handlers to deal with, such as the Canvas objects on both mouse events may occur, keyboard events can also occur, the Canvas object can be delegated to an event handler to handle a mouse event, granting permission to the event handler to deal with two key events. sometimes called the event handler listeners, but also the main reason that the listeners listening for all types of events that occur on the event source, once the event is consistent with the type of their own to deal with the type of event, as soon as processing authorization model to handle events entrusted to outside processing entity for processing, implemented a mechanism for event sources and listeners separated. event handler (listener) is usually a class if you want to be able to handle certain types of events, it is necessary to achieve the opposite type of event interface type, for example, in example 5.9 was able to handle an ActionEvent ButtonHandler, because it implements The ActionListener ActionEvent event corresponding interface. Each class has an associated event corresponding interface
event source object and the event handler (event listeners) are separated as shown in Figure 5.2
make an inappropriate analogy, One says Lee, Mr. Lee may be many legal disputes occur, possibly civil legal disputes, it may be criminal legal disputes, so Lee can hire a lawyer, he can authorize the attorney in charge of the king to help him fight the lawsuit in civil law, while You may also authorize Mr. Zhang to help him fight the criminal law of the lawsuit. the process to hire a lawyer from Mr. Lee's point of view, is the authorization process, and from Mr. Wang and Mr. Zhang's point of view, once authorized, they have Mr. Lee is responsible for the time, "listening" with Lee, once a civil dispute, the king would soon lawyers to deal with, but once a criminal dispute, Mr. Zhang will be processed immediately. At this moment, Lee is the event source, Mr. Wang is an event handler, Mr. Zhang is another event handler, civil disputes and criminal disputes that different types of events.

Reproduced in: https: //www.cnblogs.com/521taobao/archive/2012/03/17/2402467.html

Guess you like

Origin blog.csdn.net/weixin_33850015/article/details/93355896