SERIALIZABLE interface
First, the bean must implement the serializable interface. Objects supporting this interface can save and restore their status by disk. The already customized bean (typically editing its properties in the construction) must be able to save and restore its own status when needed. In order to implement the Serializable interface, you should transfer the java.io.serializable package and implement the serializable for your bean in the definition of the class. Since the bean is also visible, it is extended from the AWT component java.awt.canvas.
The COLOR property The color property is a private instance variable whose initial value is green:
PRIVATE color color = color.green;
GET and SET methods Access private variables must be made through the GET and SET methods. Note: The name format of the getter and setter methods is very special. The method name begins with "get" or "set", and the back is the attribute name, and the first letter of the attribute name is capitalized. (This format is very important, because this rule is used when the self-test tool is working.)
In this way, the format of the GET and SET methods is as follows:
Public
For the color properties of SimpleBean, we define a pair of methods below:
Public color color getcolor () {...}
Public void setColor (Color C) {...}
Test and editor bean
This bean is about to be created, it is going to be in Beanbox. Once it is complete, it will appear in the component section of Toolbox.
Put Bean in Beanbox
First, compile the Java source code, then create an executable JAR file, steps below:
Compile this bean source code to generate simpleBean.class:
Javac SimpleBean.java
Create a Manifest file in a text editor.
The Manifest file defines the name of the class file and indicates that this is a JavaBean. The Manifest file is part of the JAR file. You can name the Manifest.TMP. This file contains two lines of the following:
Name: SimpleBean.classjava-bean: true: True
(In Windows, in the Manifest file, be sure to include a carriage return at the end of the text)
Create an executable JAR file.
Use the jar command to link the Manifest file with the SimpleBean.class file:
(In a row :)
Jar cfm SimpleBean.jar Manifest.tmp SimpleBean.class
In the BEANBOX's file drop-down menu, load the JAR file to beanbox. (The first part of the tutorial has demonstrated this step). From the beanbox's file drop-down menu, select LoadJAR, the file browser will appear. Find the SimpleBean.jar file and select it. Note: SimpleBean has appeared in the bean list in Toolbox. Put the new beans in Beanbox and click SimpleBean in Toobox. The cursor nowcomes a cross shape. Move the cursor to any part of Beanbox, click. SimpleBean appears, the shape is as follows, there is a small green matrix in the red rectangle. The boundary part indicates that SimpleBean has been selected and its property appears in the property table. Drag the boundary with the mouse, you can reposition the SimpleBean in Beanbox. Once the bean is repositioned, the cursor becomes a cross arrow. Because SimpleBean is born from Canvas, you can re-change its size. Just dragging its boundaries, you can do it.
Edit the properties of Bean
The property sheet shows the properties of the selected bean. SimpleBean has the following four properties:
· colour
· Background
· Prospect
· Name
SimpleBean defines color properties, and the other three properties are derived from Canvas. If you click on an attribute such as color, an attribute editor will appear, you can change the color value of the property. Beanbox provides the default properties editor for properties such as fonts and colors.
It is also easy to add other attributes to beans.
State a new attribute as a private variable. It can be set at the beginning of the declaration, but this is not required. Declare a pair of public Getter and Setter methods for reading attribute values and sets new values.
Remember: Naming these methods, the rules are: The name is "get" and "set", followed by the name of the variable, and the first letter should be capitalized.
If you have correctly defined a GET and SET methods for new properties, once Bean is opened in Beanbox, you will appear in the property table in the genus in Beanbox, and BEANBOX will find the same. Attribute names match the GET and SET methods, and display attributes in the attribute table.
Next, we will look at how to add other types of properties to the bean and let the bean generate events and the occurrence of the listener.
JavaBeans and bags
Typically, relative to other beans, whether it is a BEA, which is purchased from a third party or yourself, should be installed in a relatively independent directory. Java's packages can help you complete this work. Java packages can organize interrelated classes.
Put Beans in the package
Put the bean into the package and need to complete the following steps:
Add a packet statement in the beginning of your file. The package statement must be in the first line of the file. A package statement defines a directory path, which is used to store the compiled bean class file, and the path is relative to the current working directory, the subdirector is separated by a period, not a slash. For example, if the compiled bean file is placed in directory / acme / beans, add the following line to your code file: package acme.beans; acme03bean is a simple JavaBean with color properties, which uses Bag. In this example, you must create a directory structure so that the Java compiler can put the generated class files in a suitable location. It is also necessary to define the name of these class files in the package. Once a program is to use the class in the package, you must transfer the package in the program. When generating a JAR file, you have to include the full name of the package and classes to in order to put the bean belonging to the package. (JAR file is an abbreviation for Java Archive Files.) For Beans generated by multiple files, JAR files are an ideal packaging mechanism, and the JAR file can include class files or simultaneous image files. In the current working directory, create a suitable directory structure for the Acme.Beans package. First create two separate directories, one for the Java source file, another for the Java class file: mkdir -p ./src/acme/beansmkdir -p./classes/acme/beans copied the Java source file to the source directory in. In UNIX systems: cp -p acme03bean.java ./src/acme/beans When compiling this javabean, be sure to put compiled class files into the class file directory, so if you use makefile to compile, you must modify it. Corresponding makefile.
Add the label to Beans
In this section of this lesson, an instance variable is added to the ACMeBean. This new variable is stored in the String tag value of the bean. (This bean is completely source code, please see Acme04Bean)
add tag
Define a private variable called Label: Private String Label; once a instance variable is defined, give it a default value via the bean constructor, and set the font value of the modified Label. Add the following two lines to the constructor: this.label = "bean" setFont (New font ("Dialog"; font.plain, 12)); This bean acts as a button. You can set Label in the constructor (such as a Beanbox tool). If you plan to set Label in the design phase, let Label become a property of bean, just add your Get and SET methods to the Bean class. Public string getlabel () {return label;} public void setlabel {string oldlabel = label; label = newLabel;} The method is similar to what you want to add color properties. After that, by adding a few lines of code in the PAINT method, the effect of the button can be more realistic.
G.fillarc (5, 5, 30, 30, 0, 360);
G.fillarc (25, 5, 30, 30, 0, 360);
G.SetColor (color.blue);
INT width = size (). width;
INT height = size (). HEIGHT;
FontMetrics FM = g.getfontmetrics ();
g.drawstring (label,
(Width - fm.stringwidth (label) / 2,
(HEIGHT FM.GETMAXASCENT () - fm.getmaxdescent ()) / 2);
The definition of the Paint method is as follows:
Public void paint (graphics g) {
G.SetColor (beancolor);
g.fillRect (20, 5, 20, 30);
G.fillarc (5, 5, 30, 30, 0, 360);
G.fillarc (25, 5, 30, 30, 0, 360);
G.SetColor (color.blue);
INT width = size (). width;
INT height = size (). HEIGHT;
FontMetrics FM = g.getfontmetrics ();
g.drawstring (label, (width - fm.stringwidth) / 2,
(HEIGHT FM.GETMAXASCENT () - fm.getmaxdescent ()) / 2);
}