在QT上可以使用QML語言來描述界面,,而QML使用的腳本語言是javascript,不過咱們做java程序的也能使用javascript來開發(fā)swing的界面,。在jdk1.6中已經(jīng)帶有Mozilla Rhino 的 JavaScript 腳本引擎,,下面用javascript編寫上一編基于郵件服務(wù)器的聊天測試http://www./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腳本代碼 importPackage(javax.swing); importPackage(javax.swing.border); importPackage(java.awt.event); importClass(java.lang.System); var contentPane; var messagePane; var sentPane; var button_1; function main(){ var frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100, 100, 542, 506); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); frame.setContentPane(contentPane); contentPane.setLayout(null); messagePane = new JTextPane(); messagePane.setContentType("text/html"); messagePane.setEditable(false); var scrollPane1 = new JScrollPane(messagePane); scrollPane1.setBounds(10, 10, 506, 290); contentPane.add(scrollPane1); sentPane = new JEditorPane(); var scrollPane = new JScrollPane(sentPane); scrollPane.setBounds(10, 320, 506, 110); contentPane.add(scrollPane); var button = new JButton("關(guān)閉"); button.addActionListener( new JavaAdapter( ActionListener, { actionPerformed : function(event){ System.exit(0); } } ) ); button.setBounds(388, 435, 58, 27); contentPane.add(button); button_1 = new JButton("發(fā)送"); button_1.setEnabled(false); button_1.setBounds(458, 435, 58, 27); contentPane.add(button_1); var button_2 = new JButton("配置"); button_2.setBounds(10, 431, 52, 30); contentPane.add(button_2); var button_3 = new JButton("登錄"); button_3.setBounds(69, 433, 52, 30); contentPane.add(button_3); frame.setVisible(true); } 運(yùn)行效果: 效果還是跟以前一樣帥, 只要把規(guī)則定義好,,java(swing/swt)程序也可以像QT一樣,,定義一套類似QML的界面描述語言,方便開發(fā)中的界面設(shè)計(jì)。 |
|