Java 错误:从内部类引用的局部变量必须是final或effective final JButton[]按钮=新的JButton[noOfDays]; 对于(int j=0;j
,虽然我不确定,但您可以从Java 错误:从内部类引用的局部变量必须是final或effective final JButton[]按钮=新的JButton[noOfDays]; 对于(int j=0;j,java,swing,Java,Swing,,虽然我不确定,但您可以从ActionEvent e参数中获取JButton引用。这似乎是getSource()返回的(事件最初发生的对象): 而不是 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)+""
ActionEvent e
参数中获取JButton
引用。这似乎是getSource()
返回的(事件最初发生的对象
):
而不是
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");
}
});
试一试
虽然我不确定,但您可以从
ActionEvent e
参数中获取JButton
引用。这似乎就是getSource()
返回的(事件最初发生的对象
):
而不是
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");
}
});
试一试
变量i在此上下文中被修改,因此编译器无法处理它。请为它指定一个不变的引用:
JButton button = (JButton) e.getSource();
button.setBackground(Color.red);
for(int j=0;j变量i在此上下文中被修改,因此编译器无法处理它。请为它指定一个不变的引用:
JButton button = (JButton) e.getSource();
button.setBackground(Color.red);
for(int j=0;jMake the button Array finalfinal JButton[]button=new JButton[noOfDays];
根据@jenn的建议将其设置为类的全局或final。提示:下次做一些事先的研究。可能会重复设置按钮数组finalfinal JButton[]button=new JButton[noOfDays];
根据@jensAnd的建议,将其设置为全局或最终版本。提示:下次做一些事先的研究。如果有助于解决问题,则可能复制“请”。如果有助于解决问题,请复制“请”。