JavaFx控件WebView基础使用

Fxml文件

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.web.WebView?>

<BorderPane fx:id="borderPane" xmlns:fx="http://javafx.com/fxml"  fx:controller="com.action.layout.MainAction">
	<center>
		<WebView fx:id="centerWebView" ></WebView>
	</center>
</BorderPane>

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<? extends State> ov, State oldState, State newState) -> {
                //判断页面是否加载完毕
			if (newState == State.SUCCEEDED) {
				JSObject win = (JSObject) webEngine.executeScript("window");
				
				win.setMember("employeeAction", employeeAction);	//人员
			}
		});
//获取页面alert弹窗和重新加载页面
centerWebView.getEngine().setOnAlert((WebEvent<String> 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);
    }
}

前台页面 


<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type='text/javascript' src="jquery-1.12.1.min.js"></script>
	<script type='text/javascript' src="jquery-1.7.1.js"></script>
	<script type='text/javascript' src="json2.js"></script>
	
        <!-- Visibility toggle script -->
        <script type="text/javascript">
        function check(userName,password){
        	var result = employeeAction.checkPassword(userName,password);
        	document.getElementById("checkResult").value=result;
        }
     
        function updatePassword(){
        	var pass = document.getElementById("password").value;
        	document.getElementById("checkResult").value=pass;
        	var a = employeeAction.resetPassword("root",pass);
        	document.getElementById("password").value=a;
        }
        function insert(){
        	var userName = document.getElementById("userName").value;
        	var password = document.getElementById("userPassword").value;
        	var a = employeeAction.insertEmployee(userName,password);
        	document.getElementById("password").value=a;
        }
        function query(){
        	var str = employeeAction.queryAll();
        	var jsondata = JSON.parse(str);
        	alert(jsondata);
        	document.getElementById("password").value=jsondata[0].userName;
        }
        </script>
    </head>
    <body>
    	<p>Employee</p>
    	<input type="text" value="" id="checkResult">
    	<input type="button" value="验证密码" onclick="check('root','root');">
    	
    	<input type="text" value="" id="password">
    	<input type="button" value="重置" onclick="updatePassword();">
    	
    	<input type="text" value="" id="userName">
    	<input type="text" value="" id="userPassword">
    	<input type="button" value="添加" onclick="insert();">
    	
    	<br>
    	<input type="button" value="查询" onclick="query();">
    	
    	
    </body>
</html>

仅供参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值