[英]In javafx how do i change the color of a button, wait 1 second than change it back do default?
So i want to change the color of a button to light green, wait 1 second than change it back to default.所以我想将按钮的颜色更改为浅绿色,等待 1 秒而不是将其更改回默认值。 How can i do this?
我怎样才能做到这一点? I tried it this way:
我是这样试的:
button1.setStyle("-fx-background-color: lightgreen");
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
button1.setStyle("");
But i have 2 problems:但我有两个问题:
the color never sets to light green, only to default.颜色永远不会设置为浅绿色,只会设置为默认值。
if i want to change it only to light green, it only changes after the 1 second of waiting and not before it.如果我只想将其更改为浅绿色,则它只会在等待 1 秒后更改,而不会在此之前更改。
Edit:编辑:
So i got to the part to use PauseTransition, but it won't work the way i want it to.所以我开始使用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;
...
}
Now the problem is that the while() won't wait until the button turns back to default, and it starts a new iteration.现在的问题是 while() 不会等到按钮恢复默认值,然后开始新的迭代。
-fx-base
instead of -fx-background-color
.-fx-base
而不是-fx-background-color
。PauseTransition
.PauseTransition
。Thread.sleep()
on the UI thread.Thread.sleep()
。 Sample code:示例代码:
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.