我有一个项目需要在桌面 Java 应用程序中嵌入谷歌地图。经过一些研究,我发现 Java FX 确实提供了此功能,并着手编写示例应用程序作为 PoC。该应用程序工作,一切都很好。升级到新的 7u7 java 版本后,我的代码不再像以前那样工作了。代码运行并显示网页,但没有向网页(谷歌地图)触发手势(平移/缩放)事件,并且由于某种原因,当我单击并拖动(好像平移)时,我得到了包含的异常,我无法理解。
我非常感谢有人在 JavaFX 2.2 应用程序中嵌入谷歌地图的任何见解。提前感谢您的任何帮助和/或建议
以下是我的应用程序代码/HTML 代码和引发的异常:
JavaFX 代码:
private Scene scene;
private MyBrowser myBrowser;
private LocationClickHandler handler;
private Vector<Button> buttons;
//private Moderator moderator;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
buttons = new Vector<Button>();
//moderator = new Moderator();
handler=new LocationClickHandler();
primaryStage.setTitle("java-buddy.blogspot.com");
myBrowser = new MyBrowser();
//moderator.registerAddIconListener(myBrowser);
VBox mainbox = new VBox();
mainbox.getChildren().add(myBrowser);
// mainbox.getChildren().add(generateEntitiesButtonBox());
// mainbox.getChildren().add(generateSensorsButtonBox());
scene = new Scene(mainbox, 1280, 760);
primaryStage.setScene(scene);
primaryStage.show();
}
class MyBrowser extends Region {
HBox toolbar;
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
public MyBrowser(){
webView.setMinSize(1280, 400);
final URL urlGoogleMaps = getClass().getResource("googlemaps2.html");
webEngine.load(urlGoogleMaps.toExternalForm());
getChildren().add(webView);
/*webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
@Override public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
JSObject win = (JSObject) webEngine.executeScript("window");
win.setMember("java", handler);
}
}
});*/
}
加载到 Web 视图中的 HTML:
<!DOCTYPE html>
<html>
<head>
<title>Java-Buddy: Google Maps</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<style>#mapcanvas { height: 500px; width: 100%}</style>
<script type="text/javascript">
var map;
var icon="icons/Tank.png";
var last_clicked;
var elliptical=0;
var sensors = new Array();
var sensors_to_targets = new Array();
var targets = new Array();
var target_paths = new Array();
var target_polys = new Array();
var counter =0;
//var marker;
function loadmap(){
var options = {
zoom: 16,
center: new google.maps.LatLng(51.507222, -0.1275),
mapTypeId: google.maps.MapTypeId.SATELLITE,
streetViewControl: false
}
map = new google.maps.Map(document.getElementById("mapcanvas"), options);
google.maps.event.trigger(map,'resize');
google.maps.event.addListener(map, 'click', function(event) {
java.saveLocation(event.latLng.lat(),event.latLng.lng());
});
}
</script>
</head>
<body onload="loadmap()">
<div id="mapcanvas"></div>
</body>
</html>
在 Web 视图中拖动时抛出的异常:
java.lang.NullPointerException: Clipboard.putContent: null data
at com.sun.javafx.tk.quantum.QuantumClipboard.putContent(QuantumClipboard.java:442)
at javafx.scene.input.Clipboard.setContent(Clipboard.java:226)
at com.sun.webpane.sg.UIClientImpl.confirmStartDrag(UIClientImpl.java:178)
at com.sun.webpane.platform.WebPage.confirmStartDrag(WebPage.java:861)
at javafx.scene.web.WebView$19.handle(WebView.java:1204)
at javafx.scene.web.WebView$19.handle(WebView.java:1201)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$DnDGesture.fireEvent(Scene.java:2627)
at javafx.scene.Scene$DnDGesture.process(Scene.java:2706)
at javafx.scene.Scene$DnDGesture.access$8700(Scene.java:2603)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3340)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3164)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3119)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1559)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2261)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:228)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Unknown Source)