1、javafx直接执行html中的js脚本
2、把java中的对象注入到html中,在html网页中调用java端的代码,执行javafx中的方法。 代码:
package com.application;
import com.controller.system.IndexController;
import com.jfoenix.controls.JFXDecorator;
import io.datafx.controller.flow.Flow;
import io.datafx.controller.flow.container.DefaultFlowContainer;
import io.datafx.controller.flow.context.FXMLViewFlowContext;
import io.datafx.controller.flow.context.ViewFlowContext;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class IndexApp extends Application {
@FXMLViewFlowContext
private ViewFlowContext flowContext;
@Override
public void start(Stage stage) throws Exception {
// TODO Auto-generated method stub
try {
Flow flow = new Flow(IndexController.class);
DefaultFlowContainer container = new DefaultFlowContainer();
flowContext = new ViewFlowContext();
flowContext.register("Stage", stage);
flow.createHandler(flowContext).start(container);
JFXDecorator decorator = new JFXDecorator(stage, container.getView());
Scene scene = new Scene(decorator, 600, 600);
scene.getStylesheets().add(IndexApp.class.getResource("/resources/css/jfoenix-fonts.css").toExternalForm());
scene.getStylesheets()
.add(IndexApp.class.getResource("/resources/css/jfoenix-design.css").toExternalForm());
scene.getStylesheets().add(IndexApp.class.getResource("/resources/css/jfoenix-main-demo.css").toExternalForm());
stage.setScene(scene);
stage.setResizable(false);
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
package com.controller.system;
import javax.annotation.PostConstruct;
import io.datafx.controller.FXMLController;
import io.datafx.controller.flow.FlowException;
import io.datafx.controller.flow.context.FXMLViewFlowContext;
import io.datafx.controller.flow.context.ViewFlowContext;
import io.datafx.controller.util.VetoException;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;
@FXMLController(value = "/fxml/Index.fxml")
public class IndexController {
@FXMLViewFlowContext
private ViewFlowContext context;
@FXML
private WebView mWebview;
@FXML
private Button btn;
@PostConstruct
public void init() throws FlowException, VetoException {
String myUrl = this.getClass().getResource("/view/javafx_js.html").toExternalForm();
mWebview.getEngine().load(myUrl);
WebEngine webEngine = mWebview.getEngine();
mWebview.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
@Override
public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
JSObject win = (JSObject) webEngine.executeScript("window"); // 获取js对象
win.setMember("app", new JavaApp()); // 然后把应用程序对象设置成为js对象
webEngine.executeScript("changeText()");// 直接执行html中的js脚本
}
}
});
}
public class JavaApp {
public void exit() {
Platform.exit();
}
public void login() {
System.out.println("login...");
}
}
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSON example</title>
<script language="javascript">
function changeText() {
document.getElementById('boldStuff').innerHTML = 'Fred Flinstone';
}
</script>
</head>
<body>
<p>
Welcome to the site <b id='boldStuff'>dude</b>
</p>
<input type='button' onclick='changeText()' value='Change Text' />
<input type='button' onclick='app.exit()' value='退出系统平台' />
<p>
<a href="#" onclick="app.exit()">Exit the Application</a>
</p>
<p>
<a href="#" onclick="app.login()">Login</a>
</p>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import com.jfoenix.controls.JFXNodesList?>
<?import javafx.scene.web.WebView?>
<?import javafx.geometry.Insets?>
<AnchorPane fx:id="root" xmlns:fx="http://javafx.com/fxml/1"
xmlns="http://javafx.com/javafx/2.2">
<children>
<StackPane prefHeight="150.0" prefWidth="200.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<WebView fx:id="mWebview"></WebView>
</StackPane>
</children>
</AnchorPane>