不了解定义的Constructor的Java语法

时间:2013-09-14 17:14:33

标签: java swing constructor

我正在实现一种表格。为此,我使用了AbstractTableModel,但我不知道我的代码在做什么(这就是为什么我得到了一个完成他的工作的片段,但显然我想理解)。你可以看到代码

public class TablePanel extends JPanel
{
    private JTable table;
    public TablePanel()
    {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
        table = new JTable(new MyTableModel());
        table.setFillsViewportHeight(true);     
        table.setPreferredScrollableViewportSize(new Dimension(500, 100));
        JScrollPane jps = new JScrollPane(table);
        add(jps);
        add(new JScrollPane(table));
        table.setCellSelectionEnabled(true);
    }
    private class MyTableModel extends AbstractTableModel {
        private String[] columns = {"","Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        private String[][] data = {{"8:30 - 9:30","","","","","",""},
            {"9:30 - 10:30","","","","","",""},
            {"10:30 - 11:30","","","","","",""},
            {"11:30 - 12:30","","","","","",""},
            {"12:30 - 13:30","","","","","",""},
            {"13:30 - 14:30","","","","","",""},
            {"14:30 - 15:30","","","","","",""},
            {"15:30 - 16:30","","","","","",""},
            {"16:30 - 17:30","","","","","",""}};

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

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

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

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
        public Object myMethod (){
            //do something
        }
    }
}

嗯,这是什么意思table = new JTable(new MyTableModel());? 我认为table将是MyTableModel实例,但事实并非如此。实际上,如果我将表传递给另一个外部类,则此类将不会看到myMethod正文中定义的方法MyTableModel

3 个答案:

答案 0 :(得分:1)

MyTableModelTableModel,但TableModel 必然是MyTableModel

如您所知,您的表格模型是MyTableModel的实例,其中包含所需的myMethod(),您可以使用强制转换:

JTable table = new JTable(new MyTableModel());
MyTableModel model = (MyTableModel) table.getModel();

创建和使用对模型的引用可能更容易:

MyTableModel model = new MyTableModel();
JTable table = new JTable(model);

请勿忽略setValueAt()的实施,here

答案 1 :(得分:0)

JTable构造函数接受实现TableModel接口

的类的任何Object

this将帮助您了解更多信息。

答案 2 :(得分:0)

其他类无法看到函数myMethod的预期行为,因为它们“认为”你的MyTableModel只是一个AbstractTableModel(实际上它不是) 您放入MyTableModel的代码和其他所有内容都是正确的,但您无法看到函数myMethod 那是因为你使用的是OOP语言

所以没错。 您正在创建MyTableModel的实例,因此如果您调用

,将执行您在其中执行的所有操作

你应该在这里阅读,这将解释有关OOP部分的更多信息: http://www.dummies.com/how-to/content/understanding-javas-objectoriented-programming-oop.html

只是快速说出来

如果您需要访问myMethod函数,只需将其转换为:      //创建MyTableInstance(这是可能的,因为AbstractTableModel是父项,MyTableModel是子项)      AbstractTableModel atmTestVariable = new MyTableModel();

 // Now to be able to call this:
 // atmTestVariable.myMethod();
 // we need to do this:

 if(atmTestVariable isntanceof MyTableModel) //make sure that atmTestVariable is REALY a MyTableModel
 {
    MyTableModel mtmTemp = (MyTableModel)atmTestVariable; //cast to MyTableModel
    mtmTemp.myMethod(); //Call the function
    // and now the short variant:
    ((MyTableModel)atmTestVariable).myMethod(); //cast to MyTableModel AND call the function
 }

希望我能够提供帮助

哦,请将MyTabelModel类的定义移动到一个单独的文件中:/