JavaFx控件WebView基础使用

Fxml文件






	

Action文件

package com.action.layout;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;

public class MainAction implements Initializable {

    @FXML BorderPane borderPane;
    @FXML WebView centerWebView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        center();
    }

    public void center() {
        //获取Engine
        final WebEngine webEngine = centerWebView.getEngine();
        //获取当前Java版本
        System.out.println("Java Version:"+System.getProperty("java.runtime.version"));
        //获取当前JavaFx版本
        System.out.println("JavaFXersion:"+System.getProperty("javafx.runtime.version"));
//获取当前系统版本
        System.out.println("OS:"+System.getProperty("os.name")+","+System.getProperty("os.arch"))
        //获取WebKit内核版本
System.out.println("User Agent:"+centerWebView.getEngine().getUserAgent());
//获取页面路径
String mainUrl = getClass().getResource("/pages/layout/index.html").toExternalForm();
//加载方法接口
webEngine.getLoadWorker().stateProperty().addListener((ObservableValue ov, State oldState, State newState) -> {
                //判断页面是否加载完毕
			if (newState == State.SUCCEEDED) {
				JSObject win = (JSObject) webEngine.executeScript("window");
				
				win.setMember("employeeAction", employeeAction);	//人员
			}
		});
//获取页面alert弹窗和重新加载页面
centerWebView.getEngine().setOnAlert((WebEvent wEvent) -> {
                //打印页面弹窗
	  	      System.out.println("Alert Event  -  Message:  " + wEvent.getData());
	  			if(wEvent.getData().equals("Awesome123123")) {
//重新加载页面
	  				webEngine.load(getClass().getResource("/pages/layout/test.html").toExternalForm());
	  			}
	  			if(wEvent.getData().equals("123")) {
	  				webEngine.load(getClass().getResource("/pages/layout/demo.html").toExternalForm());
	  			}
	  	    });
//初始化
webEngine.load(mainUrl);
    }
}

前台页面 



    
    
	
	
	
	
        
        
    
    
    	

Employee


仅供参考

你可能感兴趣的:(JAVAFX)