AWT GUI Design Notes (2)

xiaoxiao2021-03-06  41

.....

Join

4.3: EVENTADAPTER ---- Event Adapter

The Java language provides an Adapter class for some Listener interfaces. You can rewrite the need for the Adapter class corresponding to the inheritance, no need to implement it. The event adapter provides us with a simple means of realizing the listener, which can shorten the program code. However, due to the single inheritance mechanism of Java, the event adapter cannot be employed when multiple listeners or such a parent class is needed.

1. Event adapter - EventAdapter

2. Implement event processing with internal class

3. Anonymous class

The mouse adapter is used in the following example:

Import java.awt. *;

Import java.awt.event. *;

Public class mouseclickhandler extends mouseadaper {

Public void mouseclicked (MouseEvent E)

// only realize the needs

{...}

}

The event adapter classes defined in the java.awt.event package include the following: 1. ComponentAdapter 2. ContaineraDapter 3. FocusAdapter 4. KeyAdapter 5. MouseAdapter 6. MouseMotionAdapter 7. Windowadapter (Window Adapter) Using Inner Class Implementation Event Processing Inner Class (Inner Class) is a class defined in another class, the main reason for using internal classes is due to: ◇ An internal class object can access member methods of external classes And variables, including private members. ◇ When implementing an event listener, an internal class is used, and anonymous class programming is very easy to implement its function. ◇ Write an event driver, which is very convenient. Therefore, the place where the internal class can be applied is often in an event processing mechanism of AWT. import java.awt *;. import java.awt.event *;. public class InnerClass {private Frame f; private TextField tf; public InnerClass () {f = new Frame ( "Inner classes example"); tf = new TextField ( 30);} public voidi launchframe () {label label = new label ("Click and Drag the Mouse"); f.add (label, borderLayout.North); f.add (tf, borderlayout.South); f.addmouseMotionListener (New mymouseMotionListener ()); / * Parameter is internal class object * / f.setsize (300, 200); f.setVisible (TRUE);}

Class MyMouseMotionListener Extends MouseMotionAdapter {/ * Internal Class Start * / Public Void MouseDragged (MouseEvent E) {String S = "Mouse Dragging: x =" E.GETX () "y =" E.Gety (); TF. SetText (s);}}; public static void main (string args []) {innerclass obj = new innerclass (); obj.launchframe ();}} // internal class end}

Anonymous Class When an internal class's class sound name is just once when you create such objects, and the new categories you want to inherit in an existing parent class or implement an interface, you can think about anonymous classes. Since the anonymous class is not named, it does not have a constructor, which needs to display a construction method of a non-arranging parent class, and rewrite the method of the parent class. The so-called anonymity is that the type of even name is not, but only the constructor of the parent class is called. import java.awt *;. import java.awt.event *;. public class AnonymousClass {private Frame f; private TextField tf; public AnonymousClass () {f = new Frame ( "Inner classes example"); tf = new TextField ( 30);} public void launchframe () {label label = new label ("Click and Drag the Mouse"); F.Add (label, borderLayout.North); f.add (tf, borderlayout.South); f.addmouseMotionListener (New MouseMotionAdapter () {// Anonymous class starts public void mousedragged (MouseEvent E) {string s = "mouse Dragging: x =" E.GETX () "y =" E.Gety (); tf.setText (s);}}); // Anonymous class ends F.setSize (300, 200); f.setVisible (true);} public static void main (string args []) {anonymousclass obj = new anonymousclass (); obj.launchframe ();}} 5. Component Library 5.1. Button button is the most common component, its constructor is: Button B = New Button ("quit"); When the button is clicked, an ActionEvent event will be generated. The ActionListener interface is required to monitor and process events. ActionEvent's object calls the getActionCommand () method to get the ID name of the button, the default button name is label. Use setActionCommand () to set the component identifier for the button.

5.2. Checkbox Construction Methods are as follows: setLayout (New GridLayout (3,1)); Add (New Checkbox ("One", NULL, TRUE); Add (New Checkbox); add ("Two"); add (New CheckBox ("three"); check box with itemlistener to get the ItemEvent event, get the current state with getStateChange () when the check box status changes. Use GetItem () to get the string object of the modified check box. 5.3 CheckboxGroups Use the checkbox group to implement the functionality of the radio box. The method is as follows: setLayout (New GridLayout (3, 1)); CheckboxGroup CBG = New CheckboxGroup (); add (New Checkbox ("One", CBG, True); Add (New Checkbox ("Two", CBG, FALSE) Ad ("Three", CBG, FALSE); 5.4. The drop-down menu (Choice) drop-down menu can only select one of them each time, it can save display space for a lot of options. Choice colorchooser = new choice (); colorchooser.add ("green"); colorchooser.add ("red"); colorchooser.add ("blue"); choice uses the itemListener interface to monitor 5.5. Canvas An application must inherit The Canvas class can get useful features, such as creating a custom component. If you want to complete some graphic processing on the canvas, the PAINT () method in the Canvas class must be rewritten. The Canvas component monitors a variety of mice, keyboard events. When you enter characters in the Canvas component, you must first call the RequestFocus () method. 5.6. Single-line text input area can only display a line, and an ActionEvent event occurs when the Enter key is pressed, and the event can be processed accordingly by the actionPerformed () method in the ActionListener. You can use the setDitable (Boolean) method to read-only properties. Single line text input area constructor is as follows: TextField TF1, TF2, TF3, TF4: TF1 = New TextField (); TF2 = New TextField (", 20); // The display area is 20 columns TF3 = New TextField (" Hello! "); // Display TF4 = New TextField (" Hello! ", 30); // The initial text is Hello !, the display area is 30 columns 7. Text Edge (TextArea) Textarea can display more rows more Column text. Using the setEditable (Boolean) method, you can set it to read only. A horizontal or vertical scroll bar can be displayed in TextArea. To determine if the text is entered, you can set a button next to TextArea, and the input text is processed by clicking the generated ACTIONEvent.

8. List (list) list provides multiple text options, the list supports scroll bar, you can browse multiple list lst = new list (4, false); // The two parameters represent the number of lines displayed, whether to allow multiple LST.ADD ("VENUS"); LST.Add ("Earth"); LST.ADD ("JavaSoft"); Lst.Add ("Mars"); cnt.add (LST); 9. Frame (frame) Frame is a top window that displays a title, reset size. When Frame is turned off, a WindowEvent event will be generated, and Frame cannot directly listen to the keyboard input event. 10. Dialog (Dialog) It is a subclass of a Window class. The difference between dialogs and general windows is that it relies on other windows. The dialog is divided into two types of non-modal and modal. 11. File dialog (FileDialog) When the user wants to open or store files, use the File dialog box. The main code is as follows: FileDialog D = New FileDialog (PARENTFR, "FileDialog"); D.SetVisible (TRUE); string filename = d.GetFile (); 12. Menu (MENU) Can't add the menu to a location of the container It is also impossible to control it using the layout manager. The menu can only be added? Quot; menu container "(menubar). 13. MenuBar can only be added to the Frame object as the foundation of the entire menu tree. Frame fr = new frame (" menubar "); menubar MB = New Menubar (); fr.setmenubar (MB); fr.setsize (150,100); fr.setVisible (TRUE); 14. Menu drop-down menu. It can be added to Menubar or other MENU. Frame fr = new frame (" MenuBar "); MenuBar MB = new menubar (); fr.setmenubar (MB); MENU m1 = new menu (" file "); menu m2 = new menu (" edit "); Menu m3 = new menu (" Help " ); Mb.Add (m1); Mb.Add (m2); Mb.SetHelpMenu (m3); fr.setsize (200, 200); fr.setVisible (TRUE); MenuItem MenuItem is "Leaf Node" in the menu tree. MeNuitem Usually added to a MENU. ActionListener can be added to the MenuItem object so that it can complete the corresponding operation.

Menu m1 = new menu ("file"); menuitem mi1 = new menuItem ("save"); menuitem mi2 = new menuItem ("LOAD"); MenuItem Mi3 = New Menuitem ("quit"); m1.add (mi1) M1.Add (mi2); m1.addseparator (); m1.add (mi3); MenuBar and MENU do not have to register the listener, just need to add a listener ActionListener to MenuItem to complete the corresponding operation. The corresponding relationship between each component is listed in the table below, and "" indicates that the component can be registered. Search interface

ACT

Adj

CMP

CNT

FOC

ITM

Key

Mou

Mm

TEXT

Win

Button

Canvas

Checkbox

CheckBoxMenuItem

Choice

Component

Container

Dialog

Frame

Label

List

Menuitem

Panel

Scrollbar

Scrollpane

Textarea

Textfield

Window

Act = ActionListener Adj = AdjustmentListener Cmp = ComponentListener Cnt = ConatainerListener Foc = FocusListener Itm = ItemListener Key = KeyListener Mou = MouseListener MM = MouseMotionListener Text = TextListener Win = WindowListener

转载请注明原文地址:https://www.9cbs.com/read-53274.html

New Post(0)