有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在试着做一个闪屏,所以我需要一些单词保持出现。我使用了o线程,但我不知道如何使标签出现循环,一秒钟后它会改变

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) 个答案

  1. # 1 楼答案

    您可以使用PauseTransition

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