带有弹簧启动器的Java Fx Splash屏幕

时间:2019-01-02 15:58:43

标签: java spring-boot javafx javafx-11

JavaFX 11和Spring Boot 2.0。

我想显示一个启动屏幕,直到Spring初始化所有必需的bean,并在spring.run()中关闭启动阶段(或至少在x秒后)。这样就可以连接到数据库,从而创建POJO等。但是当我尝试在FX线程启动之前显示我的启动屏幕时,它会抛出:

Caused by: java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = main()

我什至尝试了Platform.runLater(),但还是没有解决。有没有解决此问题的方法?谢谢。

public class StartUp extends Application{
    public static void main(String[] args) {
        loadSplashScreen();
        appContext = SpringApplication.run(StartUp.class);
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        stage.setScene(new Scene(root, 300, 275));
        stage.show();
    }

    static void loadSplashScreen() {
        Stage splashStage = new Stage();
        try {
            BorderPane splashPane = FXMLLoader.load(getClass().getResource("splash.fxml"));
            Scene splashScene = new Scene(splashPane);
            splashStage.setScene(splashScene);
            splashStage.show();
            setFadeInOut(splashPane, splashStage);
        } catch (IOException e) {
            e.printStackTrace();
        }
}

    static void setFadeInOut(Parent splashScene, Stage splashStage) {
        FadeTransition fadeIn = new FadeTransition(Duration.seconds(3), splashScene);
        fadeIn.setFromValue(0);
        fadeIn.setToValue(1);
        fadeIn.setCycleCount(1);

        FadeTransition fadeOut = new FadeTransition(Duration.seconds(3), splashScene);
        fadeOut.setFromValue(1);
        fadeOut.setToValue(0);
        fadeOut.setCycleCount(1);
        fadeIn.play();

        fadeIn.setOnFinished((e) -> fadeOut.play());
        fadeOut.setOnFinished((e) -> splashStage.close());
    }
}

1 个答案:

答案 0 :(得分:0)

在您的代码中,有一个名为loadSplashScreen()的方法,您在Application.launch()之前调用了该方法。将是对launch方法的调用,它将启动JavaFX线程,这就是您的loadSplashScreen()方法失败的原因,即,调用该方法时JavaFX线程甚至都没有启动。

在尝试使用Spring Boot启动JavaFX之前,您可能想看一下{Pre3er}上的Oracle教程,以了解如何理解基本示例。

尽管我还没有从Spring Boot启动JavaFX,但是我在OSGi捆绑软件中也做了类似的工作,您可能想看看我的FlexFx GitHub存储库here,这可能会给您一些有关如何在Spring Boot中使用预加载器,但请注意,我目前无法在项目中显示启动画面。

最后,您的问题将发生在JavaFX-8、9或10上。它不特定于JavaFX-11。