java

位置:IT落伍者 >> java >> 浏览文章

一个javaBeans的关于RTTI的问题


发布日期:2023年10月29日
 
一个javaBeans的关于RTTI的问题

package frogbean;

import javaawt*;

import javaawtevent*;

import javaio*;

import javautil*;

/**

* 在此处插入类型说明

* 创建日期( ::)

* @authorAdministrator

*/

public class BangBean extends Canvas implements Serializable {

protected int xmym;

protected int cSize=;//circle size

protected String text=Bang!;

protected int fontSize=;

protected Color tColor=Colorred;

protected ActionListener actionListener;

class ML extends MouseAdapter{

public void mousePressed(MouseEvent e){

Graphics g=getGraphics();

gsetColor(tColor);

gsetFont(new Font(TimesRomanFontBOLDfontSize));

int width=ggetFontMetrics()stringWidth(text);

gdrawString(textxmym);

gdispose();

//Call the listeners method:

if(actionListener!=null)

actionListeneractionPerformed(new ActionEvent(BangBeanthisActionEventACTION_PERFORMEDnull));

}

}

class MML extends MouseMotionAdapter{

public void mouseMoved(MouseEvent e){

xm=egetX();

ym=egetY();

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();

}

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:

bbaddActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent e){

Systemoutprintln(BangBean action);

}

}

);

}catch(TooManyListenersException e){}

Frame f=new Frame(BangBean Test);

faddWindowListener(

new WindowAdapter(){

public void windowClosing(WindowEvent e){

Systemexit();

}

}

);

fadd(bbBorderLayoutCENTER);

fsetSize();

fsetVisible(true);

}

public void paint(Graphics g){

gsetColor(Colorblack);

gdrawOval(xmcSize/ymcSize/cSizecSize);

}

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 javaapplet*;

import javaawt*;

/**

* 在此处插入类型说明

* 创建日期( ::)

* @authorAdministrator

*/

public class TestBean extends Applet {

private BangBean ivjfirst = null;

/**

* 返回关于此 applet 的信息

* @return 返回关于此 applet 信息的字符串

*/

public String getAppletInfo() {

return TestBean\n +

\n +

在此处插入类型说明\n +

创建日期( ::)\n +

@authorAdministrator\n +

;

}

/**

* 返回 first 特性值

* @return frogbeanBangBean

*/

/* 警告此方法将重新生成 */

private BangBean getfirst() {

if (ivjfirst == null) {

try {

ivjfirst = new frogbeanBangBean();

ivjfirstsetName(first);

ivjfirstsetBounds( );

// user code begin {}

// user code end

} catch (javalangThrowable ivjExc) {

// user code begin {}

// user code end

handleException(ivjExc);

}

}

return ivjfirst;

}

/**

* 每当部件抛出异常时被调用

* @param exception javalangThrowable

*/

private void handleException(javalangThrowable exception) {

/* 除去下列各行的注释以将未捕捉到的异常打印至 stdout */

// Systemoutprintln( 未捕捉到的异常 );

// exceptionprintStackTrace(Systemout);

}

/**

* 初始化 applet

*

* @see #start

* @see #stop

* @see #destroy

*/

public void init() {

try {

superinit();

setName(TestBean);

setLayout(null);

setSize( );

add(getfirst() getfirst()getName());

// user code begin {}

// user code end

} catch (javalangThrowable ivjExc) {

// user code begin {}

// user code end

handleException(ivjExc);

}

}

/**

* 主入口点 当部件作为应用程序运行时启动这个部件

* @param args javalangString[]

*/

public static void main(javalangString[] args) {

try {

Frame frame = new javaawtFrame();

TestBean aTestBean;

Class iiCls = ClassforName(frogbeanTestBean);

ClassLoader iiClsLoader = iiClsgetClassLoader();

aTestBean = (TestBean)javabeansBeansinstantiate(iiClsLoaderfrogbeanTestBean);

frameadd(Center aTestBean);

framesetSize(aTestBeangetSize());

frameaddWindowListener(new javaawteventWindowAdapter() {

public void windowClosing(javaawteventWindowEvent e) {

Systemexit();

};

});

framesetVisible(true);

} catch (Throwable exception) {

Systemerrprintln(javaappletApplet 的 main() 中发生异常);

exceptionprintStackTrace(Systemout);

}

}

/**

* 绘制 applet

* 如果此 applet 不需要绘制(例如如果它只是其它

* awt 部件的一个容器)则可以安全地除去此方法

*

* @param g 指定的图形窗口

* @see #update

*/

public void paint(Graphics g) {

superpaint(g);

//在此处插入用来绘制 applet 的代码

}

}

其中

TestBean aTestBean;

Class iiCls = ClassforName(frogbeanTestBean);

ClassLoader iiClsLoader = iiClsgetClassLoader();

aTestBean = (TestBean)javabeansBeansinstantiate(iiClsLoaderfrogbeanTestBean);

是做什么用的?为什么不直接用

TestBean aTestBean=new TestBean()   ?

               

上一篇:Java虚拟机的垃圾收集算法

下一篇:Java通过System.getProperties()获取系统参数