1. js调用Java
Java 代码:
(1)CallFromJs 注意这里的类 必须加Public,不是方法加public!!!是类!!!
public class CallFromJs{
public String callFromJs(String info){
return "java" + info;
}
}
(2)javaFx main函数文章来源:https://www.toymoban.com/news/detail-622404.html
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
public class App extends Application{
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
primaryStage.setTitle("Hello, World!");
WebView webView = new WebView();
// setting min height
webView.minHeight(600);
// setting preferred width
webView.prefWidth(1950);
// setting pref heigth
webView.prefHeight(600);
//setting min width
webView.minWidth(1050);
WebEngine engine = webView.getEngine();
CallFromJs bridge = new CallFromJs();
engine.getLoadWorker().stateProperty().addListener(
(ObservableValue<? extends Worker.State> ov, Worker.State oldState, Worker.State newState) -> {
if (newState == Worker.State.SUCCEEDED) {
JSObject window = (JSObject) engine.executeScript("window");
//("window");
window.setMember("apps", bridge);
window.setMember("sout", System.out);
System.out.println("hasinit");
}
}
);
engine.load("http://192.168.31.96:8080/");
VBox root = new VBox();
root.getChildren().add(webView);
Scene scene = new Scene(root,960,600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
vue中执行代码文章来源地址https://www.toymoban.com/news/detail-622404.html
export default {
name: 'App',
components: {
HelloWorld
},
data(){
return{
apps:"",
tit:"btn"
}
},
methods:{
callJavaFx(){
window.sout.println(window.apps.callFromJs("ja"))
var javaja = window.apps.callFromJs("ja")
this.tit = javaja
}
}
}
java调js
WebEngine engine = webviewid.getEngine();
String callJsMethod = "jsMethod(data)";
Object executeScript = engine.executeScript(callJsMethod);
到了这里,关于JavaFx Webview 与js(vue)交互的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!