javaFX与js交互

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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() {
			@Override
			public void changed(ObservableValue 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...");
		}
	}

}





JSON example



	

Welcome to the site dude

Exit the Application

Login













	
		
			
		
	

转载于:https://my.oschina.net/zhanggongming/blog/735438

你可能感兴趣的:(javaFX与js交互)