JavaBean Getting Started

xiaoxiao2021-03-06  108

General requirements

First, you must have a common constructor that does not have parameters. This constructor should also set the default value of the characteristics by calling the setting method of each characteristic, for example:

Public fireworks ()

{

SetAutostart (TRUE);

SetBackground ;; Color.Black;

Setspeed (10);

Setradius (40);

.

.

.

}

If Bean is a visual bean that inherits from the Java.awt.component class, you should define a default preferred size for Beans, for example:

Public Dimension GetPreferRedsize ()

{

Return (New Dimension (RADIUS * 3, RADIUS * 3);

}

Public Dimension GetMinimumsize ()

{

Return getpreferredsize ();

}

characteristic

For each feature you need, you should have a dedicated instance variable with matching public getter and setter methods, for example:

Private int speed;

.

.

.

Public int getSpeed ​​()

{

Return Speed;

}

Public void setspeed (Int S)

{

Speed ​​= S;

}

This GET and SET methods must have the same name as the instance variable, but the first letter should be uppercase and start with GET and SET.

Confirm that Beans is also important when changing their feature at any time, confirming that Bean is active in runtime. If the changes in the characteristic affect the visual appearance of the bean, you should call with this feature setting.

Repaint () ;.

Similarly, if the changes in the characteristics affect the size and location of the BEAN, you need to confirm that the validated thing. We recommend writing your own ValidateAll method as follows:

Private vid validateAll ()

{

IF (isvalid ())

{

Component self = this;

Self.INVALIDATE ();

Component myparent = self.getparent ();

IF (myParent! = null)

{

myparent.invalidate ();

Self = myparent;

}

Self.validate ();

}

}

Then call the method set by this feature

ValidateAll () ;.

The Bean class will not be able to perform assumptions about the call feature setting method command. You should write beans to initially construct it, and then set its characteristics in any command while not causeing errors.

operating

For each operation you need, you should have a public method, for example:

Public void start ()

{

IF (thread == NULL)

{

Thread = new thread (this);

Thread.start ();

}

}

You should operate independently in the case where you do not need to find users to create a connection or set a lot of features. For example, if you write an audio bean, you want to open all steps to open the sound by playing operation, complete all settings you need and play the sound. Similarly, even if the sound is not played, the stop operation should also work.

event

You should define events and listener classes for each event or event setting you need. For this example, see the FireworkSevent.java source file and the fireworks.java file. The source of this event class should be like this:

Import java.awt. *;

Import java.util. *;

Public class fireworksevent extends EventObject {

Public static final int exPloded = 1;

INT ID = 0;

Public FireworkSevent (Component Source, Int ID)

{

Super (Source);

THIS.ID = ID;

}

Public int getID ()

{

Return ID;

}

}

You should define a common static end event identifier for each event in this event setting, such as an exPloded in this example.

For the source of the listener class, see the FireworksListener.java source file:

Import java.util. *;

Public Interface FireworksListener Extends EventListener

{

Public Abstract Void Explaod (FireworkSevent E);

}

You should define a public abstract method for each event in this event setting, such as the exPloded in this example. Moreover, the listener class must extend EventListener so that the JAR wizard can find it.

Then, if the event is played by the Bean class, it must track the object of the listening event. To do this, you need to define the listener instance variables and the AddListener and Removelistener methods. Returns the Fireworks.java source, for example, you will find:

Private vector listener = new vector ();

.

.

.

Public Void AddFireworksListener (FireworksListener F)

{

Listener.addelement (f);

}

Public Void RemovefireworksListener (FireworksListener F)

{

Listeners.RemoveElement (f);

}

Finally, the Bean class needs to actually broadcast the event to all listeners with the correct number. To do this, you need to define the ProcessEvent method and call it at an appropriate number, for example:

Public Void ProcessFireworkSevent (FireworkSevent E)

{

For (Enumeration Enum = listener.elements (); Enum.haASMoreElements ()

(FireworksListener) ENUM.NEXTELEMENT ()). EXPLODED (E);

}

Public void Run ()

{

.

.

.

ProcessFireworkSevent (New Fireworksevent (this, fireworksevent.explode);

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

New Post(0)