[英]Local variable “i” is accessed within an inner class fix?
I've seen some posts on this before, but I haven't been able to find one regarding actionListeners. 之前我已经看过一些帖子,但是我找不到关于actionListeners的帖子。 I am trying to create tic-tac-toe using an array of JButtons. 我正在尝试使用JButtons数组创建井字游戏。 How do I add an action listener to them whilst using a for loop temporary variable if at all possible? 如果有可能,如何在使用for循环临时变量的同时向它们添加操作侦听器? Thanks for your help in advance. 感谢您的帮助。
JButton jb [] = new JButton[9];
int checkB [] = new int[9];
public SomethingSimple(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 3, 5, 5));
p1.setBackground(Color.red);
for (int i = 0; i < jb.length; i++){
checkB[i] = 0;
}
for (int i = 0; i < jb.length; i++){
jb[i] = new JButton("");
p1.add(jb[i]);
jb[i].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
jb[i].setText("O");
}
});
}
add(p1);
}
Thanks everyone for your help, you gave me some solutions! 感谢大家的帮助,您给我一些解决方案!
Create a final int inside the forloop; 在forloop内部创建一个最终的int; You cant access a local variable from your class to a anonymous class.. 您不能从类访问匿名类的局部变量。
solution: 解:
for (int i = 0; i < jb.length; i++){
jb[i] = new JButton("");
final int index = i;
jb[i].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
jb[index].setText("O");
}
});
p1.add(jb[i]);
}
I think the best solution will be to move ActionListener
to a separate class: 我认为最好的解决方案是将ActionListener
移至单独的类:
public class MyActionListener implements ActionListener {
private final JButton button;
public MyActionListener(JButton button) {
this.button = button;
}
@Override
public void actionPerformed(ActionEvent e){
button.setText("O");
}
}
And change your code like this: 并像这样更改代码:
for (int i = 0; i < jb.length; i++) {
jb[i] = new JButton("");
p1.add(jb[i]);
jb[i].addActionListener(new MyActionLisener(jb[i]));
}
Local and anonymous classes can only access final
local variables, so do something like 本地和匿名类只能访问final
本地变量,因此可以执行以下操作
final JButton btn = jb[i];
inside the for
loop immediately after you create the new JButton
and then you can refer to btn
inside actionPerformed
. 在创建new JButton
之后立即在for
循环中new JButton
,然后可以在actionPerformed
内部引用btn
。
This is coming from a C# programmer, who is 15, so take it with a grain of salt. 这是由15岁的C#程序员提供的,因此请带一点盐。 First as I understand your question, you want to add the actionListener
to all of your Jbuttons in the array jb
, with a for loop. 首先,据我所知,您想通过一个for循环将actionListener
添加到数组jb
中的所有Jbutton中。 Basically in my java experience, you'll have to implement actionListener
into your class(or into whatever class that'll act as the actual listener). 基本上,根据我的Java经验,您必须在您的类中(或在将充当实际侦听器的任何类中)实现actionListener
。 Then your code should look something like this: 然后,您的代码应如下所示:
for(int i = 0; i < jb.length(); i++)
{
jb[i].addActionListener(class that implements the listener);
}
Please tell me if I misunderstood your question. 如果我误解了您的问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.