javafx - 没有显示启动画面

时间:2015-04-04 04:11:09

标签: java javafx

开发此应用程序,我需要显示3秒的启动画面。问题是启动窗口是空白三秒钟,只有主屏幕出现在屏幕上后才会显示启动图像。任何帮助将非常感激。 继承我的代码

class Launcher extends Application {
    @Override
    public void start(Stage stage) {
        Pane splashLayout = new VBox();
        ImageView splashImage = new ImageView(new Image(getClass().getResourceAsStream("splash/splash.png")));
        splashLayout.getChildren().add(splashImage);
        Scene scene = new Scene(splashLayout, Color.TRANSPARENT);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
        Thread.sleep(3000); // wait for three seconds.
        Window window = new Window(); // main stage
        window.show();
        stage.hide();
    }
}

现在问题是在显示Window阶段后显示启动图像。

1 个答案:

答案 0 :(得分:2)

从不在JavaFX应用程序线程上调用sleep - 它所做的只是挂起你的UI(这是代码中发生的事情)。

改为使用PauseTransition

splashStage.show();
PauseTransition pause = new PauseTransition(Duration.seconds(3_000));
pause.setOnFinished(event -> {
        Stage mainStage = new Stage();
        mainStage.setScene(createMainScene());
        mainStage.show();
        splashStage.hide();
});
pause.play();

另外,请勿致电new Window()。改为调用new Stage() - 阶段比Windows更具功能,没有真正的理由放弃该功能并使用Window。


有时,你想在显示启动画面时做一些工作(一些I / O,一个计算密集型的任务,加载带有饼的hobbits,找到矮人,等等),在这种情况下你可以使用JavaFX并发工具如splash screen sample中所示。