【发布时间】:2020-05-01 02:24:17
【问题描述】:
我想编写一个代码,在每次迭代时我都会得到一个随机数,并基于该数字,按钮将其颜色更改为浅绿色,然后在一秒钟后恢复为默认值,但我的问题是 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。
【问题讨论】:
标签: java button javafx background-color scenebuilder