Java event handling mechanism (custom event) example details
Java event handling mechanism
The participants of the event mechanism in java have three roles:
1. Event object: event status object, which is used in the corresponding methods of listener. As a parameter, it generally exists in the methods of listener
2. Event source: specific event source. For example, if you click a button, the button is the event source. To make the button respond to some events, you need to register a specific listener.
3. Event listener: define an explicit Java method for the occurrence of each explicit event. These methods are collectively defined in the event listener interface, which inherits java.util.eventlistener. The class that implements some or all of the methods in the event listener interface is the event listener.
With the occurrence of events, the corresponding state is usually encapsulated in the event state object, which must inherit from Java util. EventObject。 The event state object is passed as a single parameter to the listener method that responds to the event. The identification of the event source that sends a specific event is to define a registration method for the event listener in accordance with the specified design format, and accept a reference to the specified event listener interface instance.
Specifically, for the monitored event class, when it listens to the generation of event object, it will call the corresponding method for processing.
First look at the event package provided by JDK:
Public interface EventListener: all event listener interfaces must be extended tag interfaces.
public class EventObject extends Object implements Serializable
The root class from which all event state objects will derive. All events refer to the object "source" during construction. Logically, it is considered that the object is the object related to the event that originally occurred.
(1) Create a doorevent class through the doorevent.java file, which inherits eventobject.
(2) Define a new event listening interface, which inherits from EventListener; this interface contains handlers for doorevent events:
Through the above interface, we redefine event listening classes, which specifically implement the listening function and event processing function.
(3) Create an event source class through doormanager.java. It uses a collection listeners object to store all event listener objects through adddoorlistener (..) Such a method. notifyListeners(..) It is a method to trigger an event. It is used to inform the system that an event has occurred. You can call the corresponding handler function.
(4) Well, finally, write a test program to test our custom events. This program should not be difficult to understand:)
Run doormain
Door 1 opens, door 2 opens, and the corridor lights are turned on at the same time
I've come in
Door 1 is closed, door 2 is closed, and the lights in the corridor are turned off at the same time
Thank you for reading, hope to help you, thank you for your support to this site!