listProcut是一个列表框,定义在一个类的public void dialog()方法中,现在我想实现在列表框中选择一个条目,就能得到所选择的条目的字符串,我把事件写在了内部类中:
int[] index=new int[vectorProdcut.size()];
listProduct.addListSelectionListener(new ListSelectionListener()
{public void valueChanged(ListSelectionEvent e)
{
index = listProduct.getSelectedIndices();
System.out.println(index[index.lenth-1]);
}
});
但总是报:local variable index is accessed from within inner class:needs to be declared final
难道内部类不能访问外面的非静态变量,请高手指点一下,好郁闷
int[] index=new int[vectorProdcut.size()];
listProduct.addListSelectionListener(new ListSelectionListener()
{public void valueChanged(ListSelectionEvent e)
{
index = listProduct.getSelectedIndices();
System.out.println(index[index.lenth-1]);
}
});
但总是报:local variable index is accessed from within inner class:needs to be declared final
难道内部类不能访问外面的非静态变量,请高手指点一下,好郁闷
你可以把index定义为外部类的成员变量,或者加上final关键字就OK了