简体   繁体   English

从JFrame启动JavaFxApplication

[英]Launching JavaFxApplication from JFrame

I have written application in JavaFx that shows spinning RGB cube. 我已经用JavaFx编写了显示旋转RGB立方体的应用程序。 I wont to run this application after choosing option in JMenuBar in JFrame. 在JFrame的JMenuBar中选择选项后,我将不会运行此应用程序。 I tryed something like this: 我尝试过这样的事情:

if(e.getSource()== showRGBCube){
        cubedemo.CubeDemo.launch();
    }

But it throws: 但是它抛出:

java.lang.RuntimeException: Error: class grafika1.MyMenu is not a subclass of javafx.application.Application

Thanks for advices. 感谢您的建议。

You can do 你可以做

Application.launch(CubeDemo.class);

but note you can only execute launch(...) once during the lifetime of the JVM (so if the user selects your menu item a second time, it will throw an exception). 但请注意,您只能在JVM的生命周期内执行一次launch(...) (因此,如果用户第二次选择菜单项,它将抛出异常)。

If you want to display a JavaFX user interface in a swing application, the supported way to do that is with a JFXPanel , which you can place into a JFrame directly. 如果要在swing应用程序中显示JavaFX用户界面,则支持的方法是使用JFXPanel ,您可以将其直接放入JFrame So you would do something like: 因此,您将执行以下操作:

public class CubeDemo {

    private final Parent view ;

    public CubeDemo() {
        view = new BorderPane(); // or any other kind of pane...
        // set up ui, register event handlers, etc etc
    }

    public Parent getView() {
        return view ;
    }
}

You can still make a standalone JavaFX application (if you need) with: 您仍然可以使用以下方法制作独立的JavaFX应用程序:

public class CubeDemoApp extends Application {
    @Override
    public void start(Stage primaryStage) {
        CubeDemo cubeDemo = new CubeDemo();
        Scene scene = new Scene(cubeDemo.getView());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

and in your Swing application you can do: 在您的Swing应用程序中,您可以执行以下操作:

JFXPanel jfxPanel = new JFXPanel();
JFrame frame = new JFrame();
frame.add(jfxPanel);
frame.setSize(...);
Platform.runLater(() -> {
    CubeDemo cubeDemo = new CubeDemo();
    Scene scene = new Scene(cubeDemo.getView());
    jfxPanel.setScene(scene);
});
frame.setVisible(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM