熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

一個javaBeans的關於RTTI的問題

2022-06-13   來源: Java核心技術 

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

From:http://tw.wingwit.com/Article/program/Java/hx/201311/25856.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.