One, user events
The user's actions in the program interface, such as clicking the mouse or entering characters, are called user events .
The program responds to these user events, called event handling.
Second, the event processing model
1. Event type--listener interface
The system classifies these user events into various event types .
The system provides a listener interface for each event type, and the interface contains methods that specify the specification to accept and process such events.
2. Listener interface--event listener
In order to receive and process an event type, the component must register the appropriate event handler, known as an event listener (Listener, also known as a listener).
There are two tectonic conditions for the listener:
One is the listener interface that must implement the corresponding event type.
The second is to implement the method of the response event specified in the interface.
For example, to handle the ActionEvent event on a button, you need to define a listener class that implements the ActionListener interface.
Public interface ActionListener extends eventlistener{
public void actionperformed (ActionEvent e);
}
Only one method, actionperformed (), is defined in the interface, which is called when the ActionEvent event occurs.
Actionperformed (), is a callback function . The function that is called by the system.
The difference between a direct callback is that the calling body is not the first person's program body. Actionperformed () is a pre-set function, but the setting is not called at that time, but instead waits for the actionevent to occur, and the system calls this function as a parameter.
BTW, the system calls these callbacks by polling , that is, polling the thread pool queue to find the callback function that listens for this system event.
We go back to this listener event class. Each component has several methods, such as Addxxxlistener (Xxxlistener), through which you can register an event listener for a component.
This processing event mechanism is called the Delegate event processing mechanism .
In summary, the event is sent directly to the component that generated the event, and the component needs to register one or more listeners. The Listener class contains an event handler that receives and processes the event. An event is an object that reports only to registered listeners.
Third, GUI event processing steps
1. Introduction of Java.awt.event package in the program
Import java.awt.event.
2. Register the Listener event program for the desired event source object.
The event source object. Addxxxlistener (Xxxlistener);
3. Implement the appropriate method. If the listener interface contains multiple methods, all methods must be implemented.
The listener for an event can be defined in a separate class or in a component class.
Java Event Processing Model