转自:http://univasity.iteye.com/blog/493339
代码来个人和自网络,仅供参考,如有纰漏请指正,欢迎交流。
01. 在AWT中的使用(来自文章《Java中使用Lua脚本语言 》)
<!--StartFragment-->/
// Hello.java
- public class Hello
- {
- public static void main(String[] args)
- {
- LuaState L = LuaStateFactory.newLuaState();
- L.openLibs();
- System.out.println("这里是Java程序调用Lua脚本");
- // 加载脚本hello.lua,并执行
- L.LdoFile("res/hello.lua");
- }
- }
--------------------------------------------------------
-- hello.lua
- frame = luajava.newInstance("java.awt.Frame", "Lua Java Console")
- console = luajava.newInstance("java.awt.TextArea")
- buttons_pn = luajava.newInstance("java.awt.Panel")
- execute_bt = luajava.newInstance("java.awt.Button", "Execute")
- clear_bt = luajava.newInstance("java.awt.Button", "Clear")
- exit_bt = luajava.newInstance("java.awt.Button", "Exit")
- frame:setSize(600,300)
- buttons_pn:add(execute_bt)
- buttons_pn:add(clear_bt)
- buttons_pn:add(exit_bt)
- BorderLayout = luajava.bindClass("java.awt.BorderLayout")
- frame:add(BorderLayout.NORTH, console)
- frame:add(BorderLayout.SOUTH, buttons_pn)
- frame:pack()
- frame:show()
- --
- -- Listeners
- --
- execute_cb = {
- actionPerformed = function(ev)
- print("execute")
- pcall(loadstring(console:getText()))
- end
- }
- jproxy = luajava.createProxy("java.awt.event.ActionListener",execute_cb)
- execute_bt:addActionListener(jproxy)
- clear_cb = {actionPerformed= function (ev)
- print("clear");
- console:setText("");
- end
- }
- jproxy = luajava.createProxy("java.awt.event.ActionListener" ,clear_cb)
- clear_bt:addActionListener(jproxy)
- exit_cb = { actionPerformed=function (ev)
- print("exit")
- frame:setVisible(false)
- frame:dispose()
- end
- }
- jproxyb = luajava.createProxy("java.awt.event.ActionListener" ,exit_cb)
- exit_bt:addActionListener(jproxyb)
- close_cb = { }
- function close_cb.windowClosing(ev)
- print("close")
- frame:setVisible(false)
- frame:dispose()
- end
- function close_cb.windowActivated(ev)
- print("act")
- end
- jproxy = luajava.createProxy("java.awt.event.WindowListener", close_cb)
- frame:addWindowListener(jproxy)
02. 在SWT中的使用(本人写的一个小例子)
/
// Hello.java
- public class Hello {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // Display display = new Display();
- // Shell shell = new Shell(display);
- // shell.setLayout(null);
- // Text hello = new Text(shell, SWT.MULTI);
- // shell.setText("Java应用程序");
- // shell.setSize(200, 100);
- // hello.setText("Hello,SWT World!");
- // hello.pack();
- // shell.open();
- // while(!shell.isDisposed()){
- // if(!display.readAndDispatch()){
- // display.sleep();
- // }
- // }
- // display.dispose();
- LuaState L = LuaStateFactory.newLuaState();
- L.LdoFile("res/hello.lua");
- L.close();
- }
- }
--------------------------------------------------------------
-- hello.lua
- --
- -- create instance
- --
- display = luajava.newInstance("org.eclipse.swt.widgets.Display") -- Display
- shell = luajava.newInstance("org.eclipse.swt.widgets.Shell",display) -- Shell
- SWT = luajava.bindClass("org.eclipse.swt.SWT") -- SWT
- text = luajava.newInstance("org.eclipse.swt.widgets.Text", shell,SWT.MULTI) -- Text
- --
- -- init state
- --
- shell:setText("Java应用程序")
- shell:setSize(200, 100)
- text:setText("Hello SWT World!")
- text:pack()
- shell:open()
- --
- -- loop
- --
- while not shell:isDisposed() do
- if not display:readAndDispatch() then
- display:sleep()
- end
- end
- display:dispose()
03. 网上找到的一个例子
<!--StartFragment-->
// IBusinessLogic.java
- public interface IBusinessLogic {
- public void doLogic ();
- }
----------------------------------------------------
-- LuaLogic.lua
- local logic = {}
- function logic.doLogic ()
- print("hello from logic written in Lua")
- end
- return logic
/
// LuaJavaTest.java
- public class LuaJavaTest {
- public static void main(String[] args) throws ClassNotFoundException, LuaException {
- LuaState l = LuaStateFactory.newLuaState();
- l.openLibs();
- l.LdoFile("res/LuaLogic.lua");
- LuaObject logic = l.getLuaObject("logic");
- IBusinessLogic jlogic = (IBusinessLogic) (logic.createProxy("IBusinessLogic"));
- jlogic.doLogic();
- l.pop(1);
- }
04.数值和对象传递的一个例子(参考自http://hi.baidu.com/lff0305/blog/item/fd666931da276111eac4af45.html )
-----------------------------------------
-- test01.lua
- function sum(a, b)
- return a+b
- end
- function test1(v)
- v:init()
- end
/
// Test01.java
- public class Test01 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- LuaState L = LuaStateFactory.newLuaState();
- // 加载lua标准库,否则一些lua基本函数无法使用
- L.openLibs();
- // doFile
- L.LdoFile("res/test01.lua");
- //---------------------------------------------值传递测试
- // 找到函数 sum
- L.getField(LuaState.LUA_GLOBALSINDEX, "sum");
- // 参数1压栈
- L.pushNumber(100);
- // 参数2压栈
- L.pushNumber(50);
- // 调用,共2个参数1个返回值
- L.call(2, 1);
- // 保存返回值到result中
- L.setField(LuaState.LUA_GLOBALSINDEX, "result");
- // 读入result
- LuaObject lobj = L.getLuaObject("result");
- // 打印结果
- System.out.println(lobj.getNumber());
- //---------------------------------------------对象传递测试
- Value v = new Value();
- L.getField(LuaState.LUA_GLOBALSINDEX, "test1");
- try {
- L.pushObjectValue(v);
- } catch (LuaException e) {
- e.printStackTrace();
- }
- L.call(1, 0);
- v.print();
- }
- }
- class Value {
- private int i;
- public void init(){
- i = 111;
- }
- public void print(){
- System.out.println(i);
- }
- }