【问题标题】:I want to change the color of a random button, wait 1 sec then change it back to default, int a for loop我想更改随机按钮的颜色,等待 1 秒然后将其更改回默认值,进入 for 循环
【发布时间】: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


【解决方案1】:

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

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);
    }
}

【讨论】:

    猜你喜欢
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    相关资源
    最近更新 更多