我如何将动作侦听器添加到循环中的按钮?

How would I add an action listener to buttons on a loop?

我想要它,所以每次在我的 4x4 网格中按下一个按钮时,它都会将 moves 递增 1 。这是创建一个 4x4 的按钮布局。每次按下这些按钮中的任何一个时,我都希望 moves 递增。基本上我正在创建记忆游戏,您可以在其中翻转卡片以相互匹配。我只需要计算玩家为解决难题所做的 moves 总量。

private int moves = 0;

private GridPane makeGridPane(){
    ConcentrationModel c = new ConcentrationModel();
    GridPane grid = new GridPane();

    ColumnConstraints col1 = new ColumnConstraints();
    col1.setPercentWidth( 50 );

    grid.getColumnConstraints().addAll(col1, col1, col1, col1);
    RowConstraints row1 = new RowConstraints();
    row1.setPercentHeight( 50 );
    grid.getRowConstraints().addAll(row1, row1, row1, row1);


    for(int row = 0; row < 4; row ++){
        for(int col = 0; col < 4; col++){
            Button btn = new Button();
            ImageView image = new ImageView(c.getCards().get(0).getImage());
            image.setFitWidth(WIDTH/4);
            image.setFitHeight(HEIGHT/4);
            btn.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
            btn.setGraphic(image);
            grid.add(btn, col, row);


        }
    }
    return grid;
}

这其实很简单。您所要做的就是在 grid.add(...):

之前添加这段代码
btn.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent event) {
        moves++;
    }
});

或者,等价地,Java 8 版本:

btn.setOnAction((ActionEvent e) -> {moves++;});

(我目前无法对此进行测试,但它 应该 可以工作。如果不行,请告诉我,我会尝试修复它。)

如果您担心内存问题, 这会为每个按钮创建一个 EventHandler 的新实例。虽然这可能不是太糟糕,但养成这可能是一个坏习惯。最好的处理方法(没有双关语)是在 for 循环之前创建一个对象:

EventHandler<ActionEvent> eh = new EventHandler<>() {
    @Override public void handle(ActionEvent event) {
        moves++;
    }
};

然后,对于每个按钮,您只需编写:

,而不是最顶层的代码
btn.setOnAction(eh);

这样,将创建一个 EventHandler 并用于处理所有事件。如果你需要创建的不仅仅是几个按钮,你会想要使用这个,因为它速度更快(不需要为每个对象分配内存)并且内存效率更高(......它,呃,不'需要为每个对象分配内存)。在这种情况下,我认为这很微不足道,但还是很高兴知道。

您需要在 class 中实现 ActionListener 接口。 然后在 actionPerformed 函数中,只需增加 moves 变量。 您只需为每个按钮调用 btn.addActionListener(this); 你创造了。

public class Sample extends JFrame implements ActionListener {

  public Sample ()
  {

  }


  private GridPane makeGridPane()
  {
    ConcentrationModel c = new ConcentrationModel();
    GridPane grid = new GridPane();

    ColumnConstraints col1 = new ColumnConstraints();
    col1.setPercentWidth( 50 );

    grid.getColumnConstraints().addAll(col1, col1, col1, col1);
    RowConstraints row1 = new RowConstraints();
    row1.setPercentHeight( 50 );
    grid.getRowConstraints().addAll(row1, row1, row1, row1);


    for(int row = 0; row < 4; row ++){
        for(int col = 0; col < 4; col++){
            Button btn = new Button();
            ImageView image = new ImageView(c.getCards().get(0).getImage());
            image.setFitWidth(WIDTH/4);
            image.setFitHeight(HEIGHT/4);
            btn.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
            btn.setGraphic(image);
            btn.addActionListener(this);
            grid.add(btn, col, row);


        }
    }
    return grid;
  }


  public void actionPerformed(ActionEvent e) 
  {
    moves++;
  }  

}

您可以创建单个事件处理程序并将其重复用于所有按钮。由于您可能还希望按钮执行其他操作,因此我建议将其添加为事件处理程序,而不是使用便捷方法 setOnAction(...):

EventHandler<ActionEvent> incrementMovesHandler = e -> moves++ ;

for(int row = 0; row < 4; row ++){
    for(int col = 0; col < 4; col++){
        Button btn = new Button();
        btn.addEventHandler(ActionEvent.ACTION, incrementMovesHandler);
        // ...
    }
}