0

我首先使用内部类。我正在尝试访问table在内部类中的外部类中声明的变量MyTableModel。但是netbeans显示错误-Cannot find symbol

这是完整的代码。

Import Statements
public class TableDemo extends JPanel {
    private boolean DEBUG = true;

    public TableDemo() {
        super(new GridLayout(1,0));

        JTable table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        JScrollPane scrollPane = new JScrollPane(table);

        add(scrollPane);
    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = {"First Name","Last Name","Sport","# of Years","Dada","Vegiterian"};
        private Object[][] data = {
        {"Kathy", "Smith",
         "Snowboarding", new Integer(5), new Boolean(false),new Boolean(false)},

        };

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col) {
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }

        public void setValueAt(Object value, int row, int col) {

        }

        private void printDebugData() {

            TableColumn column = null;
            for (int i = 0; i < 5; i++) {
            column = table.getColumnModel().getColumn(i);
            if (i == 2) {
              column.setPreferredWidth(100); //third column is bigger
             } else {
                column.setPreferredWidth(50);
    }
}
            int numRows = getRowCount();
            int numCols = getColumnCount();

            for (int i=0; i < numRows; i++) {
                System.out.print("    row " + i + ":");
                for (int j=0; j < numCols; j++) {
                    System.out.print("  " + data[i][j]);
                }
                System.out.println();
            }
            System.out.println("--------------------------");
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("TableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        TableDemo newContentPane = new TableDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

这是我收到错误的代码 -

for (int i = 0; i < 5; i++) {
            column = table.getColumnModel().getColumn(i);
            if (i == 2) {
              column.setPreferredWidth(100); //third column is bigger
             } else {
                column.setPreferredWidth(50);
    }

int 行 -column = table.getColumnModel().getColumn(i);我收到类似的错误 -variable table is not found

请帮忙。

4

3 回答 3

0

您需要一个实例变量才能从内部类访问它。您的变量只有构造函数的本地范围,因此构造函数之外的任何内容都无法找到它。

将您的代码更改为:

public class TableDemo extends JPanel {
    private JTable table;

    public TableDemo() {
        table = new JTable(new MyTableModel());
        //more code
    }
    // more code
}
于 2013-07-01T13:25:43.770 回答
0

您仅在构造函数中本地声明变量。构造函数消失后(超出范围),变量消失了。要正确执行此操作,您应该在字段中定义它:

public class TableDemo extends JPanel {

    private boolean DEBUG = true;
    private JTable table; //define here

    public TableDemo() {
        super(new GridLayout(1,0));
        table = new JTable(new MyTableModel()); //remove Type here
于 2013-07-01T13:24:21.317 回答
0

您在构造函数中定义变量,而不是作为实例成员。构造函数代码结束后,变量超出范围。你需要这样做:

public class TableDemo extends JPanel {
    private boolean DEBUG = true;
    private JTable table;

    public TableDemo() {
        super(new GridLayout(1,0));

        table = new JTable(new MyTableModel());
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        JScrollPane scrollPane = new JScrollPane(table);

        add(scrollPane);
    }

    // Rest of code
于 2013-07-01T13:24:26.313 回答