2016-06-23 171 views
0

我遇到了may(fx)clipse應用程序的問題。我想在應用程序啓動時顯示啓動畫面。我成功創建了實現StartupProgressTrackerService的課程,並調用了我的stateReached方法。不過,我自己有javafx問題。我想用StageStyle.UNDECORATED創建舞臺。但是,當我調用stage.show()方法stage不會立即呈現並且在主窗口創建之後出現。它工作正常,例如與StageStyle.UTILITY。當我使用showAndWait()方法時,它也會正確呈現,但它會阻止我的應用程序加載,直到我關閉舞臺。JavaFX UNDECORATED階段未顯示

這裏是我的代碼:

public class MyStartupProgressTrackerService implements StartupProgressTrackerService { 

    private Stage stage; 

    public MyStartupProgressTrackerService() { 

    } 

    @Override 
    public OSGiRV osgiApplicationLaunched(IApplicationContext applicationContext) { 
     applicationContext.applicationRunning(); 
     return StartupProgressTrackerService.OSGiRV.CONTINUE; 
    } 

    @Override 
    public void stateReached(ProgressState state) { 
     if (DefaultProgressState.JAVAFX_INITIALIZED.equals(state)) { 
      stage = new Stage(StageStyle.UNDECORATED); 
      stage.initModality(Modality.WINDOW_MODAL); 
      stage.setAlwaysOnTop(true); 
      ImageView view = null; 
      try { 
       view = new ImageView(SPLASH_IMAGE); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      BorderPane bp = new BorderPane(); 
      bp.getChildren().add(view); 
      Scene scene = new Scene(bp, 400, 300); 
      stage.setScene(scene); 
      stage.show(); 
     } 
    } 

} 

回答

1

我發現了一個醜陋的解決方案,但是,至少,它的工作原理。我注意到,作爲副作用的方法stage.showAndWait()完成了構建尚未渲染的所有控件。所以訣竅是初始化啓動畫面,然後立即創建虛擬舞臺showAndWait() it和close()。我知道,這個解決方案很不理想,所以我將不勝感激,如果有人可以告訴我另一種方法,使其工作:)

我的代碼:

public void showSplash() { 
    splashScreen = createSplashScreen(); 
    Stage stage2 = new Stage(StageStyle.TRANSPARENT); 
    splashScreen.show(); 
    Platform.runLater(new Runnable() { 
     @Override 
     public void run() { 
      stage2.close(); 
     } 
    }); 
    stage2.showAndWait(); 
} 

private Stage createSplashScreen() { 
    Stage stage = new Stage(StageStyle.UNDECORATED); 
    stage.setAlwaysOnTop(true); 

    VBox vbox = new VBox(); 
    vbox.getChildren().add(new ImageView(splashImage)); 
    Scene scene = new Scene(vbox, 400, 300); 
    stage.setScene(scene); 
    return stage; 
}