我想运行javaFX类(MainApp.java)包含一个googlemap,使用来自java swing app的GMapsFX库(http://rterp.github.io/GMapsFX/)。
在点击jframe中的菜单项后运行 - 执行MainApp.main(args);
地图开始但会冻结应用程序的其余部分
如何运行此类以防止冻结?
下面是MainApp.java类
package com.lynden.gmapsexampleapp;
import com.lynden.gmapsfx.GoogleMapView;
import com.lynden.gmapsfx.MapComponentInitializedListener;
import com.lynden.gmapsfx.javascript.object.GoogleMap;
import com.lynden.gmapsfx.javascript.object.LatLong;
import com.lynden.gmapsfx.javascript.object.MapOptions;
import com.lynden.gmapsfx.javascript.object.MapType;
import com.lynden.gmapsfx.javascript.object.Marker;
import com.lynden.gmapsfx.javascript.object.MarkerOptions;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainApp extends Application implements MapComponentInitializedListener {
GoogleMapView mapView;
GoogleMap map;
@Override
public void start(Stage stage) throws Exception {
//Create the JavaFX component and set this as a listener so we know when
//the map has been initialized, at which point we can then begin manipulating it.
mapView = new GoogleMapView();
mapView.addMapInializedListener(this);
Scene scene = new Scene(mapView);
stage.setTitle("JavaFX and Google Maps");
stage.setScene(scene);
stage.show();
}
@Override
public void mapInitialized() {
//Set the initial properties of the map.
MapOptions mapOptions = new MapOptions();
mapOptions.center(new LatLong(47.6097, -122.3331))
.mapType(MapType.ROADMAP)
.overviewMapControl(false)
.panControl(false)
.rotateControl(false)
.scaleControl(false)
.streetViewControl(false)
.zoomControl(false)
.zoom(12);
map = mapView.createMap(mapOptions);
//Add a marker to the map
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position( new LatLong(47.6, -122.3) )
.visible(Boolean.TRUE)
.title("My Marker");
Marker marker = new Marker( markerOptions );
map.addMarker(marker);
}
public static void main(String[] args) {
launch(args);
}
}
我不知道如何在JavaFX线程中嵌套整个类。 我有这样的事情:
public class NewFXSwingMain extends JApplet {
private static final int JFXPANEL_WIDTH_INT = 300;
private static final int JFXPANEL_HEIGHT_INT = 250;
private static JFXPanel fxContainer;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
}
JFrame frame = new JFrame("JavaFX 2 in Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new NewFXSwingMain();
applet.init();
frame.setContentPane(applet.getContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
applet.start();
}
});
}
@Override
public void init() {
fxContainer = new JFXPanel();
fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
add(fxContainer, BorderLayout.CENTER);
// create JavaFX scene
Platform.runLater(new Runnable() {
@Override
public void run() {
createScene();
}
});
}
private void createScene() {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
fxContainer.setScene(new Scene(root));
}
}
现在是什么?
答案 0 :(得分:1)
您无法从Swing中启动JavaFX Application
。
原因是JavaFX与您的系统紧密相连,它使用线程进行OpenGL访问,并包含大量本机代码以提高性能。
正确的解决方案是打开一个swing窗口,并在那里嵌入一个JavaFX小部件。
有关JavaFX和Swing的更多详细信息,请参阅:
要将JavaFX嵌入到Swing中,请使用javafx.embed.swing.JFXPanel
。
答案 1 :(得分:0)
希望帮助有这个问题的人:
import com.lynden.gmapsfx.GoogleMapView;
import com.lynden.gmapsfx.javascript.object.GoogleMap;
import com.lynden.gmapsfx.javascript.object.LatLong;
import com.lynden.gmapsfx.javascript.object.MapOptions;
import com.lynden.gmapsfx.javascript.object.MapTypeIdEnum;
import java.awt.Dimension;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javax.swing.JFrame;
public class SwingGoogleMaps {
private final JFrame frame;
private final JFXPanel jfxPanel;
private Scene scene;
private GoogleMapView mapComponent;
private GoogleMap map;
public SwingGoogleMaps() {
frame = new JFrame("Hello Swing GMapsFX");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfxPanel = new JFXPanel();
jfxPanel.setPreferredSize(new Dimension(600, 600));
Platform.runLater(() -> {
mapComponent = new GoogleMapView();
mapComponent.addMapInializedListener(() -> {
LatLong center = new LatLong(-25.343924, 131.033114);
MapOptions options = new MapOptions()
.center(center)
.mapMarker(true)
.zoom(12)
.overviewMapControl(false)
.panControl(false)
.rotateControl(false)
.scaleControl(false)
.streetViewControl(false)
.zoomControl(false)
.mapType(MapTypeIdEnum.SATELLITE);
map = mapComponent.createMap(options);
});
mapComponent.setPrefSize(600, 600);
scene = new Scene(mapComponent);
jfxPanel.setScene(scene);
});
frame.getContentPane().add(jfxPanel);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
new SwingGoogleMaps();
});
}
}