如何重新启动外部JavaFX程序?即使JavaFX程序以Platform.Exit结束,启动也会阻止此操作 [英] How do I start again an external JavaFX program? Launch prevents this, even if the JavaFX program ended with Platform.Exit

查看:1402
本文介绍了如何重新启动外部JavaFX程序?即使JavaFX程序以Platform.Exit结束,启动也会阻止此操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MainProject(Java 8)中,我启动了一个JavaFX 8类。

From my MainProject (Java 8) i starts a JavaFX 8 Class.

public void startFX() {
    if (isRestartPrintModul() == true) {
        fxMain.init();
    } else {
        setRestartPrintModul(true);
        fxMain.main(new String[] {"ohne"});
    }
}

这是我的FXMain:

This is my FXMain:

        package quality;

    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.event.ActionEvent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Modality;
    import javafx.stage.Stage;
    import javafx.stage.WindowEvent;

    /**
     *
     * @author pu_laib
     */
    public class FXMain extends Application {

        private static Stage primaryStage;

        @Override
        public void init() {
            Platform.setImplicitExit(false);
            if (getPrimaryStage() != null) {
                getPrimaryStage().show();
            } else {
            }
        }

        @Override
        public void start(Stage primaryStage) {
            setPrimaryStage(primaryStage);
            // -> Applicationerror: getPrimaryStage().initModality(Modality.NONE);
            // -> Applicationerror: getPrimaryStage().initModality(Modality.APPLICATION_MODAL);
            Button btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction((ActionEvent event) -> {
                System.out.println("Hello World!");
            });

            StackPane root = new StackPane();
            root.getChildren().add(btn);

            Scene scene = new Scene(root, 300, 250);

            getPrimaryStage().setTitle("Hello World!");
            getPrimaryStage().setScene(scene);
            getPrimaryStage().show();
            this.primaryStage.setOnCloseRequest((WindowEvent event) -> {
                Platform.exit();
            });

        }

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

        public Stage getPrimaryStage() {
            return primaryStage;
        }

        public void setPrimaryStage(Stage primaryStage) {
            this.primaryStage = primaryStage;
        }

    }

无法调用再次从我的MainProject打印模块,虽然它在我看来是关闭的。

It is not possible to call the Print module from my MainProject again, although it is closed in my opinion.

一旦PrintModul模块完成,启动就不记得它之前运行了,对吧?

Once the PrintModul module is completed, launch can not remember that it ran before, right?

有什么问题?

谢谢。

推荐答案

Application :: launch(args)方法状态:


不得多次调用它或抛出异常。

It must not be called more than once or an exception will be thrown.

所以:


  1. 只需调用一次启动。

  2. 调用 Platform.setImplicitExit(fa lse) 以便JavaFX运行时将继续运行,即使所有阶段都已关闭。

  3. 一旦JavaFX应用程序完成其工作,请不要不要调用 Platform.exit() ,但即使您没有积极使用它,也要让JavaFX平台继续运行。

  4. 而不是尝试启动再次申请,调用 start() 应用程序的方法(或您在接受要传递的参数的应用程序上提供的其他一些公共方法)运行应用程序第二次或更多次(如果需要,可以实例化一个新阶段以传递给start方法)。

  5. 完成所有工作后,调用 Platform。 exit()当时要清理关闭JavaFX系统。

  1. Just call launch once.
  2. Invoke Platform.setImplicitExit(false) so that the JavaFX runtime will continue running even if all stages are closed.
  3. Once the JavaFX application has finished doing its job, don't invoke Platform.exit(), but let the JavaFX platform continue running even when you aren't actively using it.
  4. Instead of trying to launch the application again, invoke the start() method of the application (or some other public method you provide on the application which accepts the parameters you want to pass) to "run" the application a second or more times (perhaps instantiating a new stage to pass to start method if needed).
  5. Once all of the work is complete, invoke Platform.exit() at that time to cleanly shutdown the JavaFX system.






您的另一个选择是启动新流程而不是在与MainProject相同的进程中运行JavaFX应用程序,但是,一般来说,我建议使用上面概述的方法而不是创建新进程。


Your other option would be to launch a new process instead of running the JavaFX applications inside the same process as your MainProject, but, in general, I'd recommend the approach outlined above rather than creating new processes.

这篇关于如何重新启动外部JavaFX程序?即使JavaFX程序以Platform.Exit结束,启动也会阻止此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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