JavaFx实现WebView 使用js调用java代码:场景是调用java代码打开文件夹
- html
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Show</title>
<link rel="stylesheet" href="css/show.css">
<script src="js/common.base.js"></script>
<script src="js/chat.js"></script>
</head>
<body>
<div>
<div class="message">
<dl class="item right"><p class="time"><i>2019-10-10 18:08:23</i></p>
<dt><img src="image/headpic.png" alt=""></dt>
<dd>
<div class="content"><i class="arrow"></i>
<meta charset="utf-8">
<title></title> 666666
</div>
</dd>
</dl>
</div>
<div class="file">
<dl class="item left">
<p class="time">
<i>2019-10-10 18:08:23</i>
</p>
<dt><img src="image/headpic.png" alt=""></dt>
<dd>
<div class="content">
<i class="arrow"></i>
<meta charset="utf-8">
<title></title>
<div class="fileImage">
<img src="image/file.png" style="float: left" alt="">
</div>
<div class="fileName" >我是文件名</div>
<div class="fileSize" >我是文件大小</div>
<!-- String a = "onclick=\"oim.openUrl('$1');\"";-->
<div class="option"><a class="fileOption" id="fileOption1" onclick="changeText1()" >打开文件</a> <a id="fileOption" class="fileOption" onclick="changeText()" >打开所在文件夹</a></div>
<div class="pp" style="color: #fff;">
</div>
</div>
</dd>
</dl>
</div>
</div>
</body>
<script >
var stone = 'test';
function changeText() {
alert("111");
stone.openDir();
document.getElementById('fileOption').innerHTML="RUNOOB";
}
function changeText1() {
alert("111");
document.getElementById('fileOption1').innerHTML="RUNOOB";
stone.openDir();
}
</script>
</html>
- java方法
package com.mandela;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import netscape.javascript.JSObject;
public class JavaTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
File directory = new File("");
String dir=directory.getAbsoluteFile()+"\\show\\show.html";
String absolutePath = dir;
System.out.println("xxx"+absolutePath);
System.out.println("initializeHtml "+absolutePath);
absolutePath = absolutePath.replace("\\", "/");
if (absolutePath.startsWith("/")) {
webEngine.load("file:" + absolutePath);
} else {
webEngine.load("file:/" + absolutePath);
}
webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends Worker.State> ov, Worker.State oldState, Worker.State newState) {
if (newState == Worker.State.SUCCEEDED) {
//System.out.println("123");
JSObject win = (JSObject) webEngine.executeScript("window"); // 获取js对象
win.setMember("stone", new HelloWorld()); // 然后把应用程序对象设置成为js对象
//webEngine.executeScript("changeText()");
// 直接执行html中的js脚本
}
}
});
// webEngine.getLoadWorker().stateProperty()
// .addListener((obs, oldValue, newValue) -> {
// if (newValue == Worker.State.SUCCEEDED) {
// webEngine.executeScript("changeText()");
// JSObject jsobj = (JSObject) webEngine.executeScript("window");
// jsobj.setMember("stone", new HelloWorld());
// }
// });
//webEngine.load("http://www.www.w3cschool.cn");
Group root = new Group();
root.getChildren().add(webView);
Scene scene = new Scene(root, 600, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
public static class HelloWorld {
public void openDir() {
System.out.println("打开目录");
File directory = new File("");
String dir=directory.getAbsolutePath();
System.out.println("打开的文件夹目录:"+dir);
try {
Runtime.getRuntime().exec("cmd /c explorer " +dir);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}