[英]In javafx how do i change the color of a button, wait 1 second than change it back do default?
所以我想将按钮的颜色更改为浅绿色,等待 1 秒而不是将其更改回默认值。 我怎样才能做到这一点? 我是这样试的:
button1.setStyle("-fx-background-color: lightgreen");
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
button1.setStyle("");
但我有两个问题:
颜色永远不会设置为浅绿色,只会设置为默认值。
如果我只想将其更改为浅绿色,则它只会在等待 1 秒后更改,而不会在此之前更改。
编辑:
所以我开始使用PauseTransition,但它不会像我想要的那样工作。
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;
...
}
现在的问题是 while() 不会等到按钮恢复默认值,然后开始新的迭代。
-fx-base
而不是-fx-background-color
。PauseTransition
。Thread.sleep()
。示例代码:
button.setStyle("-fx-base: lightgreen");
PauseTransition pause = new PauseTransition(
Duration.seconds(1),
);
pause.setOnFinished(event -> {
button.setStyle(null);
});
pause.play();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.