gpt4 book ai didi

java - 将信息从 JavaFX 传递到 Javascript

转载 作者:行者123 更新时间:2023-12-02 05:55:05 24 4
gpt4 key购买 nike

我正在尝试将信息(纬度、经度)传递到 html webview 中的谷歌地图。问题是该应用程序无法在 netbeans 中启动。它返回:

Executing C:\Users\Carlos\Documents\NetBeansProjects\OpenPilot\dist\run1883323097\OpenPilot.jar using platform C:\Program Files\Java\jdk1.8.0_05\jre/bin/java Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894) at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56) at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158) at java.lang.Thread.run(Thread.java:745) Caused by: javafx.fxml.LoadException: unknown path

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425) at openpilot.OpenPilot.start(OpenPilot.java:29) at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837) at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335) at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301) at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39) at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112) ... 1 more Caused by: netscape.javascript.JSException: TypeError: 'undefined' is not a function at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:128) at com.sun.webkit.WebPage.twkExecuteScript(Native Method) at com.sun.webkit.WebPage.executeScript(WebPage.java:1410) at javafx.scene.web.WebEngine.executeScript(WebEngine.java:934) at openpilot.FXMLDocumentController.initialize(FXMLDocumentController.java:74) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) ... 13 more Exception running application openpilot.OpenPilot Java Result: 1

代码如下:

public class FXMLDocumentController implements Initializable {

@FXML public WebView Map;





@Override
public void initialize(URL url, ResourceBundle rb) {

URL MapURL = getClass().getResource("map.html");
WebEngine WebEngine = Map.getEngine();
WebEngine.load(MapURL.toExternalForm());
String test = "40.3130432088809";
WebEngine.executeScript("getCoordinates('40.3130432088809')");

}

}

这里是带有 map 的 html:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var latitude;
var longitude;
function getCoordinates(latitude){
this.latitude = latitude;

}
var map;
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(latitude, -2.900390625)
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);

google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});


}
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});

google.maps.event.addListener(marker, 'rightclick', function() {
deleteMarker(marker);
});

}

function deleteMarker(marker) {
marker.setMap(null);
}
google.maps.event.addDomListener(window, 'load', initialize);

</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>

最佳答案

您几乎肯定会在页面加载之前尝试执行 JavaScript。您需要指定一个处理程序,以便在 Web 引擎完成加载时执行脚本。

@Override
public void initialize(URL url, ResourceBundle rb) {

URL MapURL = getClass().getResource("map.html");
WebEngine WebEngine = Map.getEngine();
WebEngine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
if (newState == State.SUCCEEDED) {
WebEngine.executeScript("getCoordinates('40.3130432088809')");
}
});
WebEngine.load(MapURL.toExternalForm());

}

关于java - 将信息从 JavaFX 传递到 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23181678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com