A.T.AWT Introduction Window Toolkit APT (Abstract Window Toolkit) is an API to establish a graphical user interface toolset provided by Java programs, AWT available in Java's Applet and Applications. It supports graphical user interface programming features: user interface components; event processing models; graphics and image tools, including shapes, color, and font classes; layout managers, the flexible window layout and distinguish between the size and screen of the specific window The rate is independent; the data transfer class can be cut and pasted through the clipboard of the local platform. 1.1: Java.awt package Java.awT package provides the class and excuses used by the GUI design. The relationship between the main classes is listed in the following figure. Java.awt design mainly includes three concepts: Component Component Container Container layout Manager LayoutManager 1.2: Java.awt.comPonent Component class package universal method and properties, such as graphic component objects, size, display location, BlackColor / Forecolor, boundary, visibility, etc. Therefore, many component classes have inherited. The members of the Component class and the member variables, the corresponding member methods include: getComponentat (int X, int y) getFont () getForeGround () getName () getSize () Paint (GRAPHICS G) Repaint () Update () setvisible (Boolean B) ) Setsize (String Name), etc. 1.3: Container container is also a class, which is actually a subclass of Component, so the container itself is also a component, with all the nature of the component, but its main function is Accommodate other components and containers. 1.4: LayoutManager has a layout manager for each container. When the container needs to position or determine the size size of a component, its corresponding layout manager is called. In order to make our generated graphical user interface with a good platform independent, in the Java language, the layout manager is provided to manage the layout of the component in the container without using the way the component location and size. To schedule the location and size of the components in the program, you should pay attention to the following two points: 1. The layout manager in the container is responsible for the size and position of each component, so the user cannot set these attributes in this case. If you try to use the setlocation () (), setsize (), setbounds (), etc. provided using the Java language, will be overwritten by the Layout Manager. 2. If the user does need to set the component size or position personally, the layout manager of the container should be canceled, the method is: setLayout (null); two.constainer container java.awt.container is a subclass of Component, one container can accommodate multiple Components and make them a whole. The container can simplify the design of the graphical interface to arrange the interface in an overall structure. All containers can add components to the container via the add () method. There are three types of containers: Window, Panel, Scrollpane, commonly used panel, frame, applet.
2.1 Frame
The appearance of the frame is like the windows we are usually seen under the Windows system, with title, border, menu, size, and so on. After instantiation of each frame, it is not sized and invisible, so setsize () must be called to set the size, call SetVisible (TRUE) to set the window to see. AWT is a graphical system that calls the platform during the actual operation, so the same AWT program is different in different operating system platforms. For example, running under Windows, the displayed window is a Windows style window; while running in UNIX, the Unix style window is displayed. Example 1 Import java.awt. *; Public class myframe extends frame {public static void main (string args []) {myframe fr = new myframe ("Hello Out there!"); // Configuration method fr.setsize (200,200) ; // Set the size of the frame, default is (0,0) fr.setbackground (color.red); // Setting the background of the frame, the default is red fr.setVisible (TRUE); // Setting Frame is visible, Default is invisible} public myframe (string str) {super (STR); // Call the construction method of the parent class}} 2.2: Panel Example 2Import java.awt. *; Public class paneltest1 extends frame {public paneltest1 (String Str ) {Super (str); swreen static void main (string args []) {paneltest1 fr = new paneltest1 ("panel test"); Panel Pan = new panel (); fr.setsize (200, 200); fr.setBackground Color.Red; fr.setlayout (null); pan.setsize (100, 100); pan.setbackground (color.yellow); fr.d (PAN); fr.setvisible (true);}} 3. KeyoutManagerJava In order to achieve cross The characteristics of the platform and the dynamic layout effect, Java arranges all components in the container to a "layout manager" responsible for management, such as: arrangement order, size, location, how to change the component after window movement or adjustment, etc. Features Authorization to manage the corresponding container layout manager, different layout managers use different algorithms and strategies, The library can be determined by selecting a different layout manager. The layout manager mainly includes: FlowLayout, BorderLayout, GridLayout, Cardlayout, GridbagLayout3.1 FlowLayoutflowLayout is the default layout manager of Panel, applet. The lap of its components is placed from left to right. If the container is wide enough, the first component is added to the left side of the first row in the container, and the subsequent components are added to the right side of the previous component. If the current row is already placed, it is placed to the leftmost left side of the next row.
The structure is mainly below: FlowLayout (FlowLayout.right, 20, 40); / * The first parameter represents the alignment of the component, and the location in this line is aligned in this line, and the right is aligned or the left is left. Two parameters are lateral intervals between components, and the third parameter is the longitudinal interval between components, and the unit is pixel. * / FlowLayout (FlowLayout.Left); // 居 齐 齐 对 象 象 象 象 象 象 象 象 象 象 象 象 象 象 象 象 象 象 象 象 象 象 象; 象;; Value 5 pixel components initialization size is generally determined according to the body of the components; import java.awt. *; Public class exgui {public static void main (string args []) {frame f = new frame (); f. SetLayout (New flowLayout ()); Button Button1 = New Button ("Okdfdsfasd"); Button Button2 = New Button ("Open"); Button Button3 = New Button ("Close"); f.Add (Button1); f. Add (Button2); F.Add (Button3); F.setSize (300, 100); F.SetVisible (TRUE);}} 3.2: BorderLayoutBorderLayout is the default layout manager for Window, Frame, and Dialog. BorderLayout Layout Manager divides the container into 5 areas: North, South, East, West and Center, each area can only place a component. The location and size of each region are shown below: When using borderLayout, if the size of the container changes, the variation law is: the relative position of the component does not change, and the size changes. For example, the container becomes higher, then the North, the South region is constant, WEST, CENTER, EAST region becomes high; if the container is widened, WEST, EAST area is unchanged, North, Center, and South area are widened. Not necessarily all of the regions, if there is no components around the area (West, EAST, North, South District), it is replenished by the Center area, but if there is no component, if there is no component, it will be blank. 3.3: GridLayout makes the container Each component is a mesh-like layout, averages the space of the container.
Public class exgui {public static void main (string args []) {frame f = new frame (); //f.setLayout (New flowLayout ()); //f.setLayout (New borderLayout ()); f.setlayout (New GridLayout (3, 2)); Button Button1 = New Button ("okdfdsfasd"); Button Button2 = New Button ("open"); Button Button3 = New Button ("Close"); f.Add (Button1); F.Add (Button2); F.Add (Button3); F.Add ("Button4")); / * F.Add ("North", Button1); F.Add ("South", Button2) ; * / F.setsize (300, 100); f.setvisible (true);
} 3.4: CardlayoutCardLayout Layout Manager can help users handle two to more members to share the same display space, which divides the container into many layers, accounting space for each layer occupies the size of the entire container, but only allows a component to be placed. Of course, each layer can be used to realize complex user interfaces. Four. AWT Event Process Model To allow the graphical interface to receive the user's operation, you must add an event processing mechanism to each component. During the event process, mainly involve three types of objects: event-event, user-purpose descriptions on the Java language, with class, such as the keyboard operation, the event class is Keyevent. Event Source- Event source, events occurring, usually, each component, such as buttton buttton. Event Handler-Event handler, receives an event object and processes it. For example, if the user clicks on the button object button with the mouse, the button button is the event source, while the Java runtime system generates an object Actione of the ActionEvent class, which describes some information when the clicked event occurs, then Event handler objects will receive event object Actione passed by the Java runtime system and perform corresponding processing. Since multiple events may occur due to the same event source, Java has taken the execution model, and the event source can handle the events that have the possible events thereof separately to different event handlers. For example, in the Canvas object, there may be a mouse event or a keyboard event. The Canvas object can authorize the event handler to process the mouse event while authorizing the event handler two to handle the keyboard event. Sometimes the event handler is also called a listener. The main reason is that the listener time monitors all event types on the event source, once the event type is consistent with the type of event handles it, it will be handled immediately. The authorization model will delegate the process of the event to the external processing entity to achieve a mechanism that separates the event source and the listener. Event handler (listener) is usually a class, and if you want to process some type of event, you must implement an interface with the event type. For example, Example 5.9 The class BUTTONHANDLER can handle an ActionEvent event because it implements interface ActionListener corresponding to the ActionEvent event. Each event class has an interface corresponding to it. Separate event source objects and event processors (event listeners). The general method of using the authorization processing model is summarized as shown in Figure 5.2 summarizes: 1. For some type of event XXXEvent, you must define the corresponding event listener class, which needs to implement the interface XXXListener; 2. After the event source is instantiated, authorization must be licensed, registering the listener of the class event, using the AddXxxListener method to register the listener.
Example: TestButton.javaImport java.awt. *; Import java.awt.event. *; Public class testbutton {public static void main (string [] args) {frame f = "Button Test"); button b = New Button ("Press Me"); B.AddActionListener (New ButtonHandler ()); / * Registering the listener for authorization, the parameter of this method is the event handler object, * The type of event to be processed can be seen from the method name For example, the method is authorized to process ActionEvent because the method name is AddActionListener. * / F.setLayout (new flowLayout (); f.Add (b); f.setsize (200, 100); f.setVisible (true);}} Class ButtonHandler Implements ActionListener {// Implement Interface ActionListener to do event ActionEvent The ActionEvent Event Object generated by the processor public void actionPerformed (ActionEvent E) // is transmitted as parameters to this method
{System.out.println ("Action Occured));} // This interface has only one method, so the event will automatically call this method, and the code is written in a method.
} Each type of event has a corresponding event listener, the listener is an interface, defined according to the action. 4.1: KeyEventjava.lang.Object java.util.EventObject java.awt.AWTEvent java.awt.event.ComponentEvent java.awt.event.InputEvent java.awt.event.KeyEvent and keyboard events KeyEvent corresponding interface is: public interface KeyListener extends EventListener {public void keyPressed (KeyEvent ev); public void keyReleased (KeyEvent ev); public void keyTyped (KeyEvent ev);} when the keyboard is just pressed down, will be called the keyPressed () method performs, when the keyboard is lifted, The keyreleased () method will be called, and when the keyboard is tap once, the keytyped () method is called. 4.2 WindowEvent window event interface: public interface WindowListener extends EventListener {public void windowClosing (WindowEvent e); // exit window of the statement written in the present method public void windowOpened (WindowEvent e); // call the public void windowIconified when the window opens ( WINDOWEVENT E); // Call Public Void WINDOWDEICONIFIED (WindowEvent E) when the window is invoked when you call public void windowclosed (WindowEvent E); // WindowEvent E); / / WindowEvent E); // / WindowEvent E) When you call public void window Download (WindowEvent E); // Window Inactive Call} 4.3: AWT Event and Its Corresponding Listener Interface The following table lists all AWT events and its corresponding listener interface, a total of 10 categories Event, 11 interfaces.
Event category
Description
Interface name
method
Activation component ActionEvent ActionListener actionPerformed (ActionEvent) ItemEvent chose certain items ItemListener itemStateChanged (ItemEvent) MouseEvent mouse movement MouseMotionListener mouseDragged (MouseEvent) mouseMoved (MouseEvent) mouse click MouseListener mousePressed (MouseEvent) mouseReleased (MouseEvent) mouseEntered (MouseEvent) mouseExited (MouseEvent ) mouseClicked (MouseEvent) KeyEvent keyboard KeyListener keyPressed (KeyEvent) keyReleased (KeyEvent) keyTyped (KeyEvent) FocusEvent component receives or loses focus FocusListener focusGained (FocusEvent) focusLost (FocusEvent) AdjustmentEvent moving the scroll bar components such AdjustmentListener adjustmentValueChanged (AdjustmentEvent) ComponentEvent zoom display object is moved or hidden ComponentListener componentMoved (ComponentEvent) componentHidden (ComponentEvent) componentResized (ComponentEvent) componentShown (ComponentEvent) WindowEvent window receives a window-level event WindowListener windowClosing (WindowEvent) windowOpened (WindowEvent) windowIconified (WindowEvent) windowDeiconified (WindowEvent) windowClosed (WindowEvent WINDOWACTIVATED (WINDOWEVENT) WINDOWDEACTIVATED (W InDoWevent) ContaineRevent container Add Delete Components ContaNerListener Componentadded (Containerevent) TextEvent Text Field or text area changes TextListener TextValueChanged (TextEvent) 1. You can declare multiple interfaces, and the interfaces are separated from commas. ... Implements MouseMotionListener, MouseListener, Windowlistener; 2. Multiple events that occur on an event source can be listened by the same object: f.addmouseMotionListener (this); f.addmouselistener (this); f.addwindowlistener (this); multiple events occurred on object F will be A listener receives and processes. 3. Event handlers and event sources are in the same class.