很抱歉问了这么一个基本问题,但我就是想不通,我不知道如何搜索它。
我有以下代码:
letters = new ArrayList<JButton>();
String[] abc = new String[] {"A", "Á", "B", "C", "D", "E", "É", "F", "G", "H", "I", "Í", "J", "K", "L", "M", "N", "O", "Ó", "Ö", "Ő", "P",
"Q", "R", "S", "T", "U", "Ú", "Ü", "Ű", "V", "W", "X", "Y", "Z" };
for (Object o: abc)
{
letters.add(new JButton((String)o));
int i = letters.size() - 1;
letters.get(i).setBounds(i%10 * 60 + 40, 350 + ((i / 10) * 50), 55, 45);
letters.get(i).addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.out.println(o);
}
});
mF.add(letters.get(i));
}
如您所见,我有一个 for 循环,我想在函数内使用它的变量 'o'。我怎样才能做到这一点?它说:
java: local variable o is accessed from within inner class; needs to be declared final
这到底是什么意思?