【发布时间】:2016-05-21 15:20:01
【问题描述】:
我认为 JxBrowser 6.1 中的 JavaScript Java Bridge API 存在问题。我尝试了一个非常简单的代码来调用 Javascript 中的 java 类的方法。这是代码。在 java 中,java 被设置为 javascript window 对象上的属性到 Events 类的实例,然后加载 html。在 html 中,我只是调用 Events 类的 Close 方法。但是当我单击Close 按钮时,java Close 函数不会被调用,并且 JxBrowser 的控制台中有一条消息说:
未捕获的类型错误:无法读取未定义的属性“关闭”
这意味着window 对象的java 属性未定义。
Main.java:
public class Main extends Application {
private Browser browser;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Platform.setImplicitExit(false);
browser = new Browser();
JSValue window = browser.executeJavaScriptAndReturnValue("window");
window.asObject().setProperty("java", new Events());
BrowserView browserView = new BrowserView(browser);
StackPane pane = new StackPane();
pane.getChildren().add(browserView);
Scene scene = new Scene(pane, 330, 470);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setScene(scene);
primaryStage.show();
browser.loadURL(Main.class.getResource("templates/simple.html").toExternalForm());
}
}
class Events {
public void Close() {
System.out.println("close button clicked");
}
}
simple.html:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<button id="Close">Close</button>
<script>
document.getElementById('Close').onclick = function () {
window.java.Close();
}
</script>
</body>
</html>
这是我曾经这样做过的文章: https://jxbrowser.support.teamdev.com/support/solutions/articles/9000013062-calling-java-from-javascript
如果我错了,请纠正我。 提前致谢。
【问题讨论】: