在代码的不同部分,我将运行类似的变体:
_grid[4][4].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveBeginningPieceLeft(_square[4][11]);
refreshBoard();
}
});
唯一会改变的是 _grid 的坐标。我之前尝试做一些事情,我制作了一个 Action 监听器,但是 Eclipse 迫使我将整数定为最终的。我想知道是否可以轻松编写一个使用正在调用的网格值的 addListener 方法(_grid 是 JButton 数组,_square 是堆栈数组)。因此,如果我在 _grid[2][5] 上调用 addListener,它只会从 _grid 获取坐标 [2][5] 并将其放置在 Action 监听器的 _square 部分中。所以,
_grid[0][5].addListener();
与
相同 _grid[0][5].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
moveBeginningPieceLeft(_square[0][5]);
refreshBoard();
}
});
最佳答案
I'm wondering if it is easy to write an addListener method that uses the values of the grid that it is being called on(_grid is an array of JButtons and _square is an array of stacks).
- 不,没有
I tried doing something earlier where I made an actionlistener, but eclipse was forcing me to make the integers final.
将所需的 vasriable 声明为局部变量
有关使用put/getClientProperty的最简单方法的示例,您可以将此方法与另一个附加值结合使用
为了获得更好的帮助,请尽快发布 SSCCE ,短,可运行,可编译,精简且仅关于 JButton 的网格
关于java - 内部类 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15776235/