【问题标题】:JavaFX UNDECORATED Stage not showingJavaFX UNDECORATED 舞台未显示
【发布时间】:2016-06-23 14:12:38
【问题描述】:

may e(fx)clipse 应用程序有问题。我想在应用程序启动时显示启动画面。我成功创建了实现StartupProgressTrackerService 的类,并调用了我的stateReached 方法。但是我遇到了javafx 本身的问题。我想用StageStyle.UNDECORATED 创建舞台。但是,当我调用 stage.show() 方法时,stage 不会立即呈现,而是在创建主窗口后出现。它工作正常,例如与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();
        }
    }

}

【问题讨论】:

    标签: java javafx eclipse-rcp


    【解决方案1】:

    我找到了一个丑陋的解决方案,但至少它有效。我注意到方法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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-07
      • 1970-01-01
      • 2017-07-13
      • 2017-12-17
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多