专注于Jsp开发,为Jsp开发提供源动力 VM主机| 海外空间| 郑州网站建设| 郑州网络公司| 洛阳网站建设
jsp空间

应用java与flex轻松构建cs程序

添加时间:[2010-3-9 8:26:26] 

一:在java应用程序中创建一操作本地文件的test.CFile类,创建几个对文件进行
访问的方法;具体如下:    
  •    package test;    
  • import java.io.File;    
  • import cn.smartinvoke.javaflex.gui.IServerObject;  //IServerObject表示此类型是一个服务类型专门接受flex代理类型对象的访问  
  • public class CFile implements IServerObject{      public File file=null;  
  •     public CFile(String fileName) {          file=new File(fileName);  
  •     }      public long getSize(){  
  •         return this.file.length();      }  
  •     public String getName(){          return this.file.getName();  
  •     }      /** 
  •      * @param args      */  
  •     public static void main(String[] args) {            
  •     }    
  • }  
  • Java代码

  •     
  •    package test;  
  •   
  • import java.io.File;  
  •   
  • import cn.smartinvoke.javaflex.gui.IServerObject;  
  • //IServerObject表示此类型是一个服务类型专门接受flex代理类型对象的访问  
  • public class CFile implements IServerObject{  
  •     public File file=null;  
  •     public CFile(String fileName) {  
  •         file=new File(fileName);  
  •     }  
  •     public long getSize(){  
  •         return this.file.length();  
  •     }  
  •     public String getName(){  
  •         return this.file.getName();  
  •     }  
  •     /** 
  •      * @param args 
  •      */  
  •     public static void main(String[] args) {  
  •           
  •     }  
  •   
  • }  
  • packagetest;importjava.io.File;importcn.smartinvoke.javaflex.gui.IServerObject;//IServerObject表示此类型是一个服务类型专门接受flex代理类型对象的访问publicclassCFileimplementsIServerObject{publicFilefile=null; publicCFile(StringfileName){ file=newFile(fileName); }publiclonggetSize(){ returnthis.file.length();}publicStringgetName(){ returnthis.file.getName();} /** *@paramargs */ publicstaticvoidmain(String[]args){ }}      
    二:运用CodeTransform工具生成test.CFile类的代理类test.CFile.as,具体内容如下:


       package test  
  •   import cn.smartinvoke.RemoteObject;  
  • /**  此类继承于cn.smartinvoke.RemoteObject是java中的test.CFile类的代理类型 
  •  专门用于调用java中的对应类型。 */  
  • public class CFile extends RemoteObject {   public function CFile(){  
  •  super();   }  
  •  //调用此方法可以创建java中的test.CFile类型对象   public static function create_CFile(fileName:String):CFile{  
  •    var file:CFile=new CFile();     file.createRemoteObject(null,arguments);  
  •    return file;   }  
  •  //此方法会调用java中的test.CFile.getName()方法,并将该方法的返回值返回   public function getName():String{  
  •  var retObj:Object=this.call("getName",arguments);   return retObj as String;  
  •     }  
  •   public function getSize():Number{   var retObj:Object=this.call("getSize",arguments);  
  •  return Number(retObj);    
  •   }  }  
  • Java代码

  •    package test  
  •   
  • import cn.smartinvoke.RemoteObject;  
  • /** 
  •  此类继承于cn.smartinvoke.RemoteObject是java中的test.CFile类的代理类型 
  •  专门用于调用java中的对应类型。 
  • */  
  • public class CFile extends RemoteObject {  
  •  public function CFile(){  
  •  super();  
  •  }  
  •  //调用此方法可以创建java中的test.CFile类型对象  
  •  public static function create_CFile(fileName:String):CFile{  
  •    var file:CFile=new CFile();  
  •    file.createRemoteObject(null,arguments);  
  •    return file;  
  •  }  
  •  //此方法会调用java中的test.CFile.getName()方法,并将该方法的返回值返回  
  •  public function getName():String{  
  •  var retObj:Object=this.call("getName",arguments);  
  •  return retObj as String;  
  •   
  •   }  
  •   public function getSize():Number{  
  •  var retObj:Object=this.call("getSize",arguments);  
  •  return Number(retObj);  
  •   
  •   }  
  • }  
  •   
  •   
  • packagetest{importcn.smartinvoke.RemoteObject;/**此类继承于cn.smartinvoke.RemoteObject是java中的test.CFile类的代理类型专门用于调用java中的对应类型。*/publicclassCFileextendsRemoteObject{publicfunctionCFile(){ super();}//调用此方法可以创建java中的test.CFile类型对象publicstaticfunctioncreate_CFile(fileName:String):CFile{ varfile:CFile=newCFile(); file.createRemoteObject(null,arguments); returnfile;}//此方法会调用java中的test.CFile.getName()方法,并将该方法的返回值返回publicfunctiongetName():String{ varretObj:Object=this.call("getName",arguments); returnretObjasString;}publicfunctiongetSize():Number{ varretObj:Object=this.call("getSize",arguments); returnNumber(retObj);}}}三:创建flex程序smartinvoke.mxml并调用CFile代理类


     <?xml version="1.0" encoding="utf-8"?>  
  • <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onLoad()">      <mx:Script>  
  •         <![CDATA[             import mx.controls.Alert; 
  •             import test.CFile;             import cn.smartinvoke.executor.Executor; 
  •             /**              *此swf加载完毕后调用此方法 
  •              */             function onLoad():void{ 
  •                 //让smartInvoke为java与flex相互调用做好准备                 Executor.init(); 
  •             }             /** 
  •              *此方法调用java的test.CFile.getSize方法获得C:/win_yy.png文件的大小,并显示出来              */ 
  •             function getFileName():void{                 //调用java创建test.CFile类对象,并将此对象包装成flex的test.CFile 
  •                 //代理对象并返回                 var file:CFile=CFile.create_CFile("C:/win_yy.png"); 
  •                 //获得C:/win_yy.png文件的大小                 var fileSize:Number=file.getSize(); 
  •                                  Alert.show("文件大小为"+fileSize); 
  •             }         ]]>  
  •     </mx:Script>      <mx:Button label="getSize" click="getFileName()"/>  
  • </mx:Application>      
  • Xml代码

  •  <?xml version="1.0" encoding="utf-8"?>  
  • <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onLoad()">  
  •     <mx:Script>  
  •         <![CDATA[ 
  •             import mx.controls.Alert; 
  •             import test.CFile; 
  •             import cn.smartinvoke.executor.Executor; 
  •             /** 
  •              *此swf加载完毕后调用此方法 
  •              */ 
  •             function onLoad():void{ 
  •                 //让smartInvoke为java与flex相互调用做好准备 
  •                 Executor.init(); 
  •             } 
  •             /** 
  •              *此方法调用java的test.CFile.getSize方法获得C:/win_yy.png文件的大小,并显示出来 
  •              */ 
  •             function getFileName():void{ 
  •                 //调用java创建test.CFile类对象,并将此对象包装成flex的test.CFile 
  •                 //代理对象并返回 
  •                 var file:CFile=CFile.create_CFile("C:/win_yy.png"); 
  •                 //获得C:/win_yy.png文件的大小 
  •                 var fileSize:Number=file.getSize(); 
  •                  
  •                 Alert.show("文件大小为"+fileSize); 
  •             } 
  •         ]]>  
  •     </mx:Script>  
  •     <mx:Button label="getSize" click="getFileName()"/>  
  • </mx:Application>  
  •     
  • <?xmlversion="1.0"encoding="utf-8"?><mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"creationComplete="onLoad()"> <mx:Script> <![CDATA[ importmx.controls.Alert; importtest.CFile; importcn.smartinvoke.executor.Executor; /** *此swf加载完毕后调用此方法 */ functiononLoad():void{ //让smartInvoke为java与flex相互调用做好准备 Executor.init(); } /** *此方法调用java的test.CFile.getSize方法获得C:/win_yy.png文件的大小,并显示出来 */ functiongetFileName():void{ //调用java创建test.CFile类对象,并将此对象包装成flex的test.CFile //代理对象并返回 varfile:CFile=CFile.create_CFile("C:/win_yy.png"); //获得C:/win_yy.png文件的大小 varfileSize:Number=file.getSize(); Alert.show("文件大小为"+fileSize); } ]]> </mx:Script> <mx:Buttonlabel="getSize"click="getFileName()"/></mx:Application>四:利用swt将flex整合到java程序当中并运行整个程序。


      package test;  
  •   import org.eclipse.swt.SWT;  
  • import org.eclipse.swt.layout.FillLayout;  import org.eclipse.swt.widgets.Display;  
  • import org.eclipse.swt.widgets.Shell;    
  • import cn.smartinvoke.javaflex.gui.FlashContainer;    
  • public class DemoFirst extends Shell {    
  •     /**      * Launch the application 
  •      * @param args      */  
  •     public static void main(String args[]) {          try {  
  •             Display display = Display.getDefault();              DemoFirst shell = new DemoFirst(display, SWT.SHELL_TRIM);  
  •             shell.open();              shell.layout();  
  •             while (!shell.isDisposed()) {                  if (!display.readAndDispatch())  
  •                     display.sleep();              }  
  •         } catch (Exception e) {              e.printStackTrace();  
  •         }      }  
  •       /** 
  •      * Create the shell      * @param display 
  •      * @param style      */  
  •     public DemoFirst(Display display, int style) {          super(display, style);  
  •         createContents();          setLayout(new FillLayout());  
  •     }    
  •     /**      * Create contents of the window 
  •      */      protected void createContents() {  
  •         setText("smartinvoke测试程序");          //将flash控件添加到窗体  
  •         FlashContainer flashContainer=new FlashContainer(this);          //加载smartinvoke.mxml编译生成的smartinvoke.swf  
  •         flashContainer.loadMovie(0"E:/flexWork/smartinvoke/bin-debug/smartinvoke.swf");          setSize(500375);  
  •         //      }  
  •       @Override  
  •     protected void checkSubclass() {          // Disable the check that prevents subclassing of SWT components  
  •     }    
  • }  
  • Java代码

  •   package test;  
  •   
  • import org.eclipse.swt.SWT;  
  • import org.eclipse.swt.layout.FillLayout;  
  • import org.eclipse.swt.widgets.Display;  
  • import org.eclipse.swt.widgets.Shell;  
  •   
  • import cn.smartinvoke.javaflex.gui.FlashContainer;  
  •   
  • public class DemoFirst extends Shell {  
  •   
  •     /** 
  •      * Launch the application 
  •      * @param args 
  •      */  
  •     public static void main(String args[]) {  
  •         try {  
  •             Display display = Display.getDefault();  
  •             DemoFirst shell = new DemoFirst(display, SWT.SHELL_TRIM);  
  •             shell.open();  
  •             shell.layout();  
  •             while (!shell.isDisposed()) {  
  •                 if (!display.readAndDispatch())  
  •                     display.sleep();  
  •             }  
  •         } catch (Exception e) {  
  •             e.printStackTrace();  
  •         }  
  •     }  
  •   
  •     /** 
  •      * Create the shell 
  •      * @param display 
  •      * @param style 
  •      */  
  •     public DemoFirst(Display display, int style) {  
  •         super(display, style);  
  •         createContents();  
  •         setLayout(new FillLayout());  
  •     }  
  •   
  •     /** 
  •      * Create contents of the window 
  •      */  
  •     protected void createContents() {  
  •         setText("smartinvoke测试程序");  
  •         //将flash控件添加到窗体  
  •         FlashContainer flashContainer=new FlashContainer(this);  
  •         //加载smartinvoke.mxml编译生成的smartinvoke.swf  
  •         flashContainer.loadMovie(0"E:/flexWork/smartinvoke/bin-debug/smartinvoke.swf");  
  •         setSize(500375);  
  •         //  
  •     }  
  •   
  •     @Override  
  •     protected void checkSubclass() {  
  •         // Disable the check that prevents subclassing of SWT components  
  •     }  
  •   
  • }  
  • packagetest;importorg.eclipse.swt.SWT;importorg.eclipse.swt.layout.FillLayout;importorg.eclipse.swt.widgets.Display;importorg.eclipse.swt.widgets.Shell;importcn.smartinvoke.javaflex.gui.FlashContainer;publicclassDemoFirstextendsShell{ /** *Launchtheapplication *@paramargs */ publicstaticvoidmain(Stringargs[]){ try{ Displaydisplay=Display.getDefault(); DemoFirstshell=newDemoFirst(display,SWT.SHELL_TRIM); shell.open(); shell.layout(); while(!shell.isDisposed()){ if(!display.readAndDispatch()) display.sleep(); } }catch(Exceptione){ e.printStackTrace(); } } /** *Createtheshell *@paramdisplay *@paramstyle */ publicDemoFirst(Displaydisplay,intstyle){ super(display,style); createContents(); setLayout(newFillLayout()); } /** *Createcontentsofthewindow */ protectedvoidcreateContents(){ setText("smartinvoke测试程序"); //将flash控件添加到窗体 FlashContainerflashContainer=newFlashContainer(this); //加载smartinvoke.mxml编译生成的smartinvoke.swf flashContainer.loadMovie(0,"E:/flexWork/smartinvoke/bin-debug/smartinvoke.swf"); setSize(500,375); // } @Override protectedvoidcheckSubclass(){ //DisablethecheckthatpreventssubclassingofSWTcomponents }}运行DemoFirst程序就可以看到结果了^_^,是不是觉得一切都很简单呀

    java调用flex也是类似

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

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


      package test  
  • {      import cn.smartinvoke.IServerObject;  
  •           import mx.controls.Alert;  
  •       public class FlexShell implements IServerObject  
  •     {          public function FlexShell()  
  •         {          }  
  •         /**          *将info代表的消息打印出来 
  •          */          public function setStatus(info:String):void{  
  •             Alert.show(info);          }  
  •     }  }  
  • Java代码

  •   package test  
  • {  
  •     import cn.smartinvoke.IServerObject;  
  •       
  •     import mx.controls.Alert;  
  •   
  •     public class FlexShell implements IServerObject  
  •     {  
  •         public function FlexShell()  
  •         {  
  •         }  
  •         /** 
  •          *将info代表的消息打印出来 
  •          */  
  •         public function setStatus(info:String):void{  
  •             Alert.show(info);  
  •         }  
  •     }  
  • }  
  • packagetest{ importcn.smartinvoke.IServerObject; importmx.controls.Alert; publicclassFlexShellimplementsIServerObject { publicfunctionFlexShell() { } /** *将info代表的消息打印出来 */publicfunctionsetStatus(info:String):void{ Alert.show(info);} }}其次:使用CodeTransform工具生成test.FlexShell.as类的代理类test.FlexShell.java,具体内容如下:


    package test;  
  • import cn.smartinvoke.javaflex.gui.RemoteObject;  import cn.smartinvoke.javaflex.gui.FlashContainer;  
  • public class FlexShell extends RemoteObject {   public FlexShell(FlashContainer container){  
  •     super(container);      this.createRemoteObject(null);//调用flex创建该代理类的服务类  
  •  }   public void setStatus(String info){//调用flex对应服务类型的setStatus方法  
  •  this.call("setStatus",new Object[]{info});   }  
  • }  
  • Java代码

  • package test;  
  • import cn.smartinvoke.javaflex.gui.RemoteObject;  
  • import cn.smartinvoke.javaflex.gui.FlashContainer;  
  • public class FlexShell extends RemoteObject {  
  •  public FlexShell(FlashContainer container){  
  •     super(container);  
  •     this.createRemoteObject(null);//调用flex创建该代理类的服务类  
  •  }  
  •  public void setStatus(String info){//调用flex对应服务类型的setStatus方法  
  •  this.call("setStatus",new Object[]{info});  
  •  }  
  • }  
  • packagetest;importcn.smartinvoke.javaflex.gui.RemoteObject;importcn.smartinvoke.javaflex.gui.FlashContainer;publicclassFlexShellextendsRemoteObject{publicFlexShell(FlashContainercontainer){ super(container); this.createRemoteObject(null);//调用flex创建该代理类的服务类}publicvoidsetStatus(Stringinfo){//调用flex对应服务类型的setStatus方法 this.call("setStatus",newObject[]{info});}}最后:在java的DemoFirst类型中加入后台业务处理线程的逻辑如下:


     package test;  
  •   import org.eclipse.swt.SWT;  
  • import org.eclipse.swt.layout.FillLayout;  import org.eclipse.swt.widgets.Display;  
  • import org.eclipse.swt.widgets.Shell;    
  • import cn.smartinvoke.javaflex.gui.FlashContainer;  import cn.smartinvoke.javaflex.gui.ILoadCompleteListener;  
  • import cn.smartinvoke.javaflex.util.Log;    
  • public class DemoFirst extends Shell {    
  •     /**      * Launch the application 
  •      * @param args      */  
  •     public static void main(String args[]) {          try {  
  •             Log.open=true;              Display display = Display.getDefault();  
  •             DemoFirst shell = new DemoFirst(display, SWT.SHELL_TRIM);              shell.open();  
  •             shell.layout();              while (!shell.isDisposed()) {  
  •                 if (!display.readAndDispatch())                      display.sleep();  
  •             }          } catch (Exception e) {  
  •             e.printStackTrace();          }  
  •     }    
  •     /**      * Create the shell 
  •      * @param display      * @param style 
  •      */      public DemoFirst(Display display, int style) {  
  •         super(display, style);          createContents();  
  •         setLayout(new FillLayout());      }  
  •       /** 
  •      * Create contents of the window      */  
  •     protected void createContents() {          setText("smartinvoke测试程序");  
  •         //将flash控件添加到窗体          final FlashContainer flashContainer=new FlashContainer(this);  
  •         /**          *当flash加载完毕后,FlashContainer会调用ILoadCompleteListener 
  •          *的run方法,你可以在这里做一些初始化          */  
  •         flashContainer.completeListener=new ILoadCompleteListener(){              public void run() {  
  •                //启动一后台执行线程                 Thread task=new Thread(){  
  •                    public void run(){                         //模拟后台任务  
  •                        try {                          Thread.sleep(2000);//等待10秒  
  •                                                   FlexShell flexShell=new FlexShell(flashContainer);  
  •                         flexShell.setStatus("后退任务执行完毕...");                      } catch (InterruptedException e) {  
  •                         e.printStackTrace();                      }  
  •                    }                 };  
  •                                 task.setDaemon(true);//将后台线程设置为守护线程,保证主线程退出时此线程也会退出  
  •                task.start();              }     
  •         };            
  •                   //加载smartinvoke.mxml编译生成的smartinvoke.swf  
  •         flashContainer.loadMovie(0"E:/flexWork/smartinvoke/bin-debug/smartinvoke.swf");          setSize(500375);  
  •         //      }  
  •       @Override  
  •     protected void checkSubclass() {          // Disable the check that prevents subclassing of SWT components  
  •     }    
  • }  
  • Java代码

  •  package test;  
  •   
  • import org.eclipse.swt.SWT;  
  • import org.eclipse.swt.layout.FillLayout;  
  • import org.eclipse.swt.widgets.Display;  
  • import org.eclipse.swt.widgets.Shell;  
  •   
  • import cn.smartinvoke.javaflex.gui.FlashContainer;  
  • import cn.smartinvoke.javaflex.gui.ILoadCompleteListener;  
  • import cn.smartinvoke.javaflex.util.Log;  
  •   
  • public class DemoFirst extends Shell {  
  •   
  •     /** 
  •      * Launch the application 
  •      * @param args 
  •      */  
  •     public static void main(String args[]) {  
  •         try {  
  •             Log.open=true;  
  •             Display display = Display.getDefault();  
  •             DemoFirst shell = new DemoFirst(display, SWT.SHELL_TRIM);  
  •             shell.open();  
  •             shell.layout();  
  •             while (!shell.isDisposed()) {  
  •                 if (!display.readAndDispatch())  
  •                     display.sleep();  
  •             }  
  •         } catch (Exception e) {  
  •             e.printStackTrace();  
  •         }  
  •     }  
  •   
  •     /** 
  •      * Create the shell 
  •      * @param display 
  •      * @param style 
  •      */  
  •     public DemoFirst(Display display, int style) {  
  •         super(display, style);  
  •         createContents();  
  •         setLayout(new FillLayout());  
  •     }  
  •   
  •     /** 
  •      * Create contents of the window 
  •      */  
  •     protected void createContents() {  
  •         setText("smartinvoke测试程序");  
  •         //将flash控件添加到窗体  
  •         final FlashContainer flashContainer=new FlashContainer(this);  
  •         /** 
  •          *当flash加载完毕后,FlashContainer会调用ILoadCompleteListener 
  •          *的run方法,你可以在这里做一些初始化 
  •          */  
  •         flashContainer.completeListener=new ILoadCompleteListener(){  
  •             public void run() {  
  •                //启动一后台执行线程  
  •                Thread task=new Thread(){  
  •                    public void run(){  
  •                        //模拟后台任务  
  •                        try {  
  •                         Thread.sleep(2000);//等待10秒  
  •                           
  •                         FlexShell flexShell=new FlexShell(flashContainer);  
  •                         flexShell.setStatus("后退任务执行完毕...");  
  •                     } catch (InterruptedException e) {  
  •                         e.printStackTrace();  
  •                     }  
  •                    }  
  •                };  
  •                  
  •                task.setDaemon(true);//将后台线程设置为守护线程,保证主线程退出时此线程也会退出  
  •                task.start();  
  •             }     
  •         };  
  •           
  •           
  •         //加载smartinvoke.mxml编译生成的smartinvoke.swf  
  •         flashContainer.loadMovie(0"E:/flexWork/smartinvoke/bin-debug/smartinvoke.swf");  
  •         setSize(500375);  
  •         //  
  •     }  
  •   
  •     @Override  
  •     protected void checkSubclass() {  
  •         // Disable the check that prevents subclassing of SWT components  
  •     }  
  •   
  • }  
  • packagetest;importorg.eclipse.swt.SWT;importorg.eclipse.swt.layout.FillLayout;importorg.eclipse.swt.widgets.Display;importorg.eclipse.swt.widgets.Shell;importcn.smartinvoke.javaflex.gui.FlashContainer;importcn.smartinvoke.javaflex.gui.ILoadCompleteListener;importcn.smartinvoke.javaflex.util.Log;publicclassDemoFirstextendsShell{ /** *Launchtheapplication *@paramargs */ publicstaticvoidmain(Stringargs[]){ try{ Log.open=true; Displaydisplay=Display.getDefault(); DemoFirstshell=newDemoFirst(display,SWT.SHELL_TRIM); shell.open(); shell.layout(); while(!shell.isDisposed()){ if(!display.readAndDispatch()) display.sleep(); } }catch(Exceptione){ e.printStackTrace(); } } /** *Createtheshell *@paramdisplay *@paramstyle */ publicDemoFirst(Displaydisplay,intstyle){ super(display,style); createContents(); setLayout(newFillLayout()); } /** *Createcontentsofthewindow */ protectedvoidcreateContents(){ setText("smartinvoke测试程序"); //将flash控件添加到窗体 finalFlashContainerflashContainer=newFlashContainer(this); /** *当flash加载完毕后,FlashContainer会调用ILoadCompleteListener *的run方法,你可以在这里做一些初始化 */ flashContainer.completeListener=newILoadCompleteListener(){ publicvoidrun(){ //启动一后台执行线程 Threadtask=newThread(){ publicvoidrun(){ //模拟后台任务 try{ Thread.sleep(2000);//等待10秒 FlexShellflexShell=newFlexShell(flashContainer); flexShell.setStatus("后退任务执行完毕..."); }catch(InterruptedExceptione){ e.printStackTrace(); } } }; task.setDaemon(true);//将后台线程设置为守护线程,保证主线程退出时此线程也会退出 task.start(); } }; //加载smartinvoke.mxml编译生成的smartinvoke.swf flashContainer.loadMovie(0,"E:/flexWork/smartinvoke/bin-debug/smartinvoke.swf"); setSize(500,375); // } @Override protectedvoidcheckSubclass(){ //DisablethecheckthatpreventssubclassingofSWTcomponents }}将smartinvoke.mxml该为如下:


    <?xml version="1.0" encoding="utf-8"?>  
  • <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"       layout="vertical" creationComplete="onLoad()">  
  •     <mx:Script>          <![CDATA[  
  •             import test.FlexShell;              import mx.controls.Alert;  
  •             import test.CFile;              import cn.smartinvoke.executor.Executor;  
  •             /**              *此swf加载完毕后调用此方法 
  •              */              var  file:CFile=null;  
  •             //将FlexShell引用到当前程序当中,不然flex无法用反射方式创建FlexShell类型对象              var  flexShell:FlexShell;  
  •             function onLoad():void{                  //让smartInvoke为java与flex相互调用做好准备  
  •                 Executor.init();                  this.file=CFile.create_CFile("C:/win_yy.png");  
  •             }              /** 
  •              *此方法调用java的test.CFile.getSize方法获得C:/win_yy.png文件的大小,并显示出来              */  
  •             function getFileName():void{                  //调用java创建test.CFile类对象,并将此对象包装成flex的test.CFile  
  •                 //代理对象并返回                    
  •                 //获得C:/win_yy.png文件的大小                  var fileSize:Number=file.getSize();  
  •                                   Alert.show("文件大小为"+fileSize);  
  •                 //使用完毕后调用dispose方法释放java的对应类型                  file.dispose();  
  •                               }  
  •         ]]>      </mx:Script>  
  •     <mx:Label id="testLabel"/>      <mx:Button label="getSize" click="getFileName()"/>  
  • </mx:Application>  
  • Java代码

  • <?xml version="1.0" encoding="utf-8"?>  
  • <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
  •      layout="vertical" creationComplete="onLoad()">  
  •     <mx:Script>  
  •         <![CDATA[  
  •             import test.FlexShell;  
  •             import mx.controls.Alert;  
  •             import test.CFile;  
  •             import cn.smartinvoke.executor.Executor;  
  •             /** 
  •              *此swf加载完毕后调用此方法 
  •              */  
  •             var  file:CFile=null;  
  •             //将FlexShell引用到当前程序当中,不然flex无法用反射方式创建FlexShell类型对象  
  •             var  flexShell:FlexShell;  
  •             function onLoad():void{  
  •                 //让smartInvoke为java与flex相互调用做好准备  
  •                 Executor.init();  
  •                 this.file=CFile.create_CFile("C:/win_yy.png");  
  •             }  
  •             /** 
  •              *此方法调用java的test.CFile.getSize方法获得C:/win_yy.png文件的大小,并显示出来 
  •              */  
  •             function getFileName():void{  
  •                 //调用java创建test.CFile类对象,并将此对象包装成flex的test.CFile  
  •                 //代理对象并返回  
  •                   
  •                 //获得C:/win_yy.png文件的大小  
  •                 var fileSize:Number=file.getSize();  
  •                   
  •                 Alert.show("文件大小为"+fileSize);  
  •                 //使用完毕后调用dispose方法释放java的对应类型  
  •                 file.dispose();  
  •                   
  •             }  
  •         ]]>  
  •     </mx:Script>  
  •     <mx:Label id="testLabel"/>  
  •     <mx:Button label="getSize" click="getFileName()"/>  
  • </mx:Application>  
  • <?xmlversion="1.0"encoding="utf-8"?><mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"creationComplete="onLoad()"> <mx:Script> <![CDATA[ importtest.FlexShell; importmx.controls.Alert; importtest.CFile; importcn.smartinvoke.executor.Executor; /** *此swf加载完毕后调用此方法 */ varfile:CFile=null; //将FlexShell引用到当前程序当中,不然flex无法用反射方式创建FlexShell类型对象 varflexShell:FlexShell; functiononLoad():void{ //让smartInvoke为java与flex相互调用做好准备 Executor.init(); this.file=CFile.create_CFile("C:/win_yy.png"); } /** *此方法调用java的test.CFile.getSize方法获得C:/win_yy.png文件的大小,并显示出来 */ functiongetFileName():void{ //调用java创建test.CFile类对象,并将此对象包装成flex的test.CFile //代理对象并返回 //获得C:/win_yy.png文件的大小 varfileSize:Number=file.getSize(); Alert.show("文件大小为"+fileSize); //使用完毕后调用dispose方法释放java的对应类型 file.dispose(); } ]]> </mx:Script> <mx:Labelid="testLabel"/> <mx:Buttonlabel="getSize"click="getFileName()"/></mx:Application>重新编译mxml,并运行DemoFirst程序就可以看到效果了。

    关于我们 | 付款方式 | 客户管理 | 网站导航 | 友情连接


    版权所有 2008 三易网络(洛阳)科技开发有限公司 京ICP备06012028号

    服务热线:0371-63653120 63658758(郑州) 0379-63921200   63265368(洛阳)

    QQ在线客服: JSP空间咨询   JSP空间咨询    Email:web@suneasy.cn

    郑州网络公司 郑州网站建设 洛阳网站建设

    总部地址:纱厂南路41号中泰新城泰福苑803室 郑州分公司地址:金水区圣菲城