使用TimeUnit在更改JavaFX标签文本之间休眠

使用TimeUnit在更改JavaFX标签文本之间休眠,java,javafx,sleep,Java,Javafx,Sleep,我正在用JavaFX为我一直在开发的口袋妖怪游戏编写GUI。单击要使用的移动后,我想用标签讲述攻击。我想分别显示几条消息,所以我尝试使用TimeUnit.SECONDS.sleep()在每条消息之间留出3秒的间隔。当我运行程序时,它停留在上一个屏幕上(代码中的moveOptions),等待6秒钟,并重置到主屏幕(battleOptions),而不显示任何消息。为什么它不显示我的标签 @FXML HBox battleOptions, moveOptions, attackNarration;

我正在用JavaFX为我一直在开发的口袋妖怪游戏编写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
    }

我认为这可能是有帮助的:你不能使用睡眠。JavaFX在单个线程上运行,您的睡眠调用将阻塞该线程,从而阻止JavaFX在睡眠期间重新绘制或处理用户输入。正如aran所建议的,javafx.animation包是您应该使用的。谢谢,我能够让它与时间轴一起工作。嘿,很高兴它有所帮助