gpt4 book ai didi

java - 如何使用 JavaFX 使 Label 的内容在一秒后发生变化?

转载 作者:行者123 更新时间:2023-11-30 07:20:59 25 4
gpt4 key购买 nike

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

最佳答案

您可以使用PauseTransition:

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

关于java - 如何使用 JavaFX 使 Label 的内容在一秒后发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37581804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com