javaFX与js交互
2016-08-20 13:57
1201 查看
1、javafx直接执行html中的js脚本
2、把java中的对象注入到html中,在html网页中调用java端的代码,执行javafx中的方法。
代码:
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>
相关文章推荐
- flash和js交互在项目中的应用
- ASP.Net一些知识点(关于.net与JS如何交互)
- php和js交互一例
- JSON进阶五-JS和WCF的交互
- C# WinForm程序如何与js交互
- JSP与JS交互问题 值传递
- JavaFX与后端服务器的交互(第一种方案)
- Flex中As与Js进行交互
- JS完成ListBox内容的交互实例
- 在.Net下使用Access 的日期类型 及与js的日历控件交互
- [js]javascript与剪贴板交互
- Js完成ListBox内容的交互实例
- c#和js的交互
- c#和js的交互(转)
- 推荐js实现商品分类到搜索栏友好提示(人机交互)第1/3页
- [js]javascript与剪贴板交互
- Flash与JS交互
- JSP与JS交互问题 值传递
- [收藏]C# WinForm程序如何与JS交互