JavaBeans program development from getting started

xiaoxiao2021-03-06  42

JavaBeans program development from getting started

JavaBeans's properties JavaBeans's properties with the properties referred to in the general Java program, or the properties of objects in object-oriented programming languages ​​are a concept, and the specific embodiment in the program is the variables in the class. In JavaBeans design, subdivision is divided into four categories according to different properties: Simple, INDEX, Bound and constrained properties. 1. Simple property A simple property represents a variable accompanying a pair of GET / SET methods (C language or functions called "method" in the Java program). Attribute name and the GET / SET method name associated with this property. For example: If there is a setx and getx method, you have a property named "X". If there is a method name ISX, it usually fails to "X" is a Boolean property (ie the value of X is True or false). For example, in this program:

Public class aldenen1 extends can (string ourstring = "hello"; // Attribute name is ourstring, type is string public aldenen1 () {// Alden1 () is the constructor of Alden1, the meaning of constructor in C , the same setBackground Color.red; setForeground (color.blue);} / * "set" attribute * / public void setString (String newstring) {oorstring = news;} / * "get" attribute * / public string getString () {Return Ourstring ;}} 2. Indexed property A indexed property represents an array value. Use the SET / GET method corresponding to this property to get the value in the array. This property can also set or achieve the value of the entire array. example:

Public class aldenen2 extends canvas {int [] DataSet = {1, 2, 3, 4, 5, 6}; // DataSet is a indexed property public aldenen2 () {setBackground (color.red); setForeground (color.blue) } / * Set the entire array * / public void setDataSet (int [] x) {dataset = x;} / * Set a single element value in the array * / public void setDataSet (int index, int x) {dataset [index] = X;} / * acquire the entire array value * / public int [] getDataSet () {return dataset;} / * acquired the specified element value in the array * / public int getDataSet (int x) {return dataset [x];} } 3. Bound property A bound property is to inform other objects when the value of the property changes. When each attribute value changes, this property is ignited to a PropertyChange event (in the Java program, the event is also an object). The attribute name, the original value of the attribute, and the new value of the attribute change are encapsulated. This event is to other Beans, as for the beans that receive events should be defined by themselves. When the pushbutton's Background property is bind with the Dialog's Background property, if the PushButton's Background property changes, the Dialog's Background property also changes. example:

public class alden3 extends Canvas {String ourString = "Hello"; // ourString bound property is a private PropertyChangeSupport changes = new PropertyChangeSupport (this); / ** Note: Java is a pure object-oriented language, if you want some way then It must be indicated in which object is used, and the operation of the ignition event is performed in the following program, which is used in the PropertyChangeSupport class. So the above declaration and instantiate a Changes object, and will use the Changes's FirePropertyChange method to ignit the property change event. * / Public void setString (string newString) {String oldString = ourString; ourString = newString; / * ourString attribute value has changed, then followed by firing property change event * / changes.firePropertyChange ( "ourString", oldString, newString); } Public string getString () {return ourstring;} / ** The following code is used for development tools. We can't predict that Alden3 will become an application with which other Beans is unable to predict which other components are related to this change when Alden3's opstring property changes, so Alden3 beans should reserve some interfaces to development tools, development Tools use these interfaces to mount other JavaBeans objects with Alden3. * / Public void addPropertyChangeListener (PropertyChangeLisener l) {changes.addPropertyChangeListener (l);} public void removePropertyChangeListener (PropertyChangeListener l) {changes.removePropertyChangeListener (l);} by the above code, the development tools changes addPropertyChangeListener method call, the other JavaBeans In the listener queue 1 of the ourstring property, L is a vector array that stores any Java objects. The development tool can also use the Changes's RemovePropertyChangeListener method to log out of the specified object from the L, so that the change of the Oudtring property of Alden3 is no longer related to this object. Of course, when the programmer is handwritten, these two methods can also be called directly to hill other Java objects with Alden3. 4. constrained property A JavaBeans constrained property means that when the value of this property is changed, the other Java object that has established some of the other Java objects that a connection that has been connected to the attribute value is changed. The listener of the constrained property prevents the value of this attribute value by throwing PropertyveToException. Example: The constrained attribute in the following program is pricenetics.

public class JellyBeans extends Canvas {private PropertyChangeSupport changes = new PropertyChangeSupport (this); private VetoableChangeSupport Vetos = new VetoableChangeSupport (this); / * aforementioned changes the same manner as in Example Vetos using VetoableChangeSupport object, preventing PriceInCents specified conditions down Value changes. * / ...... Public void setPriceinCents (int newpriceincents) throws propertyvetoException {/ * Method name The role of THROWS PropertyveToexception is to throw exceptions when there are other Java objects to veto the change of PriceNCents. * / / * To save the original property value * / int oldPriceInCents = ourPriceInCents; / ** ignition properties change veto event * / vetos.fireVetoableChange ( "priceInCents", new Integer (OldPriceInCents), new Integer (newPriceInCents)); / * * If there is any other object veto of the change of PriceINCENTS, the program throws an exception, no longer continues to perform the following two statements, the method ends. If no other objects rejected priceInCents change, then the following code ourPriceIncents assign a new value, and the ignition property change event * / ourPriceInCents = newPriceInCents; changes.firePropertyChange ( "priceInCents", new Integer (oldPriceInCents), new Integer (newPriceInCents ));} / ** is the same as the aforementioned Changes, and the interface is reserved for the PriceIncents property, so that other objects can be registered into the PriceINCENTS vetoing listener queue, or log out of the public void addvetoablechangelistener (VetoAbleChangelistener L) {Vetos .addvetoableChangeListener (L); Public Void RemoveToableVhangeListener (VetoableChangeListener L) {Vetos.RemoveveToableChangeListener (L);} ...} From the above example, a constrained property has two listeners: attribute changes Listels of the listener and the reed attribute change. The listener of the veto attribute changes to the corresponding control statement in its object code, and when the monitoring of the constrained property is changed, it is determined whether to veto this property value should be rejected in the control statement. In summary, whether a beans's constrained property value may change depending on other Beans or whether the Java object allows such changes. The allowable condition is defined by other Beans or Java objects in their own class. JavaBeans' event event processing is one of the cores of the JavaBeans 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 JavaBeans, it is defined a general, expandable event mechanism that provides a public framework for the definition and expansion of the event type and the definition of the model, and is suitable for extensive applications. · There is a high integration with Java language and the environment. • The event can be described as an environment capture and ignition. · It can make other constructors to take some techniques to direct the connection between events, as well as between event source and event listeners. · The event mechanism itself does not rely on complex development tools. In particular, it should be: • An event that can be generated by the specified object class can be found. · Ability to find that the specified object class can observe the events (listening). · Provide a routine registration mechanism that allows the relationship between the dynamic manipulation of the event source and the event listener. · You don't need other virtual machines and languages. • Efficient event delivery between event sources and listeners. · The JavabEans event model can be completed with neutral mappings of the related other component architectural event models. The main components of the JavaBeans event model are: Events pass from the event source to the listener are passed 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. The status information related to the event status object is generally encapsulated 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. E.g:

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 the mouse position * / public point getLocation () {Return New Point (x, y);} EventListener Interface and Event Loker Based on the Java event model Calling, there is thus a way to define and organize event manipulation methods. In JavaBeans, event handling methods are defined in the EventListener interface of the Java.util.EventListener class, and the naming of the EventListener interface is ended at the end of the Listener interface. 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. The registration and logout of the event listener For various possible event listeners to register their own events into the appropriate event source, establish the event stream between the source and event listeners, the event source must provide registration and logout for event listeners. This use process has been seen in the previous Bound property introduction, in actual, event listener's registration and logout to use standard design format:

Public void add ( Listener; public void remove ( listener); for example: first defining an event listener interface:

Public Interface ModelchangedListener Extends Java.util.EventListener {Void ModelChanged (EVENTOBJECT E);} Next define 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} / * The above two methods are crowned with 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

Adapter adaptation class is an extremely important part of the Java event model. In some applications, events are "forwarded" from the source to the transmission. For example: When an event source is issued, there are several event listener objects to receive the event, but only when the specified object reacts, an event adapter class is to be inserted between event sources and event listeners. The adapter class to specify which listeners should be responded. The adaptation class has become an event listener. The event source actually uses the adaptation class as a listener into the listener queue, and the real event responder is not in the listener queue, the event responder should do by appropriate Classification decision. At present, most developments are generated when generating code, and event processing is made by adaptation classes. JavaBeans User-User JavaBeans Developers can add a beans, property editor (PropertyEditor), and BeansInfo interface to describe a beans, Beans's users can pass through the beans in the constructor. This information is used to userize the look of Beans and the action you should do. A beans don't have to have Beanscustomizer, PrperstrpertyEditor and Beansinfo, depending on the actual situation, these are optional. When some Beans is more complicated, they must provide this information, which makes Beans's users can be usernated by Wizard. Some simple beans may not have this information, then the constructor can use the self-contained perspective, see the contents of Beans, and display the information to the standard property list or event table for users to customize Beans, the first few sections The properties, methods, and event names mentioned in the mentioned Beans are named in a certain format, the main role is to make a perspective for the development tool. Of course, it is also to provide the programmer to provide convenience in handwriting, so that he can view his name and know. User Miller Interface (Customizer Interface) When a Beans has its own userizer, you can show your own property table within the constructor. The Java.BeanssS.customizer interface must be implemented when defining the userizer. For example, the following is a "button" beans user unit:

public class OurButtonCustomizer extends Panel implements Customizer {... ... / * table when implementing the general properties such as OurButtonCustomizer, which must be implemented in removePropertyChangeListener addProperChangeListener and, so, can use these tools configured by the function code is added event listener properties . * / ... ... private PropertyChangeSupport changes = new PropertyChangeSupport (this); public void addPropertyChangeListener (PropertyChangeListener l) {changes.addPropertyChangeListener (l); public void removePropertyChangeListener (PropertyChangeListener l) {changes.removePropertyChangeListener (l);}. .. ... PropertyEditor Interface A JavaBeans provides a PropertyEditor class that creates an editor for the specified properties. This class must inherit from the Java.beanss.PropertyEditorsupport class. The programmer of the constructor and the handwritten code does not use this class directly, but is instantiated and called this class in BeansInfo in the next section. Example: public class MoleculeNameEditor extends java.Beanss.PropertyEditorSupport {public String [] getTags () {String resule [] = { "HyaluronicAcid", "Benzene", "buckmisterfullerine", "cyclohexane", "ethane", "water"} }}}} The above example creates a property editor for the Tags property. In the constructor, the properties of the MoleculeName can be selected from the drop-down table should be "Hyaluronicaid" or "Water". The Beansinfo Interface Each Beans Class may have a related BeansInfo class, which describes the appearance of this Beans in the construction tool. Properties, methods, events, display in BeansInfo, show their names, providing simple help notes. E.g:

public class MoleculeBeansInfo extends SimpleBeansInfo {public PropertyDescriptor [] getPropertyDescriptors () {try {PropertyDescriptor pd = new PropertyDescriptor ( "moleculeName", Molecule.class); / * pd by a reference on the MoleculeNameEditor class, and returns the acquired attribute moleculeName * / pd.setPropertyEditorClass (MoleculeNameEditor.class); PropertyDescriptor result [] = {pd}; return result;} catch (Exception ex) {System.err.println ( "MoleculeBeansInfo: unexpected exeption:" ex); return null;} }} JavaBeans persistence When a JavaBeans is userized within the construction tool, all the status of its state should be saved, and the next time the LOAD can be constructed or during operation, it should be The last modified information. In order to do this, save the information of certain fields of Beans, to define beans to implement the java.io.Serializable interface. For example: Public class button ustess java.io.Serializable {} implements information of the field in the beans in the serialization interface, will be saved automatically. If you don't want to save some fields, you can crown in these fields with Transient or Static keywords, Transient and Static variables are not saved. Typically, all of the publication of a beans should be saved, or the internal state can also be selected. Beans Developers When you modify your software, you can add a field, remove references to other classes, change the private / protected / public status of a field, which does not affect the storage structure relationship of the class. However, when a field is removed from the class, change a variable in the location of the class system, change a field to transient / static, or it is transient / static, which is now changed to other features, will cause storage relationships. The change. JavaBeans's storage format JavaBeans Components are designed, generally in the ZIP format file stored in the extended JAR, contain information related to JavaBeans in JAR, and specify which classes of the manifest file are JavaBeans. Taking JAR file stored JAR files to transmit the number of data, it is greatly reduced by data transmission, and bundles some resources required for JavaBeans runtime. This chapter mainly discusses some internal characteristics of JavaBeans and their conventional design methods, reference The JavaBeans Specification 1.0A version. As the world's large ISV is more and more support for JavaBeans, the specification is still evolving in some details, but the basic framework will not change.

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 the mouse position * / public point getLocation () {Return New Point (x, y);} EventListener Interface and Event Loker Based on the Java event model Calling, there is thus a way to define and organize event manipulation methods. In JavaBeans, event handling methods are defined in the EventListener interface of the Java.util.EventListener class, and the naming of the EventListener interface is ended at the end of the Listener interface. 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. E.g:

/ * First define a mouse mobile event object * / public class mouseMoveDexampleEvent Extends java.util.EventObject {// In this class contains status information related to mouse mobile events ...} / * defines mouse mobile events. Listener Interface * / Interface MouseMovedexampleListener Extends Java.util.EventListener {/ * Defines the method of mouse mobile event listeners supported by mouse mobile events * / void MouseMovent MME.

Only the method name, the parameters of the method, and the return value type are defined in the interface. 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 listener of the MousemoveDexampleEvent event. The registration and logout of the event listener For various possible event listeners to register their own events into the appropriate event source, establish the event stream between the source and event listeners, the event source must provide registration and logout for event listeners. This use process has been seen in the previous Bound property introduction, in actual, event listener's registration and logout to use standard design format:

Public void add ( listener; public void remove ( listener);

For example: First define an event listener interface:

Public Interface ModelchangeDlistener Extends Java.util.EventListener {Void ModelChanged (EventObject E);}

Then 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} / * The above two methods are crowned with 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

Adapter adaptation class is an extremely important part of the Java event model. In some applications, events are "forwarded" from the source to the transmission. For example: When an event source is issued, there are several event listener objects to receive the event, but only when the specified object reacts, an event adapter class is to be inserted between event sources and event listeners. The adapter class to specify which listeners should be responded. The adaptation class has become an event listener. The event source actually uses the adaptation class as a listener into the listener queue, and the real event responder is not in the listener queue, the event responder should do by appropriate Classification decision. At present, most developments are generated when generating code, and event processing is made by adaptation classes. JavaBeans User-User JavaBeans Developers can add a beans, property editor (PropertyEditor), and BeansInfo interface to describe a beans, Beans's users can pass through the beans in the constructor. This information is used to userize the look of Beans and the action you should do. A beans don't have to have Beanscustomizer, PrperstrpertyEditor and Beansinfo, depending on the actual situation, these are optional. When some Beans is more complicated, they must provide this information, which makes Beans's users can be usernated by Wizard. Some simple beans may not have this information, then the constructor can use the self-contained perspective, see the contents of Beans, and display the information to the standard property list or event table for users to customize Beans, the first few sections The properties, methods, and event names mentioned in the mentioned Beans are named in a certain format, the main role is to make a perspective for the development tool. Of course, it is also to provide the programmer to provide convenience in handwriting, so that he can view his name and know. User Miller Interface (Customizer Interface) When a Beans has its own userizer, you can show your own property table within the constructor. The Java.BeanssS.customizer interface must be implemented when defining the userizer. For example, the following is a "button" beans user unit:

public class OurButtonCustomizer extends Panel implements Customizer {... ... / * table when implementing the general properties such as OurButtonCustomizer, which must be implemented in removePropertyChangeListener addProperChangeListener and, so, can use these tools configured by the function code is added event listener properties . * / ... ... private PropertyChangeSupport changes = new PropertyChangeSupport (this); public void addPropertyChangeListener (PropertyChangeListener l) {changes.addPropertyChangeListener (l); public void removePropertyChangeListener (PropertyChangeListener l) {changes.removePropertyChangeListener (l);}. .. ... PropertyEditor Interface A JavaBeans provides a PropertyEditor class that creates an editor for the specified properties. This class must inherit from the Java.beanss.PropertyEditorsupport class. The programmer of the constructor and the handwritten code does not use this class directly, but is instantiated and called this class in BeansInfo in the next section. Example: public class MoleculeNameEditor extends java.Beanss.PropertyEditorSupport {public String [] getTags () {String resule [] = { "HyaluronicAcid", "Benzene", "buckmisterfullerine", "cyclohexane", "ethane", "water"} }}}} The above example creates a property editor for the Tags property. In the constructor, the properties of the MoleculeName can be selected from the drop-down table should be "Hyaluronicaid" or "Water". The Beansinfo Interface Each Beans Class may have a related BeansInfo class, which describes the appearance of this Beans in the construction tool. Properties, methods, events, display in BeansInfo, show their names, providing simple help notes. E.g:

public class MoleculeBeansInfo extends SimpleBeansInfo {public PropertyDescriptor [] getPropertyDescriptors () {try {PropertyDescriptor pd = new PropertyDescriptor ( "moleculeName", Molecule.class); / * pd by a reference on the MoleculeNameEditor class, and returns the acquired attribute moleculeName * / pd.setPropertyEditorClass (MoleculeNameEditor.class); PropertyDescriptor result [] = {pd}; return result;} catch (Exception ex) {System.err.println ( "MoleculeBeansInfo: unexpected exeption:" ex); return null;} }} JavaBeans persistence When a JavaBeans is userized within the construction tool, all the status of its state should be saved, and the next time the LOAD can be constructed or during operation, it should be The last modified information. In order to do this, save the information of certain fields of Beans, to define beans to implement the java.io.Serializable interface. For example: Public class button ustess java.io.Serializable {} implements information of the field in the beans in the serialization interface, will be saved automatically. If you don't want to save some fields, you can crown in these fields with Transient or Static keywords, Transient and Static variables are not saved. Typically, all of the publication of a beans should be saved, or the internal state can also be selected. Beans Developers When you modify your software, you can add a field, remove references to other classes, change the private / protected / public status of a field, which does not affect the storage structure relationship of the class. However, when a field is removed from the class, change a variable in the location of the class system, change a field to transient / static, or it is transient / static, which is now changed to other features, will cause storage relationships. The change. JavaBeans's storage format JavaBeans Components are designed, generally in the ZIP format file stored in the extended JAR, contain information related to JavaBeans in JAR, and specify which classes of the manifest file are JavaBeans. Taking JAR file stored JAR files to transmit the number of data, it is greatly reduced by data transmission, and bundles some resources required for JavaBeans runtime. This chapter mainly discusses some internal characteristics of JavaBeans and their conventional design methods, reference The JavaBeans Specification 1.0A version. As the world's large ISV is more and more support for JavaBeans, the specification is still evolving in some details, but the basic framework will not change.

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 the mouse position * / public point getLocation () {Return New Point (x, y);} EventListener Interface and Event Loker Based on the Java event model Calling, there is thus a way to define and organize event manipulation methods. In JavaBeans, event handling methods are defined in the EventListener interface of the Java.util.EventListener class, and the naming of the EventListener interface is ended at the end of the Listener interface. 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. E.g:

/ * First define a mouse mobile event object * / public class mouseMoveDexampleEvent Extends java.util.EventObject {// In this class contains status information related to mouse mobile events ...} / * defines mouse mobile events. Listener Interface * / Interface MouseMovedexampleListener Extends Java.util.EventListener {/ * Defines the method of mouse mobile event listeners supported by mouse mobile events * / void MouseMovent MME.

Only the method name, the parameters of the method, and the return value type are defined in the interface. 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 listener of the MousemoveDexampleEvent event. The registration and logout of the event listener For various possible event listeners to register their own events into the appropriate event source, establish the event stream between the source and event listeners, the event source must provide registration and logout for event listeners. This use process has been seen in the previous Bound property introduction, in actual, event listener's registration and logout to use standard design format:

Public void add ( listener; public void remove ( listener);

For example: First define an event listener interface:

Public Interface ModelchangeDlistener Extends Java.util.EventListener {Void ModelChanged (EventObject E);}

Then 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} / * The above two methods are crowned with 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

Adapter adaptation class is an extremely important part of the Java event model. In some applications, events are "forwarded" from the source to the transmission. For example: When an event source is issued, there are several event listener objects to receive the event, but only when the specified object reacts, an event adapter class is to be inserted between event sources and event listeners. The adapter class to specify which listeners should be responded. The adaptation class has become an event listener. The event source actually uses the adaptation class as a listener into the listener queue, and the real event responder is not in the listener queue, the event responder should do by appropriate Classification decision. At present, most developments are generated when generating code, and event processing is made by adaptation classes. JavaBeans User-User JavaBeans Developers can add a beans, property editor (PropertyEditor), and BeansInfo interface to describe a beans, Beans's users can pass through the beans in the constructor. This information is used to userize the look of Beans and the action you should do. A beans don't have to have Beanscustomizer, PrperstrpertyEditor and Beansinfo, depending on the actual situation, these are optional. When some Beans is more complicated, they must provide this information, which makes Beans's users can be usernated by Wizard. Some simple beans may not have this information, then the constructor can use the self-contained perspective, see the contents of Beans, and display the information to the standard property list or event table for users to customize Beans, the first few sections The properties, methods, and event names mentioned in the mentioned Beans are named in a certain format, the main role is to make a perspective for the development tool. Of course, it is also to provide the programmer to provide convenience in handwriting, so that he can view his name and know. User Miller Interface (Customizer Interface) When a Beans has its own userizer, you can show your own property table within the constructor. The Java.BeanssS.customizer interface must be implemented when defining the userizer. For example, the following is a "button" beans user unit:

public class OurButtonCustomizer extends Panel implements Customizer {... ... / * table when implementing the general properties such as OurButtonCustomizer, which must be implemented in removePropertyChangeListener addProperChangeListener and, so, can use these tools configured by the function code is added event listener properties . * / ... ... private PropertyChangeSupport changes = new PropertyChangeSupport (this); public void addPropertyChangeListener (PropertyChangeListener l) {changes.addPropertyChangeListener (l); public void removePropertyChangeListener (PropertyChangeListener l) {changes.removePropertyChangeListener (l);}. .. ... PropertyEditor Interface A JavaBeans provides a PropertyEditor class that creates an editor for the specified properties. This class must inherit from the Java.beanss.PropertyEditorsupport class. The programmer of the constructor and the handwritten code does not use this class directly, but is instantiated and called this class in BeansInfo in the next section. Example: public class MoleculeNameEditor extends java.Beanss.PropertyEditorSupport {public String [] getTags () {String resule [] = { "HyaluronicAcid", "Benzene", "buckmisterfullerine", "cyclohexane", "ethane", "water"} }}}} The above example creates a property editor for the Tags property. In the constructor, the properties of the MoleculeName can be selected from the drop-down table should be "Hyaluronicaid" or "Water". The Beansinfo Interface Each Beans Class may have a related BeansInfo class, which describes the appearance of this Beans in the construction tool. Properties, methods, events, display in BeansInfo, show their names, providing simple help notes. E.g:

public class MoleculeBeansInfo extends SimpleBeansInfo {public PropertyDescriptor [] getPropertyDescriptors () {try {PropertyDescriptor pd = new PropertyDescriptor ( "moleculeName", Molecule.class); / * pd by a reference on the MoleculeNameEditor class, and returns the acquired attribute moleculeName * / pd.setPropertyEditorClass (MoleculeNameEditor.class); PropertyDescriptor result [] = {pd}; return result;} catch (Exception ex) {System.err.println ( "MoleculeBeansInfo: unexpected exeption:" ex); return null;} }} JavaBeans persistence When a JavaBeans is userized within the construction tool, all the status of its state should be saved, and the next time the LOAD can be constructed or during operation, it should be The last modified information. In order to do this, save the information of certain fields of Beans, to define beans to implement the java.io.Serializable interface. For example: Public class button ustess java.io.Serializable {} implements information of the field in the beans in the serialization interface, will be saved automatically. If you don't want to save some fields, you can crown in these fields with Transient or Static keywords, Transient and Static variables are not saved. Typically, all of the publication of a beans should be saved, or the internal state can also be selected. Beans Developers When you modify your software, you can add a field, remove references to other classes, change the private / protected / public status of a field, which does not affect the storage structure relationship of the class. However, when a field is removed from the class, change a variable in the location of the class system, change a field to transient / static, or it is transient / static, which is now changed to other features, will cause storage relationships. The change. JavaBeans's storage format JavaBeans Components are designed, generally in the ZIP format file stored in the extended JAR, contain information related to JavaBeans in JAR, and specify which classes of the manifest file are JavaBeans. Taking JAR file stored JAR files to transmit the number of data, it is greatly reduced by data transmission, and bundles some resources required for JavaBeans runtime. This chapter mainly discusses some internal characteristics of JavaBeans and their conventional design methods, reference The JavaBeans Specification 1.0A version. As the world's large ISV is more and more support for JavaBeans, the specification is still evolving in some details, but the basic framework will not change.

Posted on February 19, 2005 12:30 am

Favorite this page

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

New Post(0)