我怎么知道是否按下了 2 个按钮? JavaFX

How can I know if 2 buttons are pressed? JavaFX

我有一个带有 10x10 按钮的 GridPane。全部放在一个名为 'buttons' 的 ArrayList 中。现在我需要知道,当一个按钮被点击时,它应该记住那个按钮,当另一个按钮被点击时执行一个动作。所以你会得到一个方法 makeTurn(按钮 1,按钮 2); 现在是我的问题,我该怎么做?这是我制作 GUI

的第一个 class
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.File;
import java.util.ArrayList;




public class DamspelApp  extends Application implements      EventHandler<ActionEvent>{
private ArrayList<Button> buttons = new ArrayList<Button>();
private Damspel spel = new Damspel();





@Override
public void start(Stage primaryStage) {
//CREATING UPPER HBOX
    Label melding = new Label(""+spel.getMeldingProperty());
    if(spel.getMelding()==null){
        melding.setText("Hier komen de meldingen!");
    }
    HBox hbox = new HBox();
    hbox.getChildren().add(melding);
    hbox.setPrefHeight(40);
    hbox.setAlignment(Pos.CENTER_RIGHT);

 //CREATING HBOX
    Button reset = new Button("reset");
    reset.setPrefWidth(60);
    CheckBox roteren = new CheckBox();
    roteren.getStyleClass().add("roteren");
    Label rot = new Label("Bord roteren");
    Label speler_lb = new Label("Speler: " + spel.getSpelerProperty());
    Label stenenZ_lb = new Label("Stenen Z: ");
    Label stenenW_lb = new Label("Stenen W: ");
//CREATING VBOX 
    VBox vbox = new VBox();
    vbox.getChildren().add(reset);
    HBox hbxo = new HBox();
    hbxo.getChildren().add(roteren);
    hbxo.getChildren().add(rot);
    vbox.getChildren().add(hbxo);
    vbox.getChildren().add(speler_lb);
    vbox.getChildren().add(stenenZ_lb);
    vbox.getChildren().add(stenenW_lb);
    vbox.setSpacing(10);
    vbox.setPrefWidth(100);


//CREATING AND POSITIONING IN BORDERPANE        
    BorderPane border = new BorderPane();
    GridPane grid = new GridPane();     
    border.setCenter(grid);
    border.setTop(hbox);
    border.setLeft(vbox);   

 //CREATING THE BUTTONS     
    int columns = 1;
    int rows=0;
    for(int i = 0; i < 100; i++){
        buttons.add(new Button(""+i));
    }
    for(Button b : buttons){
        grid.add(b, columns, rows);
        String text = b.getText();
        int texti = Integer.parseInt(text);
        columns++;
        b.setId(text);
        b.setPrefSize(45,45);
        b.setOnAction(this);
        if(texti == 9 || texti == 19||texti==29||texti==39||texti==49||texti==59||texti==69||texti==79||texti==89||texti==99){
            rows++;
            columns = 1;
        }

        String cssClassVoorButton = spel.getVeldStatus(texti);
            switch(cssClassVoorButton){
            case "WITDAM":b.getStyleClass().add("witdam");break;
            case "ZWARTDAM": b.getStyleClass().add("zwartdam");break;
            case "WIT": b.getStyleClass().add("wit");break;
            case "ZWART": b.getStyleClass().add("zwart"); break;
            case "NIETSPEELBAAR":    b.getStyleClass().add("nietspeelbaar");break;
            case "LEEG": b.getStyleClass().add("leeg");break;
            default: break;
            }
    }

 //MAKING THE SCENE
    Scene scene = new Scene(border, 500, 500);
    //File cssSheet = new File("myCSS.css");
    //scene.getStylesheets().add(cssSheet.toURI().toString());
    scene.getStylesheets().add("stylesheet2.css");
    primaryStage.setTitle("Kamer Boeken");
    primaryStage.setScene(scene);
    primaryStage.setResizable(false);
    primaryStage.show();


//---------------------------------------------------------------------------------------------------------Rotate-RESET     
    reset.setOnAction(new EventHandler<ActionEvent>(){
        public void handle(ActionEvent event){
            RotateTransition rt = new RotateTransition(Duration.millis(3000), grid);
            rt.setByAngle(180);
            rt.setCycleCount(1);
            rt.setAutoReverse(false);
            rt.play();
        }
    });



    if(roteren.isSelected() && spel.getCheck()){
        RotateTransition rt = new RotateTransition(Duration.millis(3000), grid);
        rt.setByAngle(180);
        rt.setCycleCount(1);
        rt.setAutoReverse(false);
    }

}
public void handle(ActionEvent event) {
    System.out.println(""+ event.getSource());
    for(Button b : buttons){
        if(event.getSource() == b){
            System.out.println(""+ event.getSource());
            b.getStyleClass().add("leeg");
            String styleclass = spel.getVeldStatus(Integer.parseInt(b.getText()));
            String stylec = styleclass.toLowerCase();
            b.getStyleClass().remove(stylec);
            System.out.println(spel.getVeldStatus(Integer.parseInt(b.getText())));

        }
    }

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


}

您只需要存储在某处点击的第一个 Button。另外我建议使用 ToggleButtons,因为用户可以更清楚地看到按下了哪个 Button

public class Main extends Application {

    private final ObjectProperty<ToggleButton> rememberMe = new SimpleObjectProperty<>();
    private final List<ToggleButton> buttons = new ArrayList<>();

    @Override
    public void start(Stage primaryStage) {

        GridPane grid = new GridPane();
        grid.setHgap(5);
        grid.setVgap(5);
        grid.setPadding(new Insets(5));

        for (int row = 0; row < 10; row++) {
            for (int col = 0; col < 10; col++) {
                ToggleButton button = new ToggleButton(row + " | " + col);

                button.selectedProperty().addListener((w, o, n) -> {
                    if (n) {
                        if(rememberMe.get() != null) {
                            turn(rememberMe.get() , button);
                        }
                        else {
                            rememberMe.set(button);
                        }
                    }
                });
                grid.add(button, col, row);
                buttons.add(button);
            }
        }

        Scene scene = new Scene(grid, 500, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void turn(ToggleButton pButton1, ToggleButton pButton2) {
        rememberMe.set(null);

        System.out.println(pButton1 + " - " + pButton2);

        buttons.forEach(tb -> tb.setSelected(false));
    }

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

从这里开始,创建突出显示和其余游戏逻辑应该非常简单。

您可以使用 Node 的方法 setUserData and getUserData 来存储各种信息,例如您是否单击了按钮。

作为替代方案,您可以使用 BitSet 来存储布尔状态。