记JavaFx中的那些坑

WebView加载相对路径下的HTML页面

1
webview.engine.load(javaClass.getResource("web/index.html").toExternalForm())

重点在于toExternalForm()方法

WebView添加Javascript交互接口

1
2
3
4
5
6
7
webview.engine.loadWorker.stateProperty()
.addListener { _: ObservableValue<out Worker.State>, _: Worker.State, newValue: Worker.State ->
if (newValue == Worker.State.SUCCEEDED) {
val win = wvContent.engine.executeScript("window") as JSObject
win.setMember("app", AppEnv())
}
}

在HTML页面中调用Java端代码应该调用app.doSomething()

1
2
3
4
inner class AppEnv {
fun doSomething() {
}
}

WebView添加样式

1
webview.engine.userStyleSheetLocation = javaClass.getResource("scrollbar_style.css").toExternalForm()

scrollbar_style.css是相对此代码文件同一目录下的