Java event

xiaoxiao2021-03-06  94

Event processing is one of the cores of the JavaBean architecture. Through the event handling mechanism, some components can be made as an event source, and an event that can be described in the environment or other component can be issued. Thus, different components can be combined in the construction tool, and the components communicate between the components, constitute an application. Conceptually, an event is a transmission mechanism that changes between "source objects" and "listener objects". There are many different purposes, such as mouse events, window boundaries, etc., and keyboard events, etc., for example, in Windows systems. In Java and JavaBean, it is defined a general, expandable event mechanism that provides a public framework for the definition and expansion of the model type and passing model, and is suitable for extensive applications. There is a high integration with Java language and the environment. The event can be described to capture and ignition. Other constructors can take some techniques to directly control events during design, as well as between event sources and event listeners. The event mechanism itself does not rely on complex development tools. In particular, it should also be that an event that can be generated by the specified object class can be found. It can be found that the specified object class can observe (listening) the event. Provide a regular registration mechanism that allows the relationship between the dynamic manipulation of the event source and event listener. No need other virtual machines and languages ​​can be implemented. Efficient event delivery between event sources and listeners. A neutral mapping of the Javabean event model and the related other component architectural event model can be completed. 3.2.1 Overview The overall structural diagram of the JavabEan event model is shown in Figure 3.3, which is mainly configured: the event from the event source to the listener is conducted by the Java method call to the target listener object. A clear Java method is defined accordingly for each of the clear events. These methods are centralized in the event listener (EventListener interface, this interface should inherit Java.util.EventListener. A class that implements some or all of the methods in an event listener interface is an event listener. With the occurrence of the event, the corresponding state is usually packaged in the event status object, which must inherit from Java.util.EventObject. Event status objects are transmitted to the listener method that should respond to the event as a single ginseng. The identity of the event source that issues a certain particular event is that the design format of the specified design is the registration method for event listeners and accepts references to the specified event listener interface instance. Sometimes, event listeners cannot directly implement event listener interface, or other additionalctions, it is necessary to insert an instance of an event adapter class between a source and other one or more listeners to establish them. Contact. 3.2.2 Event State Object The status information related to the event is generally packaged in an event status object, which is a subclass of java.util.EventObject. According to the design habits, the names of this event status object class should be ended at Event.

For example: public class MouseMovedExampleEvent extends java.util.EventObject {protected int x, y; / * create a mouse move event MouseMovedExampleEvent * / MouseMovedExampleEvent (java.awt.Component source, Point location) {super (source); x = location. X; Y = location.y;} / * Get mouse position * / public point getLocation () {Return New Point (x, y);}} 3.2.3 Event Loker Interface (EventListener Interface) and event monitors Due to Java The event model is based on method call, thus requiring a way to define and organize event handling methods. In javabean, event handling methods are defined in the EventListener interface of the Java.util.EventListener class, with the naming of the EventListener interface to end with Listener. Any class If you want to manipulate the method defined in the EventListener interface, you must perform this interface. This class is the event monitors. For example: / * Define a mouse mobile event object * / public class mouseMoveDexampleEvent Extends java.util.EventObject {// In this class contains status information related to the mouse mobile event ...} / * Defines the mouse movement Event's listener interface * / interface mousemovedexamplelistener extends java.util.EventListener {/ * Defines how the mouse mobile event listener should support the method * / void mouseMovent MME);} Only the method is defined in the interface Name, method of parameters and return value types. Such as: The specific implementation of the mousemoved method in the above interface is defined in the ArbitraryObject class below. Class ArbitraryObject Implements MousemovedexampleListener {Public Void MouseMoved (MousemovedexampleEvent MME) {...}} ArbitraryObject is the monitors of the MouseMoveDexampleEvent event. 3.2.4 Event monitors Registration and logout For various possible event listeners to register their own events into the appropriate event sources, establish the event stream between the source and event listeners, the event source must provide registration and logout for event listeners Methods.

This use process has been seen in the previous Bound property introduction, in practice, event listeners registration and logout To use standard design format: public void add ( listener; public void Remove < ListenerType> ( listener); for example: first, defines an event listener interfaces: public interface ModelChangedListener extends java.util.EventListener {void modelChanged (EventObject e);} Next define the event source class: public abstract class Model {private Vector listeners = new Vector (); // defines a storage array of event listener / * the above design format that is here below ModelChangedListener * / public synchronized void addModelChangedListener (ModelChangedListener mcl) {listeners. addElement (mcl);} // Register the listener into the array of listeners public synchronized void removeModelChangedListener (ModelChangedListener mcl) {listeners.removeElement (mcl); // the logout listener from the listeners} / * in front of two or more methods of The top crown is SYNCHRONIZED, because when running in a multi-threaded environment, there may be several objects at the same time to register and log out, using Synchronized to ensure that they are synchronized. Development tools or programmers use these two ways to create event streams between source and listeners * / protected void notifymodelchanged () {/ ** Event source uses this method to notify the listener ActOlChanged event * / Vector L; EventObject E = New EventObject (this); / * First, copy the listener to the L array, freeze the status of EventListeners to pass the event. This way to ensure that the corresponding method of the target listener that has received the event is not effective before the event is passed to all listeners. * / Synchronized (this) {l = (vector) listeners.clone ();} for (int i = 0; i

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

New Post(0)