如何在循环上向按钮添加动作侦听器? [英] How would I add an action listener to buttons on a loop?
问题描述
我希望每次在我的4x4网格中按下按钮时,它会增加将
移动1。这是创建4x4按钮布局。每次按下任何按钮,我都希望移动
来增加。基本上我正在创建记忆游戏,你翻转卡片以相互匹配。我只需要计算一个玩家解决这个难题的移动
的总金额。
I want it so every time a button is pressed in my 4x4 grid, that it increments moves
by 1 . This is creating a 4x4 layout of buttons. Each time any of those buttons are pressed, I want moves
to increment. Basically I'm creating the memory game, where you flip cards over to match each other. I just have to keep count of the total amount of moves
a player does 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;
}
推荐答案
这实际上非常简单。您所要做的就是在 grid.add(...)
之前添加这段代码:
This is actually pretty simple. All you have to do is add this bit of code just before your grid.add(...)
:
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
moves++;
}
});
或者,相当于Java 8版本:
Or, equivalently, the Java 8 version:
btn.setOnAction((ActionEvent e) -> {moves++;});
(我目前无法对此进行测试,但应 >工作。如果没有,lemme知道,我会尝试解决它。)
(I can't currently test this, but it should work. If not, lemme know and I'll try to fix it.)
如果你担心记忆,基督徒指出以下这会为每个按钮创建一个 EventHandler
的新实例。虽然这可能不是太可怕,但这可能是一个坏习惯。处理(没有双关语)的最佳方法是在之前为
循环创建一个对象:
If you're concerned about memory, Christian points out below that this creates a new instance of EventHandler
for every button. While this may not be too terrible, it's probably a bad habit to get into. The best way to handle (no pun intended) this is by making an object before your for
loop:
EventHandler<ActionEvent> eh = new EventHandler<>() {
@Override public void handle(ActionEvent event) {
moves++;
}
};
然后,对于每个按钮,而不是最顶层的代码,你只需写:
Then, for each of your buttons, instead of the top-most code, you'd simply write:
btn.setOnAction(eh);
这样,单个 EventHandler
正在创建并用于处理所有事件。如果你需要创建的不仅仅是几个按钮,你会想要使用这个,因为它更快(不需要为每个对象分配内存)和更高的内存效率(...它,呃,没有不需要为每个对象分配内存。在这种情况下,我认为这是微不足道的,但尽管如此,我还是很高兴。
That way, a single EventHandler
is being created and used to handle all the events. You'll want to use this one if you need to create more than just a few buttons, both because it's faster (doesn't need to allocate memory for each object) and more memory-efficient (...it, uh, doesn't need to allocate the memory for each object). In this case, I think it's pretty trivial, but it's good to know nonetheless.
这篇关于如何在循环上向按钮添加动作侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!