本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9694622
建立时间:2004年12月20日




[脚本语言]java里面使用javascript(四):使用java的类
原创空间,  软件技术,  电脑与网络

邢红瑞 发表于 2006/5/22 18:22:48

调用java类必须写全路径名var out = new java.lang.StringBuffer()package test; /** * * @author hongrui xing */import javax.script.ScriptEngine;import javax.script.ScriptEngineManager; public class ScriptingTester extends javax.swing.JFrame {        /** Creates new form ScriptingTester */    public ScriptingTester() {        initComponents();        input.setText("var out = new java.lang.StringBuffer();\nfor (i=0; i<3; i++)\n   out.append('hello, world! ('+i+')\\n');");    }        /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">    private void initComponents() {        java.awt.GridBagConstraints gridBagConstraints;         jLabel1 = new javax.swing.JLabel();        jButton1 = new javax.swing.JButton();        inputPane = new javax.swing.JScrollPane();        input = new javax.swing.JTextArea();        outputPane = new javax.swing.JScrollPane();        output = new javax.swing.JTextArea();         getContentPane().setLayout(new java.awt.GridBagLayout());         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        setFocusable(false);        jLabel1.setText("Scripting tester - press button to execute script");        gridBagConstraints = new java.awt.GridBagConstraints();        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);        getContentPane().add(jLabel1, gridBagConstraints);         jButton1.setText("Script it!");        jButton1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                scriptIt(evt);            }        });         gridBagConstraints = new java.awt.GridBagConstraints();        gridBagConstraints.gridx = 0;        gridBagConstraints.gridy = 2;        gridBagConstraints.insets = new java.awt.Insets(6, 0, 6, 0);        getContentPane().add(jButton1, gridBagConstraints);         input.setColumns(20);        input.setRows(5);        inputPane.setViewportView(input);         gridBagConstraints = new java.awt.GridBagConstraints();        gridBagConstraints.gridx = 0;        gridBagConstraints.gridy = 1;        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);        getContentPane().add(inputPane, gridBagConstraints);         output.setColumns(20);        output.setRows(5);        outputPane.setViewportView(output);         gridBagConstraints = new java.awt.GridBagConstraints();        gridBagConstraints.gridx = 0;        gridBagConstraints.gridy = 3;        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);        getContentPane().add(outputPane, gridBagConstraints);         pack();    }    // </editor-fold>     private void scriptIt(java.awt.event.ActionEvent evt) {        // initiate ScriptEngineManager ScriptEngineManager manager = new ScriptEngineManager();  // return engine ScriptEngine jsengine = manager.getEngineByName("js");  // execute the script                try {            jsengine.eval(input.getText());            String out = new String((StringBuffer)jsengine.get("out"));            output.setText(out);        } catch (javax.script.ScriptException e) {            output.setText("ScriptException" + e);        }    }        /**     * @param args the command line arguments     */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                new ScriptingTester().setVisible(true);            }        });    }        // Variables declaration - do not modify    private javax.swing.JTextArea input;    private javax.swing.JScrollPane inputPane;    private javax.swing.JButton jButton1;    private javax.swing.JLabel jLabel1;    private javax.swing.JTextArea output;    private javax.swing.JScrollPane outputPane;    // End of variables declaration    }


阅读全文(4286) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.039 second(s), page refreshed 144765705 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号