A bean is a
reusable software component.Beans allow developers to reap the benifits of
rapid application development in java by assembling predefined software
components to create powerful applications and applets .Graphical programming
and design environments (Builder tools) that support beans provide programmers
to reuse and integrate existing components .These components linked together to
create applets ,applications (or) new beans for reuse by others.
Devloping a
simple bean
-------------------------------
1) Create a
Directory for the new Bean
2) Create
The Java Source File
3) Compile
The Source File
4) Create a
Manifest File
5) Generate
a Jar File
6) Start the
BDK
7) Test
Create a
Directory For the New Bean
----------------------------------------------
"c:\bdk\demo\sunw\demo\colors"
Create a
directory for the Bean with "Bean" name ie) colors and change to that
directory.
Create The
Java Source File
----------------------------------
//Creating a
new bean
package
sunw.demo.colors;
import
java.awt.*;
import
java.awt.event.*;
public class
Colors extends Canvas
{
transient
private Color color;
private
boolean rectangular;
public
Colors()
{
addMouseListener(new
MouseAdapter(){
public void
mousePressed(MouseEvent me){
change();
}
});
rectangular=false;
setSize(200,100);
change();
}
public
boolean getRectangular(){
return
rectangular;
}
public void
setRectangular(boolean flag){
this.rectangular=flag;
repaint();
}
public void
change(){
color=randomColor();
repaint();
}
private
Color randomColor(){
int r=(int)
(255*Math.random());
int g=(int)
(255*Math.random());
int b=(int)
(255*Math.random());
return new
Color(r,g,b);
}
public void
paint(Graphics g)
{
Dimension
d=getSize();
int
h=d.height;
int
w=d.width;
g.setColor(color);
if(rectangular)
g.fillRect(0,0,w-1,h-1);
else
g.fillOval(0,0,w-1,h-1);
}
}
*) Save
this file in "colors"
directory.
Compile The Source File
--------------------------------
*) Compile
the source file "Colors.java" .It automatically creates it's class
files.
Create a Manifest File
--------------------------
You must now
create a manifest file .First ,switch to the c:\bdk\demo directory .This is the directory in which the
manifest files for the BDK demos are located .Put the source code for your
manifest file in the file "colors.mft"
Name:
sunw/demo/colors/Colors.class
Java-Bean:
True
Generate a Jar File
----------------------
Beans are
included in the toolbox window of the BDK only if they are in JAR files in the
directory "c:\BDK\Jars" .These
files are generated with the "jar" utility.
c:\bdk\demo>
jar cfm colors.jar colors.mft sunw\demo\colors\*.class
*) It
created a "colors.jar" file ,copy the "colors.jar" file
into "c:\BDK\Jars" directory.
Start The
BDK
-----------------
Change to
the directory "c:\bdk\beanbox" and type 'run' . It opens bean window
.The toolbox window should include an entry labelled 'colors' for new bean.
*) Create an
Instance of Color Bean
*) Chnage
it's propertes.
*) Create an
instnace of "ourbutton" and assign event to "colorbean" .
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి