JavaFX从另一个应用程序启动Application standalone OR [英] JavaFX launch Application standalone OR from another application

查看:204
本文介绍了JavaFX从另一个应用程序启动Application standalone OR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下场景:

JavaFxMainApp
JavaFXUpdaterApp

JavaFxMainApp JavaFXUpdaterApp

两者都是带有GUI的JavaFX应用程序void main()方法。

Both are JavaFX applications with a GUI and a void main() method.


  1. Updater必须能够通过访问
    JavaFxMainApp来启动JavaFXMainApp .jar只知道主要课程 - >它只能!调用
    main()。

  1. The Updater has to be able to start the JavaFXMainApp by accessing the JavaFxMainApp.jar ONLY knowing about the main class -> it can only! call main().

JavaFxMainApp也必须能够通过启动
main()来自行运行。

The JavaFxMainApp has to also be able to run on its own, by starting main().

我无法启动多个VMS,这两个应用程序无法通信。

I cannot start multiple VMS and the two apps have no means of communication.

问题是:

每个JVM只能执行一次Application.launch()。

Application.launch() can only be executed once per JVM.

启动应用的标准javaFx方式:

Updater + MainApp
    public static void main(String[] args) {
        launch(args);
    }

这里不可能满足要求1)。因为两个main()方法都调用launch()。

Here it is impossible to fullwill Requirement 1). As both main() methods call launch().

我找到的第二种方法是:

Updater + MainApp
 public static void main(String[] args) {
        Application app2 = JavaFxMainApp.class.newInstance(); 
        Stage anotherStage = new Stage();
        app2.start(anotherStage);
}

首先,我失去了通过args的可能性,但我可以忍受因为他们不会被使用。
这里的主要问题是,如果JVM已经运行了JavaFx线程,则此代码仅起作用,因此它要求在之前的某个时刻在JVM中调用launch()。情况并非如此,因为两个调用都不再是launch()。

First off, i lose the possibility to pass args, but i can live with that as they would not be used anyways. Major Problem here is, that this code ONLY works, if the JVM already has a JavaFx Thread running, hence it requires that launch() has been called in the JVM at some point before. This is not the case as none of the two calls launch() anymore.

混合方式:

Updater calls launch(), MainApp takes the second approach

要求2)无法实现,现在启动MainApp而不启动更新程序是不可能的。

Requirement 2) cannot be fulfilled, als starting MainApp without launcher Updater is impossible now.

另一个想法是脏try launch()catch( ) - >在两个应用程序中尝试第二种方法(),但是这两种应用程序结合在一起并使设置不那么灵活。

Another idea i had where dirty "try launch() catch()-> try second approach() in both apps, but that couples both apps and make the setup less flexible.

有没有办法在不必覆盖的情况下完成此操作JavaFxs的LauncherImpl或Application类是否满足这些需求?

Is there a way to accomplish this without having to override JavaFxs' LauncherImpl or Application classes to fit these needs?

推荐答案

你能做到这样的事情:

public class MainApp extends Application {

    private Parent uiContent ;

    public static final double DEFAULT_WIDTH = 800 ;
    public static final double DEFAULT_HEIGHT = 600 ;

    public Parent getContent() {
        if (uiContent == null) {
            uiContent = initializeUI();
        }
        return uiContent ;
    }

    public Scene createScene() {
        return new Scene(getContent(), DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    public void initializeAndShowStage(Stage stage) {
        stage.setScene(createScene());
        stage.show();
    }

    private Parent initializeUI() {
        // probably wise to check we are on the FX Application thread here...
        Pane root = ... ;
        // build ui....
        return root ;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        initializeAndShowStage(primaryStage);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

public class UpdaterApp extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        // whatever you need to launch the updater app here...

    }

    // invoke from the FX Application Thread to "start" main app:
    private void showMainApp(Stage stage) {
        MainApp app = new MainApp();
        app.initializeAndShowStage(stage);
    }

    private void showMainApp() {
        showMainApp(new Stage());
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这将是最受欢迎的方法。如果你的要求迫使你打电话给 main ,那么你可以尝试这样的事情:

This would be the far preferred approach. If you have requirements that force you to call main, then you could try something like this:

public class MainApp extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        // .... build UI etc
    }

    public static void main(String[] args) throws Exception {
        if (Platform.isFXApplicationThread()) {
            Stage someStage = new Stage();
            MainApp app = new MainApp();
            app.start(stage);
        } else {
            launch(args);
        }
    }
}

那么你的更新程序应用程序可以只是从FX应用程序线程调用 MainApp()。main(new String [0]));

Then your updater app can just call MainApp().main(new String[0])); from the FX Application Thread.

这感觉有点像黑客。

这篇关于JavaFX从另一个应用程序启动Application standalone OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆