java - 从 java swing 框架使用 gmapsfx 库运行 javafx 类

标签 java swing google-maps javafx

我想运行 javaFX 类 (MainApp.java),其中包含使用 Java swing 应用程序中的 GMapsFX 库 ( http://rterp.github.io/GMapsFX/ ) 的 googlemap。

它在单击 jframe 中的菜单项后运行 - 执行 MainApp.main(args);

map 启动但卡住应用程序的其余部分

我如何运行这个类来防止卡住?

下面是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));
}    
}

现在怎么办?

最佳答案

您无法从 Swing 启动 JavaFX 应用程序

原因是 JavaFX 与您的系统紧密相连,它使用线程进行 OpenGL 访问,并包含大量 native 代码以提高性能。

正确的解决方案是打开一个 Swing 窗口,并在其中嵌入 JavaFX 小部件

有关 JavaFX 和 Swing 的更多详细信息,请参阅:

http://docs.oracle.com/javase/8/javafx/interoperability-tutorial/swing-fx-interoperability.htm#CHDIEEJE

要将 JavaFX 嵌入到 Swing 中,请使用 javafx.embed.swing.JFXPanel

关于java - 从 java swing 框架使用 gmapsfx 库运行 javafx 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31967823/

相关文章:

java - JTable 政策。表格下方的 gridBagLayout 面板上的空白空间。

java - JTable基于一列宽度的水平滚动条

ios - Google map 信息窗口根据内部内容调整大小

javascript - 尽管有事件监听器,Google map API 仍未调整大小 - 通过 AJAX 创建

javascript - 新生成的 ul li 输入值没有被插入航点数组/不使用谷歌地图位置自动完成

java - 结果集获取单列中的所有行值

java - 尝试在 shell 脚本中同时运行 hadoop MapReduce 命令和 linux 命令

JavaFX - 如何创建(不可见的)WebView 的快照/屏幕截图

java - 如何在 Spring 中关闭 VelocityViewResolver 错误?

Java Swing : JTextField doesn't lose focus as expected