1. Event model
Throughout the event triggering and the corresponding process, the main concerns about the 3 class of objects
(1) Event Source: GUI objects that cause time, such as various components (Button,label,textfield), container components (Frame,panel,scrallpane, etc.)
(2) Event class: The event class is thrown into a class of GUI time encapsulation, such as mouse events MouseEvent
(3) event handler: Receive event class
1.1 Event classes
Low-level events
ComponentEvent (Component event: Change in component size, moving)
ContainerEvent (Container event: component increment, move)
WindowEvent (Window event: Close window, window closed, icon)
FocusEvent (Focus event: Acquisition and loss of focus)
KeyEvent (keyboard event: Key pressed, released)
MouseEvent (mouse events: mouse click, move)
Advanced Events (semantic events)
ActionEvent (Action event: Button pressed, press ENTER in TextField)
AdjustmentEvent (Adjust event: Move the slider on the scroll bar to adjust the value)
ItemEvent (Project event: Select Project, do not select "Project Change")
TextEvent (Text event, text object change)
1.2 Event handler
The method that handles the component triggering event becomes the event handler, and the event handler must implement an event listener interface to receive the corresponding event class, each of which receives the corresponding event class with the event handler of the corresponding event listener interface implementation interface.
1.3 Registering the event handler
Registering the component in the component raises the event handler for the time class, such as a button's click event that can be encapsulated with the Behavior event class (ActionEvent) or MouseEvent. To register the processor for the ActionEvent event in the button component, use the addActionListener method:
Button A = New button ("a");
A.addactionlistener (New Actionhandler ());
Common event handler Registration methods:
(1) addActionListener () for components with behavior actions such as button and TextField
(2) Addadjustmentlistener () can be used for components with scrolling operations such as ScrollBar
(3) Addfocuslistener () available for components with focus operation
(4) Additemlistener () can be used for components with item selection operations such as CheckBox combobox
(5) Addkeylistener () can be used for components with keyboard input operations
(6) Addmouselistener () can be used for components with mouse action
(7) Addwindowlistener () components that can be used for form operations
All AWT events and their corresponding listener interfaces, total 10 types of events, 11 interfaces
Event Category |
Description information |
Interface Name |
Method |
ActionEvent |
Activating components |
ActionListener |
Actionperformed (ActionEvent) |
ItemEvent |
Some items have been selected |
ItemListener |
Itemstatechanged (ItemEvent) |
MouseEvent |
Mouse movement |
Mousemotionlistener |
Mousedragged (MouseEvent) Mousemoved (MouseEvent) |
Mouse click and so on |
MouseListener |
Mousepressed (MouseEvent) Mousereleased (MouseEvent) Mouseentered (MouseEvent) Mouseexited (MouseEvent) Mouseclicked (MouseEvent) |
KeyEvent |
Keyboard input |
KeyListener |
Keypressed (KeyEvent) Keyreleased (KeyEvent) Keytyped (KeyEvent) |
FocusEvent |
component receives or loses focus |
Focuslistener |
Focusgained (FocusEvent) Focuslost (FocusEvent) |
AdjustmentEvent |
Moved a component such as a scroll bar |
Adjustmentlistener |
Adjustmentvaluechanged (AdjustmentEvent) |
ComponentEvent |
Object Move zoom display hidden etc. |
Componentlistener |
Componentmoved (ComponentEvent) Componenthidden (ComponentEvent) Componentresized (ComponentEvent) Componentshown (ComponentEvent) |
WindowEvent |
window receives window-level events |
WindowListener |
Windowclosing (WindowEvent) WindowOpened (WindowEvent) Windowiconified (WindowEvent) Windowdeiconified (WindowEvent) WindowClosed (WindowEvent) WindowActivated (WindowEvent) Windowdeactivated (WindowEvent) |
ContainerEvent |
Add components removed from container |
ContainerListener |
ComponentAdded (ContainerEvent) Componentremoved (ContainerEvent) |
TextEvent |
The text field or text area has changed |
TextListener |
Textvaluechanged (TextEvent) |
|
Java Learning note--AWT event handling