如何使用JavaFX在一秒钟后更改Label的内容?

时间:2016-06-02 02:46:26

标签: java multithreading loops javafx sleep

我正在尝试制作启动画面,所以我需要一些单词不断出现。我使用了o Thread,但我不知道如何制作标签循环,并在一秒钟后更改。

package br.com.codeking.zarsystem.splash;

import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class ControllerSplash {

    @FXML
    Label lblLoading;



    @FXML
    private void initialize() {
        System.out.println("app start");

        lblLoading.setStyle("-fx-font-size: 16px;" + "-fx-font-family: Ubuntu;"
                + " -fx-text-fill: white;");

我已尝试重复此步骤10次,但它不起作用

        while (i <= 10) {

            Task<Void> sleeper = new Task<Void>() {

                @Override
                protected Void call() throws Exception {
                        try {
                            Thread.sleep(1500);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    return null;
                }
            };

            sleeper.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
                @Override
                public void handle(WorkerStateEvent event) {

                    lblLoading.setText("to change " + i);
                }
            });
             new Thread(sleeper).run();

                i++;
        }
    }

}

我无法在for循环中执行此操作?所以我不知道我是什么  必须这样做...我正在寻找,但没有任何帮助我。你能?谢谢 非常!

1 个答案:

答案 0 :(得分:1)

您可以使用PauseTransition

 PauseTransition pauseTransition = new PauseTransition(Duration.seconds(1));
 pauseTransition.setOnFinished(e -> lblLoading.setText("complete"));
 pauseTransition.play();
相关问题