Lua is a small scripting language. Pontifical
A research group at Catholic University of Rio de Janeiro was developed in 1993 by Robert to ierusalimschy, Waldemar Celes, and Luiz Henrique De Figueiredo. For a brief introduction, see: http://baike.baidu.com/view/416116.htm
· To use Lua scripts in Java, you must have a Lua script interpreter and Java programs that can access the relevant APIs of these scripts, that is, the relevant class libraries. I am using an open source project called luajava, you can find the luajava class library and the source code in: http://www.keplerproject.org/luajava/, use documentation, etc. Download it and unzip it to include two files (: luajava-1.1.jar
Files and luajava-1.1.dll dynamically connect to library files, the luajava-1.1.jar is easy, that is, adding it to the classpath of your project so that the program can use the APIS it provides, and the luaJava-1.1.dll is in trouble, you must add it to your Windows installation directory. For example, if you are using XP and installing it on drive C, you can directly add it to the C: \ Windows directory, of course, you can add it to the JRE under your JDK. Now your project can use the Lua script to implement dynamic expansion! But don't worry. Do you still have tools to write Lua scripts? Cannot be written using notepad? You can use ultraedit, but after you open the Lua file with ue, you will find that it is similar to notepad and does not highlight it (the new version may support Lua scripts). If your ue does not support it, then go to the official website of ultraedit to download the wordfiles file that supports Lua (http://www.ultraedit.com/files/wf/lua.txt files, is a personal file (lua.txt ). Open wordfile.txt in the ultraeditinstallation directory, paste the content in the lua.txt file to the end of wordfile.txt, save the disk, and OK, so there is more Lua IN THE ultraedit syntax highlight item, you can choose to use it. Syntax highlighting in other languages is similar to this. But if you are a Java developer, should you have used eclipse? Can I directly write the Lua script in eclipse? The answer is yes!
This is of course the powerful plug-in management function of eclipse. You can download the luaeclipse plug-in to enable eclipse to write Lua scripts (which can highlight your scripts, is it nice), you can download here: http://www.ideais.com.br/luaeclipse/
After the download and installation, you can create and compile a Lua script for eclipse, note: Set the Lua attributes in the preferences (I don't need to talk about the plug-in installation in eclipse, right ????)
Now everything is ready. Let's have a helloworld!
First, create a testlua project in eclipse, and then write the following program:
Import org. keplerproject. luajava .*;
Public class hello
{
Public static void main (string [] ARGs)
{
Luastate L = luastatefactory. newluastate ();
L. openlibs ();
System. Out. println ("the Java program calls the Lua script ");
// Load the script hello. Lua and execute
L. ldofile ("Res/Hello. Lua ");
}
}
Okay, the program is finished. Of course, it is saved as hello. java. Note that this is Java code! This is the Java code that calls a script named hello. Lua. below is the content of this script file (you can copy them directly to your hello. Lua file ):
========================================================== ======================================
-- Basic Method
Print ("You are currently using the Lua scripting language ")
Print ("Let's take a look at it! \ N ")
-- Feature 1: assign values
A = {1, 2}
B =
Print (A = B, ~ = B) -- Output True, false
A = {1, 2}
B = {1, 2}
Print (A = B, ~ = B) -- outputs false, true
-- Feature 2: Exchange
A, B = 1, 2
A, B = B,
Print ()
Print (B)
Print ("Connect"... "string"... 2 ^ 3)
Print (type (2 ))
-- While loop
I = 0
Print ("while loop example ")
While I <5
Do
Print (I );
I = I + 1
End
-- Repeat loop
I = 0
Print ("repeat loop example ")
Repeat
Print (I)
I = I + 1
Until I> = 5
-- For Loop
Print ("For Loop example ")
For I = 0, 5, 1
Do
Print (I)
End
T1 = {}
T1 [1] = 10
Print (T1 [1])
Function fun (a, B ,...)
Print ()
Print (B)
Print (ARG [1])
Print (ARG [2])
Print (ARG [3])
Return
End
A, B = 2, 3
Fun (a, B, 200,400,500)
========================================================== ======================
Well, it doesn't matter if you don't understand the above script. Run it directly.
You can execute the compilation and execute the hello. Java program. The following output is displayed:
========================================================== ======================
Here is the Lua script called by the Java program
You are using the Lua script language.
Let's take a look at it!
True False
False true
2
1
Connection string 8
Number
While loop example
0
1
2
3
4
Repeat loop example
0
1
2
3
4
For Loop example
0
1
2
3
4
5
10
2
3
200
400
500
========================================================== ============================
What's the matter? That's boring? Okay. Here is a script:
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 = {
Actioncompleted med = function (EV)
Print ("execute ")
Pcall (loadstring (console: gettext ()))
End
}
Jproxy = luajava. createproxy ("Java. AWT. event. actionlistener", execute_cb)
Execute_bt: addactionlistener (jproxy)
Clear_cb = {action1_med = function (EV)
Print ("clear ");
Console: settext ("");
End
}
Jproxy = luajava. createproxy ("Java. AWT. event. actionlistener", clear_cb)
Clear_bt: addactionlistener (jproxy)
Exit_cb = {action1_med = 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)
From: http://blog.163.com/pzxii_001/blog/static/392586792007322103020971/