首页 > 解决方案 > 使用 TimeUnit 在更改 JavaFX 标签文本之间休眠

问题描述

我正在用 JavaFX 为我一直在开发的 Pokemon 游戏编写 GUI。单击要使用的动作后,我想用标签叙述攻击。我想单独显示几条消息,所以我尝试使用 TimeUnit.SECONDS.sleep() 在每条消息之间放置 3 秒的间隔。当我运行程序时,它停留在前一个屏幕(代码中的 moveOptions),等待 6 秒并重置到主屏幕(battleOptions)而不显示任何消息。为什么它不显示我的标签?

@FXML
HBox battleOptions, moveOptions, attackNarration;

@FXML
Label narration;

public void handleMoveButton() throws InterruptedException {

        this.moveOptions.setVisible(false);                  //makes previous screen invisible
        this.attackNarration.setVisible(true);               //makes the layout containing my messages visible

        this.narration.setText("Pikachu used Tackle");       
        TimeUnit.SECONDS.sleep(3);                           //should display above message for 3 seconds

        this.narration.setText("Foe Pikachu used Tackle");
        TimeUnit.SECONDS.sleep(3);                           //should change to this message for 3 seconds

        this.attackNarration.setVisible(false);              //makes message screen invisible
        this.battleOptions.setVisible(true);                 //makes home screen visible to restart the turn
    }

标签: javajavafxsleep

解决方案


推荐阅读