JButton[] button = new JButton[noOfDays];
for(int j=0 ;j<studentNameList.size() ;j++) {
for(int i=0 ;i<button.length ;i++) {
button[i]=new JButton((i+1)+"");
attendencepanels.add(button[i]);
button[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
----> button[i].setBackground(Color.red); //Local variable refenced from inner class must be final or effective final
//JOptionPane.showMessageDialog(null, "test");
}
});
我如何解决actionPerformed方法中button[i]中的这个问题
最佳答案
虽然我不确定,但您也许可以从 ActionEvent e
参数获取 JButton
引用。看起来这就是 getSource()
返回的内容(事件最初发生的对象
):
而不是
button[i].setBackground(Color.red);
尝试
JButton button = (JButton) e.getSource();
button.setBackground(Color.red);
关于java - 错误: local variable referenced from inner class must be final or effective final,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39483665/