Java 我想更改随机按钮的颜色,等待1秒钟,然后将其更改回默认值,int a for循环

Java 我想更改随机按钮的颜色,等待1秒钟,然后将其更改回默认值,int a for循环,java,button,javafx,background-color,scenebuilder,Java,Button,Javafx,Background Color,Scenebuilder,我想做一个代码,每次迭代我都会得到一个随机数,根据这个数,一个按钮会将其颜色更改为浅绿色,然后在一秒钟后,它会更改回默认值,但我的问题是for不会等到按钮更改回来,然后开始新的迭代。 这是我目前的代码: for(int i=0; i<n; i++) { int x = rand.nextInt(4) + 1; switch(x) { case 1: { System.out.println("b1");

我想做一个代码,每次迭代我都会得到一个随机数,根据这个数,一个按钮会将其颜色更改为浅绿色,然后在一秒钟后,它会更改回默认值,但我的问题是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更新?我看到了一些关于为循环创建一个新线程的内容,但我似乎不知道如何做,以及在哪里使用该平台。请稍后运行。

也许下面的代码可以满足您的需要。它使用时间线每秒随机选择一个按钮,并将按钮设置为绿色,而之前设置了颜色的任何按钮都将重置为默认颜色

import javafx.animation.*;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.Random;

public class RandomLighting extends Application {
    private static final int NUM_BUTTONS = 4;
    private static final Duration LIGHT_DURATION = Duration.seconds(1);
    private static final HBox buttons = new HBox(10);

    private static final String LIT_STYLE = "-fx-base: lightgreen;";
    private static Random random;

    private Button litButton;

    @Override
    public void init() {
        random = new Random(42);
    }

    @Override
    public void start(Stage stage) {
        buttons.setPadding(new Insets(10));
        for (int i = 0; i < NUM_BUTTONS; i++) {
            buttons.getChildren().add(new Button("" + (i + 1)));
        }

        Timeline lightAnimation = new Timeline(
                new KeyFrame(Duration.ZERO, 
                        event -> lightButton(chooseRandomButton())
                ),
                new KeyFrame(LIGHT_DURATION)
        );
        lightAnimation.setCycleCount(Timeline.INDEFINITE);

        stage.setScene(new Scene(buttons));
        stage.show();

        lightAnimation.play();
    }

    private Button chooseRandomButton() {
        ObservableList<Node> candidates = buttons.getChildren();
        return (Button) candidates.get(random.nextInt(candidates.size()));
    }

    private void lightButton(Button button) {
        if (litButton == button) {
            return;
        }

        if (litButton != null) {
            litButton.setStyle(null);
        }

        litButton = button;

        if (litButton != null) {
            button.setStyle(LIT_STYLE);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}