【发布时间】:2016-08-11 18:19:03
【问题描述】:
我正在尝试计算一组按钮中的鼠标点击次数,但我的“点击”变量出现错误,我不太明白:
“在封闭范围内定义的局部变量点击必须是最终的或有效的最终”
我猜这与我安排 for 循环和 actionListener 的方式有关。有人可以指导我正确的方向吗?谢谢。
int click = 0;
JButton buttonArray1 [] = {a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4};
for (int d = 0; d < buttonArray1.length; d++) {
buttonArray1[d].addActionListener(new ActionListener() {
//add an action listener to the current button
@Override
public void actionPerformed(ActionEvent e)
{
click = click +1;
}
});
已解决:
class Click{
int counter = 0 ;
public void Click(){
counter++;
}
public int GetClick(){
return counter;
}
}
【问题讨论】:
标签: java for-loop scope jbutton