JavaBeans initial contact

xiaoxiao2021-03-06  39

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 aldenn1 () {// Alden1 () is Alden1 constructor, with C The meaning of the constructor is the same setBackground; 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 an 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;} / * get the specified element value in the array * / public int getDataTaset (INT X) {Return Dataset [x] }} 3. Bound attribute 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 to use some kind of The method must indicate which object of which object is to be used. In the following program, the method of ignition event is performed, and the method used in this operation is 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, development tools, the method calls the changes of other JavaBeans register addPropertyChangeListener In the listener queue L of the opstring 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 model 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 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 JavaBeans event model and the associated other component architecture event model can be completed. 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. 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 the mouse position * / public point getLocation () {Return New Point (x, y);} EventListener Interface and event listener due to the Java event model is Based on method call, there is thus a way to define and organize event handling 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 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

For example, following is a "button" of a user device Beans: public class OurButtonCustomizer extends Panel implements Customizer {... ... / * table when implementing the general properties such as OurButtonCustomizer, which must be implemented in addProperChangeListener and removePropertyChangeListener In this way, the constructor can use these functional code to add a listener to the attribute event. * / ... ... 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.

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

New Post(0)