簡體   English   中英

JavaFX UNDECORATED階段未顯示

[英]JavaFX UNDECORATED Stage not showing

我可能對e(fx)clipse應用程序有問題。 我想在啟動應用程序時顯示啟動屏幕。 我成功創建了實現StartupProgressTrackerService類,並調用了stateReached方法。 但是, javafx本身存在問題。 我想用StageStyle.UNDECORATED創建舞台。 但是,當我調用stage.show()方法時, stage不會立即呈現,而是在創建主窗口后立即出現。 它可以很好地與StageStyle.UTILITY配合StageStyle.UTILITY 當我使用showAndWait()方法時,它也可以正確呈現,但是它會停止加載我的應用程序,直到我關閉舞台。

這是我的代碼:

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

}

我找到了一個丑陋的解決方案,但至少可以奏效。 我注意到該方法stage.showAndWait()作為副作用完成了尚未渲染的所有控件的構建。 因此,訣竅是初始化啟動畫面,然后創建虛擬舞台,將showAndWait()並立即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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM