带有计时器的 javafx 标签消息不起作用

javafx label message showing with timer does not work

所以我试图在 javafx 中的标签上显示一条消息,然后让它在 1 秒后消失。我可以根据需要显示消息,但我无法让它消失。其实我的问题是我从来没有出现过。所以如果我只使用这个:

lbReserva.setText("RESERVA REALITZADA");

按预期工作,但显然它只是保持原样。然后我尝试了这个:

        try {
        lbReserva.setText("RESERVA REALITZADA");
        TimeUnit.SECONDS.sleep(1); 
        lbReserva.setText("");           
    } catch (InterruptedException e) {
        System.err.format("IOException: %s%n", e);
    }

但后来标签就再也没有出现过。我试过将第一组文本放在 try 块之前的外面。我试过将第二组文本放在接球之后。无论如何我得到了相同的结果,标签从未出现,或者可能出现并立即消失。任何线索我做错了什么?提前谢谢你。

pd:我尝试使用 Thread.sleep 而不是 TimeUnit 但我得到了相同的结果。

使用PauseTransition.

import javafx.animation.PauseTransition;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class TestingGround extends Application
{
    @Override
    public void start(Stage stage) throws Exception
    {
        Label label = new Label("Hello World!");
        PauseTransition wait = new PauseTransition(Duration.seconds(1));
        wait.setOnFinished((e) -> {
            label.setVisible(false);
        });
        wait.play();
        VBox root = new VBox(label);
        stage.setScene(new Scene(root, 700, 500));
        stage.show();
    }

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