0

我对如何在 JavaFX 启动期间显示“正在加载”消息感兴趣。例如,我注意到,当我启动 SceneBuilder 时,我必须等待大约 5 秒才能将应用程序显示在用户面前。例如,我可以在 JVM 启动期间显示一些消息吗?

更新

我测试了这段代码:

public class test extends Application
{

    private Pane splashLayout;
    private ProgressBar loadProgress;
    private Label progressText;
    private WebView webView;
    private Stage mainStage;
    private static final int SPLASH_WIDTH = 676;
    private static final int SPLASH_HEIGHT = 227;

    public static void main(String[] args) throws Exception
    {
        launch(args);
    }

    @Override
    public void init()
    {
        ImageView splash = new ImageView(new Image("http://fxexperience.com/wp-content/uploads/2010/06/logo.png"));
        loadProgress = new ProgressBar();
        loadProgress.setPrefWidth(SPLASH_WIDTH - 20);
        progressText = new Label("Loading System Modules ...");
        splashLayout = new VBox();
        splashLayout.getChildren().addAll(splash, loadProgress, progressText);
        progressText.setAlignment(Pos.CENTER);
        splashLayout.setStyle("-fx-padding: 5; -fx-background-color: cornsilk; -fx-border-width:5; -fx-border-color: linear-gradient(to bottom, chocolate, derive(chocolate, 50%));");
        splashLayout.setEffect(new DropShadow());
    }

    @Override
    public void start(final Stage initStage) throws Exception
    {
        showSplash(initStage);
        showMainStage();

        webView.getEngine().documentProperty().addListener(new ChangeListener<Document>()
        {
            @Override
            public void changed(ObservableValue<? extends Document> observableValue, Document document, Document document1)
            {
                if (initStage.isShowing())
                {
                    loadProgress.progressProperty().unbind();
                    loadProgress.setProgress(1);
                    progressText.setText("All Modules Are Loaded!");
                    mainStage.setIconified(false);
                    initStage.toFront();
                    FadeTransition fadeSplash = new FadeTransition(Duration.seconds(1.2), splashLayout);
                    fadeSplash.setFromValue(1.0);
                    fadeSplash.setToValue(0.0);
                    fadeSplash.setOnFinished(new EventHandler<ActionEvent>()
                    {
                        @Override
                        public void handle(ActionEvent actionEvent)
                        {
                            initStage.hide();
                        }
                    });
                    fadeSplash.play();
                }
            }
        });
    }

    private void showMainStage()
    {
        mainStage = new Stage(StageStyle.DECORATED);
        mainStage.setTitle("FX Experience");
        mainStage.setIconified(true);

// create a WebView.
        webView = new WebView();
        webView.getEngine().load("http://fxexperience.com/");
        loadProgress.progressProperty().bind(webView.getEngine().getLoadWorker().workDoneProperty().divide(100));

// layout the scene.
        Scene scene = new Scene(webView, 1000, 600);
        webView.prefWidthProperty().bind(scene.widthProperty());
        webView.prefHeightProperty().bind(scene.heightProperty());
        mainStage.setScene(scene);
        mainStage.show();
    }

    private void showSplash(Stage initStage)
    {
        Scene splashScene = new Scene(splashLayout);
        initStage.initStyle(StageStyle.UNDECORATED);
        final Rectangle2D bounds = Screen.getPrimary().getBounds();
        initStage.setScene(splashScene);
        initStage.setX(bounds.getMinX() + bounds.getWidth() / 2 - SPLASH_WIDTH / 2);
        initStage.setY(bounds.getMinY() + bounds.getHeight() / 2 - SPLASH_HEIGHT / 2);
        initStage.show();
    }
}

我注意到飞溅图像是从主舞台同时开始的,但主舞台被最小化了。我可以冻结主舞台的外观,直到加载完成启动画面。在显示主要阶段之前,我想进行一些内部检查。

4

1 回答 1

0

用来javax.swing.JProgressBar实现这一点。

检查JProgressBar中的详细信息(文档)

于 2013-06-01T18:02:54.557 回答