在继续JavaFX之前更新UI并强行等待 [英] Updating your UI and forcibly waiting before continuing JavaFX

查看:182
本文介绍了在继续JavaFX之前更新UI并强行等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里展示了我欢迎场景的图片。

I show here an image of my welcome scene.

创建新项目按钮的当前行为如下所示:

The current behavior of the Create New Project button is shown here:

Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
stage.hide();
Parent root = FXMLLoader.load(getClass().getResource("/view/scene/configure/NewProjectConfigureScene.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Configure New Project Settings");
stage.setScene(scene);
stage.show();

用文字解释:按下按钮 - >我得到舞台 - >隐藏当前场景 - >切换舞台以获得新场景 - >重新显示舞台。这种行为是有效的。

To explain it in words: the button is pressed -> I get the stage -> hide the current scene -> switch the stage to have a new scene -> re-display the stage. This behavior is working.

我是GUI编程的新手,所以请在我解释我需要的时候请耐心等待。

I am new to GUI programming so please bear with me as I explain what I need.

我现在正在尝试添加一个小功能,当按下创建新项目按钮时,当前场景中会出现加载消息,我强制GUI等待几秒钟(所以在继续进入下一个场景之前,用户有时间看到这个加载消息。加载消息看起来像这样。

I am now trying to add a small feature where when the Create New Project button is pressed, a loading message appears in the current scene, I force the GUI to wait for a few seconds (so that the user has time to see this "loading" message) before continuing onto the next scene. The loading message would look something like this.

我真的想要实现此功能,因为加载消息后跟等待可能更直观,从而改善了我在使用程序时的用户体验。

I really want to implement this feature because a loading message followed by a wait could be more intuitive and consequently improve my users experience when using the program.

我最初的尝试是做以下:

My initial attempt was to do the following:

statusText.setText("Please wait..."); // statusText is the "loading message"
Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();
try {
    Thread.sleep(2000);                
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}
stage.hide();

Parent root = FXMLLoader.load(getClass().getResource("/view/scene/configure/NewProjectConfigureScene.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Configure New Project Settings");
stage.setScene(scene);
stage.show();

我刚读过 here ,你永远无法睡眠UI线程,因为它会冻结整个应用程序。所以我一直在尝试上面链接的第一个答案中提到的方法,它说要使用 javafx.concurrent.Task ,但是我已经尝试了很长时间无效。

I just read here that you can never sleep the UI thread because it will freeze the entire application. So I've been trying the approach mentioned in the first answer from the above link, which says to use javafx.concurrent.Task, however I have been trying for a long time to no avail.

如何更新用户界面,强行等待几秒钟,然后显示一个新场景?

How do I update the UI, forcibly wait for a few seconds, and then display a new scene?

推荐答案

使用 PauseTransition 并在暂停结束后加载新场景。

Instead of sleeping the thread, use a PauseTransition and load your new scene after the pause has finished.

createNewProject.setOnAction(event -> {
    statusText.setText("Please wait...");
    PauseTransition pause = new PauseTransition(
        Duration.seconds(1),
    );
    pause.setOnFinished(event -> {
        Parent root = FXMLLoader.load(
            getClass().getResource(
                "/view/scene/configure/NewProjectConfigureScene.fxml"
            )
        );
        Scene scene = new Scene(root);
        stage.setTitle("Configure New Project Settings");
        stage.setScene(scene);
        stage.sizeToScene();    
    });
    pause.play();
});

上面的代码假设您只有一个阶段,因此您调整欢迎阶段的大小配置新项目设置阶段。

The above code assumes you have just a single stage, so you resize your "Welcome" stage to become the "Configure New Project Settings" stage.

这篇关于在继续JavaFX之前更新UI并强行等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆