我想编写一个代码,每次迭代我都会得到一个随机数,并基于该数字将按钮的颜色更改为浅绿色,然后在一秒钟后将其更改为默认值,但是我的问题是for()赢得了不要等到按钮变回原位,它才开始新的迭代。到目前为止,这是我的代码:
for(int i=0; i<n; i++) {
int x = rand.nextInt(4) + 1;
switch(x) {
case 1: {
System.out.println("b1");
button1.setStyle("-fx-background-color: lightgreen; -fx-border-color: black;");
PauseTransition wait = newPauseTransition(Duration.seconds(1));
wait.setOnFinished(event -> {
button1.setStyle("");
});
wait.play();
}
break;
case 2: {
System.out.println("b2");
button2.setStyle("-fx-background-color: lightgreen; -fx-border-color: black;");
PauseTransition wait = new PauseTransition(Duration.seconds(1));
wait.setOnFinished(event -> {
button2.setStyle("");
});
wait.play();
}
break;
...
}
我如何进行这项工作,以使循环不会阻止UI更新?我看到了有关为循环创建新线程的信息,但我似乎无法弄清楚如何执行该操作以及在何处使用Platform.runLater。