Java Script Dynamic Usage

Source: Internet
Author: User
Tags bind

function Dosth () {
Jf.setsize (500,300);
Jf.setvisible (true);
Jf.setdefaultcloseoperation (JF. Exit_on_close);
}
Function GetNames () {
List.add ("Dosth");
List.add ("GetNames");
return list;
}
Function Testjs () {
Print (' Hello world! '); 
}
Import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
Import Javax.script.Bindings;
Import javax.script.Invocable;
Import Javax.script.ScriptContext;
Import Javax.script.ScriptEngine;
Import Javax.script.ScriptEngineManager;
Import Javax.swing.JFrame;
/**
*
* @author hadeslee
*/
public class Test {
public static void Main (string[] args) throws Exce ption {
//to generate a parse JS script resolution engine based on the suffix name of JS
ScriptEngine engin=new Scriptenginemanager (). Getenginebyextension ("JS");
 Check to see if this engine implements the following very useful interface
System.out.println (engin instanceof invocable);
//Declare two objects and pass it to JS
JFrame jf=new JFrame ("test");
List<string> List=nEW arraylist<string> ();
//Get tying the key value object, put the current two Java objects into the
Bindings bind=engin.createbindings (),
Bind.put ("JF", JF),
Bind.put (" List ", list);
//Put the key-value object under the tying, the scope is the range of the current engine
Engin.setbindings (bind,scriptcontext.engine_scope);
//execute a section of code written in a JS file with the engine
Object Obj=engin.eval (New FileReader ("Test.js"));
//This time the return value is of course null
System.out.println (obj);
//force the current engine to invocable so that you can invoke a function defined in the JS file
Invocable in= (invocable) Engin;
//Got the object returned from JS
list<string> l= (list<string>) in.invokefunction ("GetNames");
System.out.println (l);
//Call another function defined in JS
in.invokefunction ("Testjs");
///Finally call a function that will make the form we defined earlier
In.invokefunction ("Dosth");
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.