Home >Common Problem >What behaviors can objects recognize and respond to?
The behavior that an object can recognize and respond to is an "event". Behaviors that an object can recognize and respond to are called events. Events are actions that an object can recognize and detect. When this action occurs on an object, its corresponding event will be triggered.
Java event summary
Event processing model
For GUI applications, event processing is essential, so we need to master the event processing model proficiently. For events we need to understand two terms: event source object and listener object. We can get a rough idea from the literal meaning. Let’s explain the system below:
The listener object is an instance of a class that implements a specific listener interface
The event source is an object that can register listener objects and send event objects
When an event occurs, the event source passes the event object to all registrations The listener
The listener object will use the information in the event object to decide how to respond to the event
First we will the listener object Register with the event source object so that when the event is triggered, the system can access the corresponding listener through the event source. As shown below:
#When the event source triggers an event, the system encapsulates the relevant information of the event into an event object of the corresponding type and sends it to the event source registered to the event source. Corresponding listener. As shown below:
#When the event object is sent to the listener, the system calls the corresponding event processing method of the listener to process the event, that is, to respond. As shown below:
#Note: There is a "many-to-many" relationship between the listener and the event source.
The hierarchy of event objects
The highest level in the event object is java.util.EventObject, the root class from which all event status objects will be derived. In addition to the classes inherited from the Object class, this class also has the getSource() method, whose function is to return the object where the Event originally occurred.
Except this class is in the util package, the others are in the java.awt, java.awt.event package or java.swing, java.swing.event package. It is worth noting that it does not mean the Swing control. Only use Swing events.
The AWTEvent class provides the getID() method to return the identifier of the event nature. For example, if a mouse event occurs, you can find out whether it was a click, drag, press, or other operation.
Description of several commonly used event classes:
EventObject: the super class of all event classes. The most important method - getSource(), returns the object that generated an event
AWTEvent: the super class of all AWT event classes. The most important method - getID(), returns the ID number of an event. The event ID is an integer, which specifies the type of event, such as a button event or a mouse click event
The hierarchy of listener objects
The listener object is an instance of a class that implements a specific listener interface, then the listener interface That's what we're concerned about. The top-level interface in the listener interface is java.util.EventListener. This interface is the mark interface that all event listener interfaces must extend. What is surprising is that this interface is completely empty. There is no definition of any abstract method in it. When I check the source code, it is empty!The event listener interface naming method is: XXListener, and in java, these interfaces have already been defined. Used to be implemented, it defines the event handler (that is, the event handling method prototype, this method needs to be reimplemented).
For example: ActionListener interface, MouseListener interface, WindowListener interface, KeyListener interface, ItemListener interface, MouseMotionListener interface, FocusListener interface, ComponentListener interface, etc.
Event source
Events are initially generated by an event source. The event source can be a GUI component Java Bean or an object capable of generating events. In the case of a GUI component, the event source or a peer of the component (for Abstract Window Toolkit [awt] GUI components) say) or the component itself (for Swing components).
In java, what kind of events each component will generate has been defined. In other words, for any event, it is already determined which components can produce it.
Events
AWT divides events into low-level events and semantic events.
Semantic events are events that represent user actions.
Low-level events are the events that form those events.
Common semantic event classes in the java.awt.event package:
ActionEvent (corresponding to button click, menu selection, selection list item or ENTER in the text box)
AdjustmentEvent (the user adjusts the scroll bar)
ItemEvent (the user selects an item from a check box or list box Item)
Five commonly used low-level event classes:
KeyEvent (a key is pressed or released)
MouseEvent (mouse button is pressed, released, moved or dragged)
MouseWheelEvent (mouse wheel is turned)
FocusEvent (a component gains or loses focus)
WindowEvent (the window state is changed)
java.awt.event package Commonly used event adapter classes defined in include the following:
1. ComponentAdapter(Component Adapter)
2. ContainerAdapter(Container Adapter)
3. FocusAdapter(Focus Adapter)
4. KeyAdapter(Keyboard Adapter)
5. MouseAdapter(Mouse Adapter)
6. MouseMotionAdapter(Mouse Motion Adapter)
7. WindowAdapter(Window Adapter)
Corresponding relationship between event source and listener
The following figure is the corresponding relationship diagram in actual Java development:
Common event objects and listener interface comparison table:
Listener interface | Event source |
---|---|
AbstractButton |
JcomboBox JTextField Timer |
JscrollBar | |
AbstractButton |
JComboBox |
Component | |
Component | |
Component | |
Component | |
Component | |
Window | |
Window | |
Window |
The above is the detailed content of What behaviors can objects recognize and respond to?. For more information, please follow other related articles on the PHP Chinese website!