[LUA]几个简单的luajava使用例子

转自:http://univasity.iteye.com/blog/493339

代码来个人和自网络,仅供参考,如有纰漏请指正,欢迎交流。

 

01. 在AWT中的使用(来自文章《Java中使用Lua脚本语言 》)

<!--StartFragment-->/

// Hello.java
Java代码   收藏代码
  1. public class Hello  
  2. {  
  3.     public static void main(String[] args)  
  4.     {  
  5.       LuaState L = LuaStateFactory.newLuaState();  
  6.       L.openLibs();      
  7.       System.out.println("这里是Java程序调用Lua脚本");      
  8.       
  9.       // 加载脚本hello.lua,并执行  
  10.       L.LdoFile("res/hello.lua");  
  11.     }  
  12. }  
 
--------------------------------------------------------
-- hello.lua
Lua代码   收藏代码
  1. frame = luajava.newInstance("java.awt.Frame""Lua Java Console")  
  2. console = luajava.newInstance("java.awt.TextArea")  
  3. buttons_pn = luajava.newInstance("java.awt.Panel")  
  4. execute_bt = luajava.newInstance("java.awt.Button""Execute")  
  5. clear_bt = luajava.newInstance("java.awt.Button""Clear")  
  6. exit_bt = luajava.newInstance("java.awt.Button""Exit")  
  7. frame:setSize(600,300)  
  8.   
  9. buttons_pn:add(execute_bt)  
  10. buttons_pn:add(clear_bt)  
  11. buttons_pn:add(exit_bt)  
  12.   
  13. BorderLayout = luajava.bindClass("java.awt.BorderLayout")  
  14.   
  15. frame:add(BorderLayout.NORTH, console)  
  16. frame:add(BorderLayout.SOUTH, buttons_pn)  
  17. frame:pack()  
  18. frame:show()  
  19.   
  20. --  
  21. -- Listeners  
  22. --  
  23.   
  24. execute_cb = {  
  25. actionPerformed = function(ev)  
  26.     print("execute")  
  27.     pcall(loadstring(console:getText()))  
  28. end  
  29. }  
  30.   
  31. jproxy = luajava.createProxy("java.awt.event.ActionListener",execute_cb)  
  32.   
  33. execute_bt:addActionListener(jproxy)  
  34.   
  35.   
  36. clear_cb = {actionPerformed= function (ev)  
  37.     print("clear");  
  38.     console:setText("");  
  39. end  
  40. }  
  41.   
  42. jproxy = luajava.createProxy("java.awt.event.ActionListener" ,clear_cb)  
  43. clear_bt:addActionListener(jproxy)  
  44.   
  45. exit_cb = { actionPerformed=function (ev)  
  46. print("exit")  
  47.     frame:setVisible(false)  
  48.     frame:dispose()  
  49. end  
  50. }  
  51.   
  52.   
  53. jproxyb = luajava.createProxy("java.awt.event.ActionListener" ,exit_cb)  
  54.   
  55. exit_bt:addActionListener(jproxyb)  
  56.   
  57. close_cb = {    }  
  58. function close_cb.windowClosing(ev)  
  59.     print("close")  
  60.     frame:setVisible(false)  
  61.     frame:dispose()  
  62. end  
  63.   
  64. function close_cb.windowActivated(ev)  
  65.     print("act")  
  66. end  
  67.   
  68. jproxy = luajava.createProxy("java.awt.event.WindowListener", close_cb)  
  69. frame:addWindowListener(jproxy)  
 

 

 02. 在SWT中的使用(本人写的一个小例子)

/

// Hello.java

 

 

Java代码   收藏代码
  1. public class Hello {  
  2.   
  3.  /** 
  4.   * @param args 
  5.   */  
  6.  public static void main(String[] args) {  
  7. //  Display display = new Display();  
  8. //  Shell shell = new Shell(display);  
  9. //  shell.setLayout(null);  
  10. //  Text hello = new Text(shell, SWT.MULTI);  
  11. //  shell.setText("Java应用程序");  
  12. //  shell.setSize(200, 100);  
  13. //  hello.setText("Hello,SWT World!");  
  14. //  hello.pack();  
  15. //  shell.open();  
  16. //  while(!shell.isDisposed()){  
  17. //   if(!display.readAndDispatch()){  
  18. //    display.sleep();  
  19. //   }  
  20. //  }  
  21. //  display.dispose();  
  22.     
  23.   LuaState L = LuaStateFactory.newLuaState();  
  24.   L.LdoFile("res/hello.lua");  
  25.   L.close();  
  26.  }  
  27.   
  28. }  

 --------------------------------------------------------------

-- hello.lua

Lua代码   收藏代码
  1. --  
  2. -- create instance  
  3. --  
  4. display = luajava.newInstance("org.eclipse.swt.widgets.Display") -- Display  
  5.   
  6. shell = luajava.newInstance("org.eclipse.swt.widgets.Shell",display) -- Shell  
  7.   
  8. SWT = luajava.bindClass("org.eclipse.swt.SWT") -- SWT  
  9.   
  10. text = luajava.newInstance("org.eclipse.swt.widgets.Text", shell,SWT.MULTI) -- Text  
  11.   
  12. --  
  13. -- init state  
  14. --  
  15. shell:setText("Java应用程序")  
  16. shell:setSize(200100)  
  17. text:setText("Hello SWT World!")  
  18. text:pack()  
  19. shell:open()  
  20.   
  21. --  
  22. -- loop  
  23. --  
  24. while not shell:isDisposed() do  
  25.  if not display:readAndDispatch() then  
  26.   display:sleep()  
  27.  end  
  28. end  
  29. display:dispose()   

 

 

03. 网上找到的一个例子

<!--StartFragment-->

// IBusinessLogic.java
Java代码   收藏代码
  1. public interface IBusinessLogic {  
  2.  public void doLogic ();  
  3. }  
  
----------------------------------------------------
-- LuaLogic.lua
Lua代码   收藏代码
  1. local logic = {}  
  2. function logic.doLogic ()  
  3.  print("hello from logic written in Lua")  
  4. end  
  5. return logic  
  
/
// LuaJavaTest.java
Java代码   收藏代码
  1. public class LuaJavaTest {  
  2.    
  3. public static void main(String[] args) throws ClassNotFoundException, LuaException {  
  4.       LuaState l = LuaStateFactory.newLuaState();  
  5.       l.openLibs();  
  6.   
  7.       l.LdoFile("res/LuaLogic.lua");  
  8.         
  9.       LuaObject logic = l.getLuaObject("logic");  
  10.       IBusinessLogic jlogic = (IBusinessLogic) (logic.createProxy("IBusinessLogic"));  
  11.       jlogic.doLogic();  
  12.         
  13.       l.pop(1);  
  14. }  
  
04.数值和对象传递的一个例子(参考自http://hi.baidu.com/lff0305/blog/item/fd666931da276111eac4af45.html )
-----------------------------------------
-- test01.lua
Lua代码   收藏代码
  1. function sum(a, b)  
  2.  return a+b  
  3. end  
  4. function test1(v)  
  5.  v:init()  
  6. end  
 
/
// Test01.java
Java代码   收藏代码
  1. public class Test01 {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.           
  8.         LuaState L = LuaStateFactory.newLuaState();  
  9.         // 加载lua标准库,否则一些lua基本函数无法使用  
  10.         L.openLibs();  
  11.         // doFile  
  12.         L.LdoFile("res/test01.lua");  
  13.           
  14.         //---------------------------------------------值传递测试  
  15.         // 找到函数 sum  
  16.         L.getField(LuaState.LUA_GLOBALSINDEX, "sum");  
  17.           
  18.         // 参数1压栈  
  19.         L.pushNumber(100);  
  20.           
  21.         // 参数2压栈  
  22.         L.pushNumber(50);  
  23.           
  24.         // 调用,共2个参数1个返回值  
  25.         L.call(21);  
  26.           
  27.         // 保存返回值到result中  
  28.         L.setField(LuaState.LUA_GLOBALSINDEX, "result");  
  29.           
  30.         // 读入result  
  31.         LuaObject lobj = L.getLuaObject("result");  
  32.         // 打印结果   
  33.         System.out.println(lobj.getNumber());  
  34.           
  35.         //---------------------------------------------对象传递测试  
  36.         Value v = new Value();  
  37.           
  38.         L.getField(LuaState.LUA_GLOBALSINDEX, "test1");  
  39.           
  40.         try {  
  41.             L.pushObjectValue(v);  
  42.         } catch (LuaException e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.           
  46.         L.call(10);  
  47.           
  48.         v.print();  
  49.     }  
  50.   
  51. }  
  52.   
  53. class Value {  
  54.     private int i;  
  55.       
  56.     public void init(){  
  57.         i = 111;  
  58.     }  
  59.       
  60.     public void print(){  
  61.         System.out.println(i);  
  62.     }  
  63.       
  64. }  
 
  • luaInJava.rar (116.1 KB)
  • 描述: 部分例子源代码
  • 下载次数: 235

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值