我認爲JxBrowser 6.1中的JavaScript Java Bridge API存在問題。我嘗試了一個非常簡單的代碼來調用Javascript中的java類的方法。這裏是代碼。在java中,java
被設置爲javascript window
對象的屬性,該對象爲Events
類的實例,然後加載html。在html中,我只需要撥打Events
類的Close
方法即可。但是,當我點擊Close
按鈕,JAVA Close
功能不會被調用,並從JxBrowser控制檯有一條消息說:JxBrowser 6.1 JavaScript Java Bridge API不起作用
遺漏的類型錯誤:無法讀取屬性未定義
「關閉」,這意味着即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
請更正我如果我錯了。 在此先感謝。
謝謝,我這樣做了,它解決了這個問題一些如何,但有另一種錯誤說:'未捕獲IllegalAccessException:類com.teamdev.jxbrowser.chromium.JSContext不能訪問類RezRem成員使用修飾符「public」「'的事件。爲了這個錯誤,我在'public'修飾符的另一個文件中聲明瞭'Events'類,現在沒有問題。 –
JavaScript只能訪問公共類和方法。 //jxbrowser.support.teamdev.com/solution/articles/9000013062-calling-java-from-javascript – Vladimir
最近添加註釋,謝謝;) –