【问题标题】:How can I make the content of a Label change after one second using JavaFX?如何使用 JavaFX 在一秒钟后更改标签的内容?
【发布时间】:2016-10-01 14:20:08
【问题描述】:

我正在尝试制作一个启动画面,所以我需要一些字词保持出现。我使用了 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 循环中执行这个?所以我不知道我是什么 必须做...我正在寻找,但没有任何帮助。你可以吗?谢谢 非常喜欢!

【问题讨论】:

    标签: java multithreading loops javafx sleep


    【解决方案1】:

    您可以使用PauseTransition

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 2017-07-10
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多