游戏中的第二个按钮没有着色

htrmnn0y  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(244)

我是javafx新手,我遇到了一个问题。我想让用户每次只能从2x2按钮中选择两个按钮的颜色,并且2x2按钮只有在第二个按钮被单击后才能重置,或者当用户选择通过重置按钮使其静止时,然后他可以选择另外两个按钮,但是在单击第二个按钮之后,第二个按钮没有着色,整个2x2按钮重置,只有第一个按钮着色。

public class ColorGame extends Application {

    int onlyTwoSelection = 0;//allowing user to clicke 2 times
    ArrayList<Integer> buttonId = new ArrayList<Integer>(); //Storing the last 2 button clicked Id

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button blankButton[] = new Button[4];//uncolored buttons
        Button reset = new Button("Reset");//Reset all buttons to the first state 
        HBox hbox = new HBox();//Storing Reset
        BorderPane borderPane = new BorderPane();
        GridPane gridpane = new GridPane();

        for (int i = 0; i < 4; i++) {//This for loop setting all Buttons
            blankButton[i] = new Button();
            blankButton[i].setMinSize(170, 110);
            blankButton[i].setStyle(null);
        }   
            reset.setOnAction(e -> {//Reset all buttons to the first state 

                for (int i = 0; i < 4; i++) {
                    blankButton[i].setStyle(null);
                }

        });

                gridpane.add(blankButton[0], 0, 0);
                gridpane.add(blankButton[1], 0, 1);
                gridpane.add(blankButton[2], 1, 0);
                gridpane.add(blankButton[3], 1, 1);

           for(int i = 0; i < 4; i++) {//finding which buttons get clicked
                final int buttonInd = i;

                blankButton[i].setOnAction(e -> {
                    onlyTwoSelection++;
                    if((onlyTwoSelection<=2&&onlyTwoSelection>0)) {
                        buttonId.add(buttonInd);
                        blankButton[buttonInd].setStyle("-fx-BackGround-color: #ff0000");
                        if(onlyTwoSelection==2) 
                        {
                            blankButton[buttonId.get(0)].setStyle(null);
                            blankButton[buttonId.get(1)].setStyle(null);

                            onlyTwoSelection=0;
                            buttonId.clear();
                        }

                    }

                });
            }   

        hbox.getChildren().addAll(reset);
        hbox.setAlignment(Pos.CENTER);
        gridpane.setAlignment(Pos.CENTER);
        borderPane.setCenter(gridpane);
        borderPane.setBottom(hbox);     
        Scene scene = new Scene(borderPane, 1000, 700);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ColorGame"); 
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
``` `buttonId` 存储最后2个按钮。 `onlyTwoSelection` 确保用户选择了两个。 `reset` 重置4x4按钮。 `buttonInd` 最后一个按钮是最后一次被点击吗 `for` loob用于查找用户单击了哪个按钮。
..............
编辑:我认为问题是这个代码在用户看到红色之前就被激活了,有没有什么方法可以阻止或延迟它被激活一秒钟?

if(onlyTwoSelection==2)
{
blankButton[buttonId.get(0)].setStyle(null);
blankButton[buttonId.get(1)].setStyle(null);

                        onlyTwoSelection=0;
                        buttonId.clear();
                    }
mwyxok5s

mwyxok5s1#

for (int i = 0; i < 4; i++) {//finding which buttons get clicked
            final int buttonInd = i;
            blankButton[i].setOnAction(e -> {

                if (onlyTwoSelection < 2) {
                    blankButton[buttonInd].setStyle("-fx-BackGround-color: #ff0000");
                    onlyTwoSelection++;
                } else {
                    onlyTwoSelection = 0;
                    reset.fire();
                    blankButton[buttonInd].setStyle("-fx-BackGround-color: #ff0000");
                    onlyTwoSelection++;
                }
            });
        }

这就是逻辑。

相关问题