单击时暂时更改按钮的颜色

问题描述 投票:0回答:1

我需要暂时更改单击按钮的背景颜色0.5秒,然后让它恢复原始颜色。

我尝试过使用pausetransition,但我对java很新,我不确定如何正确使用它。现在,我能做的最好的事情就是让他按下按钮,在点击时保持新的颜色。

       Color[] colors = new Color[]{Color.DARKORCHID,Color.SALMON,Color.SPRINGGREEN,Color.GOLD};
        Color randomColor = colors[(new Random().nextInt(4))]; 
        button.setBackground(new Background(new BackgroundFill(randomColor,null,null)));
        grid.add(button, column, row);
         button.setOnAction(new EventHandler<ActionEvent>(){
          public void handle(ActionEvent e){
             Button b = (Button)e.getSource();
             Button save = b;
             b.setBackground(new Background(new BackgroundFill(Color.LIGHTGRAY,null,null)));
          }
        });
}

现在这只是将颜色改为灰色。如果我能弄清楚如何让颜色暂时改变,或者甚至在再次点击时改回来。我的另一个问题是所有按钮都有不同的颜色。任何提示或帮助将不胜感激。

button javafx actionevent
1个回答
1
投票

对于你创建的每个Button,你需要创建一个持续时间为半秒的PauseTransition,它将背景设置为原始背景。当您单击Button时,您将背景更改为灰色背景并重新启动PauseTransition。这样可以使背景在最后一次点击后恢复半秒钟。这是一个小例子:

import java.util.Random;
import javafx.animation.Animation.Status;
import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        GridPane root = new GridPane();
        root.setAlignment(Pos.CENTER);
        root.setHgap(10);
        root.setVgap(10);
        root.setPadding(new Insets(10));

        fillGrid(root);

        primaryStage.setScene(new Scene(root, 500, 300));
        primaryStage.show();
    }

    private static void fillGrid(GridPane grid) {
        Background[] backgrounds = {
            new Background(new BackgroundFill(Color.DARKORCHID, null, null)),
            new Background(new BackgroundFill(Color.SALMON, null, null)),
            new Background(new BackgroundFill(Color.SPRINGGREEN, null, null)),
            new Background(new BackgroundFill(Color.GOLD, null, null))
        };
        Background greyBg = new Background(new BackgroundFill(Color.GREY, null, null));

        Random rand = new Random();
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Background original = backgrounds[rand.nextInt(backgrounds.length)];

                Button button = new Button(String.format("(%d,%d)", i, j));
                button.setBackground(original);

                PauseTransition transition = new PauseTransition(Duration.seconds(0.5));
                transition.setOnFinished(event -> button.setBackground(original));

                button.setOnMouseClicked(event -> {
                    event.consume();
                    button.setBackground(greyBg);
                    transition.playFromStart();
                });

                grid.add(button, j, i);
            }
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.