日期:2014-05-20  浏览次数:20774 次

内部类不能访问外部类的变量吗?
Java code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class InformationTable extends JTable {

    public InformationTable(AlterPanel alterPanel) {
        super(new ResultTableModel());
        
        this.getTableHeader().setReorderingAllowed(false);
        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent me) {
                int rowIndex = getSelectedRow();
                JTextField[] fieldList = { alterPanel.getIdField(),
                        alterPanel.getNameField(), alterPanel.getSexField(),
                        alterPanel.getBirthField(), alterPanel.getClassField(),
                        alterPanel.getDepartmentField() };
            }
        });
    }

}



为什么下面那段代码总是报错?
Java code

JTextField[] fieldList = { alterPanel.getIdField(),
                        alterPanel.getNameField(), alterPanel.getSexField(),
                        alterPanel.getBirthField(), alterPanel.getClassField(),
                        alterPanel.getDepartmentField() };



报错是:Cannot refer to a non-final variable alterPanel inside an inner class defined in a different method

为什么呢?


------解决方案--------------------
public InformationTable(final AlterPanel alterPanel)