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

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

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

一:在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. }  

Java代码 复制代码
  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. }  

Java代码 复制代码
  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. }   
  30.   
  31.   
三:创建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.     

Xml代码 复制代码
  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. }  

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. }  

Java代码 复制代码
  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代码 复制代码
  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. }  

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. 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>  

Java代码 复制代码
  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 程序就可以看到效果了。

关于我们 | 付款方式 | 客户管理 | 友情链接 | 网站导航 | 在线地图


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

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

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

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

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