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");
}
}