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

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

问题描述

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

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

Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();隐藏();父根 = FXMLLoader.load(getClass().getResource("/view/scene/configure/NewProjectConfigureScene.fxml"));场景场景 = 新场景(根);stage.setTitle("配置新项目设置");stage.setScene(场景);舞台表演();

用文字解释:按下按钮->我得到舞台->隐藏当前场景->切换舞台有新场景->重新显示舞台.此行为有效.

我是 GUI 编程的新手,所以请耐心等待我解释我需要什么.

我现在正在尝试添加一个小功能,当按下 Create New Project 按钮时,当前场景中会出现加载消息,我强制 GUI 等待几秒钟(所以在继续下一个场景之前,用户有时间看到这个正在加载"消息).加载消息看起来像这样.

我真的很想实现这个功能,因为加载消息后等待可能更直观,从而改善我的用户使用程序时的体验.

我最初的尝试是执行以下操作:

statusText.setText("请稍候...");//statusText 是加载消息"Stage stage = (Stage)((Node)event.getSource()).getScene().getWindow();尝试 {线程睡眠(2000);} catch(InterruptedException ex) {Thread.currentThread().interrupt();}隐藏();父根 = FXMLLoader.load(getClass().getResource("/view/scene/configure/NewProjectConfigureScene.fxml"));场景场景 = 新场景(根);stage.setTitle("配置新项目设置");stage.setScene(场景);舞台表演();

我刚刚读到这里你永远不能让 UI 线程休眠,因为它会冻结整个应用程序.所以我一直在尝试上面链接的第一个答案中提到的方法,它说使用 javafx.concurrent.Task,但是我已经尝试了很长时间没有成功.>

如何更新UI,强行等待几秒,然后显示新场景?

解决方案

不要让线程休眠,而是使用 PauseTransition 并在暂停完成后加载新场景.

createNewProject.setOnAction(event -> {statusText.setText("请稍候...");PauseTransition pause = new PauseTransition(持续时间.秒(1),);pause.setOnFinished(事件 -> {父根 = FXMLLoader.load(getClass().getResource(/view/scene/configure/NewProjectConfigureScene.fxml"));场景场景 = 新场景(根);stage.setTitle("配置新项目设置");stage.setScene(场景);stage.sizeToScene();});暂停播放();});

以上代码假设您只有一个阶段,因此您将欢迎"阶段调整为配置新项目设置"阶段.

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.

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

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

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?

解决方案

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.

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

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