通过SmartInvoke运用java与flex轻松构建cs程序(两分钟轻松上手)

 

原文地址

http://smartinvoke.cn/pages/disArticel.jsp?id=13


大家都知道flex的web application不能操作和访问本地文件,我们今天就通过
smartInvoke让flex可以轻松的访问到本地文件。

一:在java应用程序中创建一操作本地文件的test.CFile类,创建几个对文件进行
访问的方法;具体如下:

  1.     
  2.    package  test;  
  3.   
  4. import  java.io.File;  
  5.   
  6. import  cn.smartinvoke.javaflex.gui.IServerObject;  
  7. //IServerObject表示此类型是一个服务类型专门接受flex代理类型对象的访问   
  8. public   class  CFile  implements  IServerObject{  
  9.     public  File file= null ;  
  10.     public  CFile(String fileName) {  
  11.         file=new  File(fileName);  
  12.     }  
  13.     public   long  getSize(){  
  14.         return   this .file.length();  
  15.     }  
  16.     public  String getName(){  
  17.         return   this .file.getName();  
  18.     }  
  19.     /**  
  20.      * @param args  
  21.      */   
  22.     public   static   void  main(String[] args) {  
  23.           
  24.     }  
  25.   
  26. }  

      
二:运用CodeTransform工具生成test.CFile类的代理类test.CFile.as,具体内容如下:

  1.     package  test  
  2.   
  3. import  cn.smartinvoke.RemoteObject;  
  4. /**  
  5.  此类继承于cn.smartinvoke.RemoteObject是java中的test.CFile类的代理类型  
  6.  专门用于调用java中的对应类型。  
  7. */   
  8. public   class  CFile  extends  RemoteObject {  
  9.  public  function CFile(){  
  10.  super ();  
  11.  }  
  12.  //调用此方法可以创建java中的test.CFile类型对象   
  13.  public   static  function create_CFile(fileName:String):CFile{  
  14.    var file:CFile=new  CFile();  
  15.    file.createRemoteObject(null ,arguments);  
  16.    return  file;  
  17.  }  
  18.  //此方法会调用java中的test.CFile.getName()方法,并将该方法的返回值返回   
  19.  public  function getName():String{  
  20.  var retObj:Object=this .call( "getName" ,arguments);  
  21.  return  retObj as String;  
  22.   
  23.   }  
  24.   public  function getSize():Number{  
  25.  var retObj:Object=this .call( "getSize" ,arguments);  
  26.  return  Number(retObj);  
  27.   
  28.   }  
  29. }  

三:创建flex程序smartinvoke.mxml并调用CFile代理类

  1.   <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < mx:Application   xmlns:mx = "http://www.adobe.com/2006/mxml"   layout = "absolute"   creationComplete = "onLoad()" >   
  3.     < mx:Script >   
  4.         <![CDATA[  
  5.             import mx.controls.Alert;  
  6.             import test.CFile;  
  7.             import cn.smartinvoke.executor.Executor;  
  8.             /**  
  9.              *此swf加载完毕后调用此方法  
  10.              */  
  11.             function onLoad():void{  
  12.                 //让smartInvoke为java与flex相互调用做好准备  
  13.                 Executor.init();  
  14.             }  
  15.             /**  
  16.              *此方法调用java的test.CFile.getSize方法获得C:/win_yy.png文件的大小,并显示出来  
  17.              */  
  18.             function getFileName():void{  
  19.                 //调用java创建test.CFile类对象,并将此对象包装成flex的test.CFile  
  20.                 //代理对象并返回  
  21.                 var file:CFile=CFile.create_CFile("C:/win_yy.png");  
  22.                 //获得C:/win_yy.png文件的大小  
  23.                 var fileSize:Number=file.getSize();  
  24.                   
  25.                 Alert.show("文件大小为"+fileSize);  
  26.             }  
  27.         ]]>   
  28.     </ mx:Script >   
  29.     < mx:Button   label = "getSize"   click = "getFileName()" />   
  30. </ mx:Application >   
  31.     

四:利用swt 将flex 整合到java程序当中并运行整个程序。

  1.    package  test;  
  2.   
  3. import  org.eclipse.swt.SWT;  
  4. import  org.eclipse.swt.layout.FillLayout;  
  5. import  org.eclipse.swt.widgets.Display;  
  6. import  org.eclipse.swt.widgets.Shell;  
  7.   
  8. import  cn.smartinvoke.javaflex.gui.FlashContainer;  
  9.   
  10. public   class  DemoFirst  extends  Shell {  
  11.   
  12.     /**  
  13.      * Launch the application  
  14.      * @param args  
  15.      */   
  16.     public   static   void  main(String args[]) {  
  17.         try  {  
  18.             Display display = Display.getDefault();  
  19.             DemoFirst shell = new  DemoFirst(display, SWT.SHELL_TRIM);  
  20.             shell.open();  
  21.             shell.layout();  
  22.             while  (!shell.isDisposed()) {  
  23.                 if  (!display.readAndDispatch())  
  24.                     display.sleep();  
  25.             }  
  26.         } catch  (Exception e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30.   
  31.     /**  
  32.      * Create the shell  
  33.      * @param display  
  34.      * @param style  
  35.      */   
  36.     public  DemoFirst(Display display,  int  style) {  
  37.         super (display, style);  
  38.         createContents();  
  39.         setLayout(new  FillLayout());  
  40.     }  
  41.   
  42.     /**  
  43.      * Create contents of the window  
  44.      */   
  45.     protected   void  createContents() {  
  46.         setText("smartinvoke测试程序" );  
  47.         //将flash控件添加到窗体   
  48.         FlashContainer flashContainer=new  FlashContainer( this );  
  49.         //加载smartinvoke.mxml编译生成的smartinvoke.swf   
  50.         flashContainer.loadMovie(0"E:/flexWork/smartinvoke/bin-debug/smartinvoke.swf" );  
  51.         setSize(500375 );  
  52.         //   
  53.     }  
  54.   
  55.     @Override   
  56.     protected   void  checkSubclass() {  
  57.         // Disable the check that prevents subclassing of SWT components   
  58.     }  
  59.   
  60. }  

运行DemoFirst程序就可以看到结果了^_^,是不是觉得一切都很简单呀

java调用flex也是类似

接下来我们模拟一执行后台业务的java工作线程,当此线程执行完毕后调用test.FlexShell类的
setStatus(String info)方法将info字符串显示在flex窗体中,以表示后台业务执行完成。

首先:在flex中创建test.FlexShell.as类,具体内容如下:

  1.    package  test  
  2. {  
  3.     import  cn.smartinvoke.IServerObject;  
  4.       
  5.     import  mx.controls.Alert;  
  6.   
  7.     public   class  FlexShell  implements  IServerObject  
  8.     {  
  9.         public  function FlexShell()  
  10.         {  
  11.         }  
  12.         /**  
  13.          *将info代表的消息打印出来  
  14.          */   
  15.         public  function setStatus(info:String): void {  
  16.             Alert.show(info);  
  17.         }  
  18.     }  
  19. }  

其次:使用CodeTransform工具生成test.FlexShell.as类的代理类test.FlexShell.java,具体内容如下:

  1. package  test;  
  2. import  cn.smartinvoke.javaflex.gui.RemoteObject;  
  3. import  cn.smartinvoke.javaflex.gui.FlashContainer;  
  4. public   class  FlexShell  extends  RemoteObject {  
  5.  public  FlexShell(FlashContainer container){  
  6.     super (container);  
  7.     this .createRemoteObject( null ); //调用flex创建该代理类的服务类   
  8.  }  
  9.  public   void  setStatus(String info){ //调用flex对应服务类型的setStatus方法   
  10.  this .call( "setStatus" , new  Object[]{info});  
  11.  }  
  12. }  

最后:在java的DemoFirst类型中加入后台业务处理线程的逻辑如下:

  1.   package  test;  
  2.   
  3. import  org.eclipse.swt.SWT;  
  4. import  org.eclipse.swt.layout.FillLayout;  
  5. import  org.eclipse.swt.widgets.Display;  
  6. import  org.eclipse.swt.widgets.Shell;  
  7.   
  8. import  cn.smartinvoke.javaflex.gui.FlashContainer;  
  9. import  cn.smartinvoke.javaflex.gui.ILoadCompleteListener;  
  10. import  cn.smartinvoke.javaflex.util.Log;  
  11.   
  12. public   class  DemoFirst  extends  Shell {  
  13.   
  14.     /**  
  15.      * Launch the application  
  16.      * @param args  
  17.      */   
  18.     public   static   void  main(String args[]) {  
  19.         try  {  
  20.             Log.open=true ;  
  21.             Display display = Display.getDefault();  
  22.             DemoFirst shell = new  DemoFirst(display, SWT.SHELL_TRIM);  
  23.             shell.open();  
  24.             shell.layout();  
  25.             while  (!shell.isDisposed()) {  
  26.                 if  (!display.readAndDispatch())  
  27.                     display.sleep();  
  28.             }  
  29.         } catch  (Exception e) {  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.   
  34.     /**  
  35.      * Create the shell  
  36.      * @param display  
  37.      * @param style  
  38.      */   
  39.     public  DemoFirst(Display display,  int  style) {  
  40.         super (display, style);  
  41.         createContents();  
  42.         setLayout(new  FillLayout());  
  43.     }  
  44.   
  45.     /**  
  46.      * Create contents of the window  
  47.      */   
  48.     protected   void  createContents() {  
  49.         setText("smartinvoke测试程序" );  
  50.         //将flash控件添加到窗体   
  51.         final  FlashContainer flashContainer= new  FlashContainer( this );  
  52.         /**  
  53.          *当flash加载完毕后,FlashContainer会调用ILoadCompleteListener  
  54.          *的run方法,你可以在这里做一些初始化  
  55.          */   
  56.         flashContainer.completeListener=new  ILoadCompleteListener(){  
  57.             public   void  run() {  
  58.                //启动一后台执行线程   
  59.                Thread task=new  Thread(){  
  60.                    public   void  run(){  
  61.                        //模拟后台任务   
  62.                        try  {  
  63.                         Thread.sleep(2000 ); //等待10秒   
  64.                           
  65.                         FlexShell flexShell=new  FlexShell(flashContainer);  
  66.                         flexShell.setStatus("后退任务执行完毕..." );  
  67.                     } catch  (InterruptedException e) {  
  68.                         e.printStackTrace();  
  69.                     }  
  70.                    }  
  71.                };  
  72.                  
  73.                task.setDaemon(true ); //将后台线程设置为守护线程,保证主线程退出时此线程也会退出   
  74.                task.start();  
  75.             }     
  76.         };  
  77.           
  78.           
  79.         //加载smartinvoke.mxml编译生成的smartinvoke.swf   
  80.         flashContainer.loadMovie(0"E:/flexWork/smartinvoke/bin-debug/smartinvoke.swf" );  
  81.         setSize(500375 );  
  82.         //   
  83.     }  
  84.   
  85.     @Override   
  86.     protected   void  checkSubclass() {  
  87.         // Disable the check that prevents subclassing of SWT components   
  88.     }  
  89.   
  90. }  

将smartinvoke.mxml该为如下:

  1. <?xml version= "1.0"  encoding= "utf-8" ?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"   
  3.      layout="vertical"  creationComplete= "onLoad()" >  
  4.     <mx:Script>  
  5.         <![CDATA[  
  6.             import  test.FlexShell;  
  7.             import  mx.controls.Alert;  
  8.             import  test.CFile;  
  9.             import  cn.smartinvoke.executor.Executor;  
  10.             /**  
  11.              *此swf加载完毕后调用此方法  
  12.              */   
  13.             var  file:CFile=null ;  
  14.             //将FlexShell引用到当前程序当中,不然flex无法用反射方式创建FlexShell类型对象   
  15.             var  flexShell:FlexShell;  
  16.             function onLoad():void {  
  17.                 //让smartInvoke为java与flex相互调用做好准备   
  18.                 Executor.init();  
  19.                 this .file=CFile.create_CFile( "C:/win_yy.png" );  
  20.             }  
  21.             /**  
  22.              *此方法调用java的test.CFile.getSize方法获得C:/win_yy.png文件的大小,并显示出来  
  23.              */   
  24.             function getFileName():void {  
  25.                 //调用java创建test.CFile类对象,并将此对象包装成flex的test.CFile   
  26.                 //代理对象并返回   
  27.                   
  28.                 //获得C:/win_yy.png文件的大小   
  29.                 var fileSize:Number=file.getSize();  
  30.                   
  31.                 Alert.show("文件大小为" +fileSize);  
  32.                 //使用完毕后调用dispose方法释放java的对应类型   
  33.                 file.dispose();  
  34.                   
  35.             }  
  36.         ]]>  
  37.     </mx:Script>  
  38.     <mx:Label id="testLabel" />  
  39.     <mx:Button label="getSize"  click= "getFileName()" />  
  40. </mx:Application>  

重新编译mxml,并运行DemoFirst 程序就可以看到效果了。

到现在我们已经轻松的实现java与flex之间互相调用了,是不是有种幸福来得太突然的感觉^_^

 

下载地址 http://smartinvoke.cn/pages/download.jsp

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值