How to add a listener to buttons in a loop?

I want every time a button is pressed in my 4x4 grid, it increments movesby 1. This creates the layout of the 4x4 buttons. Every time any of these buttons is pressed, I want to moveszoom. I basically create a memory game where you flip cards to match each other. I just need to calculate the total amount movesthat the player makes to solve the puzzle.

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;
}
0
source share
3 answers

It is actually quite simple. All you have to do is add this bit of code directly in front of yours grid.add(...):

btn.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent event) {
        moves++;
    }
});

Or, equivalently, Java 8 version:

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

( , . , lemme , .)

, , EventHandler . , , . ( ) - for:

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

:

btn.setOnAction(eh);

EventHandler . , , , ( ) (... it, uh, doesn ). , , , , .

+1

. , , , , 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);
        // ...
    }
}
+2

ActionListener . actionPerformed . 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++;
  }  

}
-2
source

Source: https://habr.com/ru/post/1661676/


All Articles