2015-04-04 250 views
1

我開發這個應用程序,我需要顯示啓動畫面3秒。問題是飛濺窗口在三秒鐘內是空白的,並且飛濺圖像僅在屏幕上出現主舞臺後才顯示。任何幫助將非常感激。 繼承人我的代碼javafx - 啓動畫面不顯示

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(); 
    } 
} 

現在這裏的問題是顯示窗口舞臺後顯示的開機畫面。

+0

更新:當我把這個類變成Preloader類(即擴展Preloader類)並註釋掉這兩行時,閃屏圖像顯示出來。 窗口窗口=新窗口(); // window.show();// – bazi 2015-04-04 04:31:45

回答

2

從來沒有在JavaFX應用程序線程上調用睡眠 - 它只會掛起您的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,計算密集型任務,負載霍比特人與餡餅,發現矮人,等等),在這種情況下,你可以使用如此splash screen sample中演示的JavaFX併發實用程序。

+0

謝謝jewelsea :)這有助於.. – bazi 2015-04-05 14:39:47