package frogbean;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
/**
* 在此处插入类型说明。
* 创建日期:(2002-5-6 17:02:28)
* @author:Administrator
*/
public class BangBean extends Canvas implements Serializable {
protected int xm,ym;
protected int cSize=20;//circle size
protected String text="Bang!";
protected int fontSize=48;
protected Color tColor=Color.red;
protected ActionListener actionListener;
class ML extends MouseAdapter{
public void mousePressed(MouseEvent e){
Graphics g=getGraphics();
g.setColor(tColor);
g.setFont(new Font("TimesRoman",Font.BOLD,fontSize));
int width=g.getFontMetrics().stringWidth(text);
g.drawString(text,xm,ym);
g.dispose();
//Call the listener's method:
if(actionListener!=null)
actionListener.actionPerformed(new ActionEvent(BangBean.this,ActionEvent.ACTION_PERFORMED,null));
}
}
class MML extends MouseMotionAdapter{
public void mouseMoved(MouseEvent e){
xm=e.getX();
ym=e.getY();
repaint();
}
}
/**
* BangBean 构造子注解。
*/
public BangBean() {
addMouseListener(new ML());
addMouseMotionListener(new MML());
}
public void addActionListener(ActionListener l) throws TooManyListenersException {
if(actionListener!=null)
throw new TooManyListenersException();
actionListener=l;
}
public String getBeanText(){return text;}
public int getCircleSize(){return cSize;}
public int getFontSize(){return fontSize;}
public Dimension getPreferredSize(){
return new Dimension(200,200);
}
public Color getTextColor(){return tColor;}
//Testing the BangBean:
public static void main(String[] args){
BangBean bb=new BangBean();
try{
//During testing, send action information to the console:
bb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("BangBean action");
}
}
);
}catch(TooManyListenersException e){}
Frame f=new Frame("BangBean Test");
f.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
f.add(bb,BorderLayout.CENTER);
f.setSize(300,300);
f.setVisible(true);
}
public void paint(Graphics g){
g.setColor(Color.black);
g.drawOval(xm-cSize/2,ym-cSize/2,cSize,cSize);
}
public void removeActionListener(ActionListener l){
actionListener=null;
}
public void setBeanText(String newText){text=newText;}
public void setCircleSize(int newSize){cSize=newSize;}
public void setFontSize(int newSize){fontSize=newSize;}
public void setTextColor(Color newColor){tColor=newColor;}
}
以上是bean类
下面用Ibm visual Age for Java 自动生成调试代码,测试上边的bean
package frogbean;
import java.applet.*;
import java.awt.*;
/**
* 在此处插入类型说明。
* 创建日期:(2002-5-6 21:44:50)
* @author:Administrator
*/
public class TestBean extends Applet {
private BangBean ivjfirst = null;
/**
* 返回关于此 applet 的信息。
* @return 返回关于此 applet 信息的字符串。
*/
public String getAppletInfo() {
return "TestBean\n" +
"\n" +
"在此处插入类型说明。\n" +
"创建日期:(2002-5-6 21:44:50)\n" +
"@author:Administrator\n" +
"";
}
/**
* 返回 first 特性值。
* @return frogbean.BangBean
*/
/* 警告:此方法将重新生成。 */
private BangBean getfirst() {
if (ivjfirst == null) {
try {
ivjfirst = new frogbean.BangBean();
ivjfirst.setName("first");
ivjfirst.setBounds(94, 18, 200, 200);
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
return ivjfirst;
}
/**
* 每当部件抛出异常时被调用
* @param exception java.lang.Throwable
*/
private void handleException(java.lang.Throwable exception) {
/* 除去下列各行的注释,以将未捕捉到的异常打印至 stdout。 */
// System.out.println("--------- 未捕捉到的异常 ---------");
// exception.printStackTrace(System.out);
}
/**
* 初始化 applet。
*
* @see #start
* @see #stop
* @see #destroy
*/
public void init() {
try {
super.init();
setName("TestBean");
setLayout(null);
setSize(426, 240);
add(getfirst(), getfirst().getName());
// user code begin {1}
// user code end
} catch (java.lang.Throwable ivjExc) {
// user code begin {2}
// user code end
handleException(ivjExc);
}
}
/**
* 主入口点 - 当部件作为应用程序运行时,启动这个部件。
* @param args java.lang.String[]
*/
public static void main(java.lang.String[] args) {
try {
Frame frame = new java.awt.Frame();
TestBean aTestBean;
Class iiCls = Class.forName("frogbean.TestBean");
ClassLoader iiClsLoader = iiCls.getClassLoader();
aTestBean = (TestBean)java.beans.Beans.instantiate(iiClsLoader,"frogbean.TestBean");
frame.add("Center", aTestBean);
frame.setSize(aTestBean.getSize());
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
};
});
frame.setVisible(true);
} catch (Throwable exception) {
System.err.println("java.applet.Applet 的 main() 中发生异常");
exception.printStackTrace(System.out);
}
}
/**
* 绘制 applet。
* 如果此 applet 不需要绘制(例如,如果它只是其它
* awt 部件的一个容器),则可以安全地除去此方法。
*
* @param g 指定的“图形”窗口
* @see #update
*/
public void paint(Graphics g) {
super.paint(g);
//在此处插入用来绘制 applet 的代码。
}
}
其中
TestBean aTestBean;
Class iiCls = Class.forName("frogbean.TestBean");
ClassLoader iiClsLoader = iiCls.getClassLoader();
aTestBean = (TestBean)java.beans.Beans.instantiate(iiClsLoader,"frogbean.TestBean");
是做什么用的?为什么不直接用
TestBean aTestBean=new TestBean() ?