用javascript编写swing程序
在QT上可以使用QML语言来描述界面,而QML使用的脚本语言是javascript,不过咱们做java程序的也能使用javascript来开发
swing的界面。在jdk1.6中已经带有Mozilla Rhino 的 JavaScript 脚本引擎,下面用javascript编写上一编基于邮件服务器的聊天测试http://www.blogjava.net/pengo/archive/2010/12/11/340303.html
?界面。
先在程序要指定要解析的脚本,Main.java
import
?java.io.FileInputStream;
import
?java.io.InputStreamReader;
import
?javax.script.Invocable;
import
?javax.script.ScriptEngine;
import
?javax.script.ScriptEngineManager;
import
?javax.swing.UIManager;
public
?
class
?Main?{
????
public
?
static
?
void
?main(String[]?args)?{
????????
try
?{
????????????UIManager
????????????????????.setLookAndFeel(
"
com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
"
);
????????????ScriptEngineManager?factory?
=
?
new
?ScriptEngineManager();
????????????
//
?指定脚本语言为javascript,engine支持很多种脚本语言
????????????ScriptEngine?engine?
=
?factory.getEngineByName(
"
JavaScript
"
);
????????????
//
?告诉脚本引擎需要加载的脚本代码
????????????InputStreamReader?in?
=
?
new
?InputStreamReader(
new
?FileInputStream(
????????????????????
"
test.js
"
));
????????????engine.eval(in);
????????????Invocable?inv?
=
?(Invocable)?engine;
????????????inv.invokeFunction(
"
main
"
);
????????}?
catch
?(Exception?e)?{
????????????e.printStackTrace();
????????}
????}
}
test.js脚本代码