JavaBean Getting Started

zhaozj2021-02-17  70

JavaBean Getting Started

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 () {setostostart (true); setBackground; 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 void value () {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 three (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;}} You should define a common static end event identifier for each event in this event setting, such as the exPloded in this example.

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

Import java.util. *; public interface firesworkslistener extends EventListener {public Abstract void Explaoded (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 listeners = new Vector (); ... public void addFireworksListener (FireworksListener f) {listeners.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 = listeners.elements (); enum.hasMoreElements ();) ((FireworksListener) enum.nextElement ()) exploded (e);.} public void run () {. . ProcessFireworksevent (New Fireworksevent (this, fireworksevent.explode);

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

New Post(0)