无法在同一个类的对象上使用方法

时间:2013-09-15 11:50:04

标签: java object

我正在实现一个JFrame类,它必须查看表并对其执行某些操作。该表有AbstractTableModel,我在其上定义了一个我想在JFrame类中使用的方法。但我无法做到这一点。 这里是代码:

必须查看JTable的JFrame类

public class CreaOrario extends JFrame {


public CreaOrario(int num) {
    initialize(num);
}

/**
 * Initialize the contents of the frame.
 */
private void initialize(final int semestre) {
    this.setSize(1150,650);
    this.setTitle("Creazione dell'Orario");
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    TabellaOrario orarioEditabile = new TabellaOrario(semestre);
    this.getContentPane().add(orarioEditabile, BorderLayout.CENTER);

    final JTable table = orarioEditabile.getTable();
    //now I'd like to do: table.getLesson;
}
}

必须在JFrame中添加并包含表格的JPanel类:

public class TabellaOrario extends JPanel
{
private JTable table;

public TabellaOrario (int semestre)
{

    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
    table = new JTable(new MyTableModel());
    table.setFillsViewportHeight(true);     
    table.setPreferredScrollableViewportSize(new Dimension(510, 350));
    JScrollPane jps = new JScrollPane(table);
    add(jps);
    add(new JScrollPane(table));
    table.setCellSelectionEnabled(true);
    table.setRowHeight(40);

}

public JTable getTable() {
    return table;
}
public class MyTableModel extends AbstractTableModel 
{   


private String[] columns = {"","Lunedì","Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"};


private Object [][] 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","","","","","",""}};



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

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

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

        @Override
        public Object getValueAt(int row, int col) {
            return data[row][col];

        }

        @Override
        public void setValueAt(Object aValue, int row, int col) {
            data[row][col] = aValue;
            fireTableCellUpdated(row, col);

            }

     }
        public String[] getLesson (int row, int column)
        {
            String value = (String) table.getValueAt(row, column);
            int start = value.indexOf("<b>");
            int end = value.indexOf("</b>");
            String insegnamento = value.substring(start + "<b>".length(), end);

            start = value.lastIndexOf("<b>");
            end = value.lastIndexOf("</b>");
            String aula = value.substring(start + "<b>".length(), end);

            return new String[] {insegnamento,aula};

        }
}
}

MyTableModel课程中,我已经定义了我想在getLesson()课程中使用的方法CreaOrario

但如果在MyTableModel我写table.getLesson(),则会显示一条消息错误,其中显示The method getLesson() is undefined for the type JTable。 为什么? table infact是一个TabellaOrario对象,并以MyTableModel为模型。 getLesson()定义了MyTableModel,因此我希望table.getLesson()有效。

2 个答案:

答案 0 :(得分:2)

这是因为table被声明为JTablegetTable()也会返回JTable。您只需要进行类型转换,因为getLesson()中不存在JTable

你需要像这样调用getLesson():

String[] lessons = ((TabellaOrario.MyTableModel)table.getModel()).getLesson();

答案 1 :(得分:0)

您的代码getLesson中的

似乎在TabellaOrario中定义,而不在MyTableModel中定义。这可能是问题......

尝试保持代码的形成,你会发现该方法没有在类

中实现
public class TabellaOrario extends JPanel
{
   private JTable table;
   MyTableModel tm;

   public TabellaOrario (int semestre)
   {

       setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
       tm = new MyTableModel();
       table = new JTable(tm);
       // YOU CAN NOW CALL tm.getLesson() OR table.((MyTableModel)getTableModel()).getLesson();
       table.setFillsViewportHeight(true);     
       table.setPreferredScrollableViewportSize(new Dimension(510, 350));
       JScrollPane jps = new JScrollPane(table);
       add(jps);
       add(new JScrollPane(table));
       table.setCellSelectionEnabled(true);
       table.setRowHeight(40);
   }

   public JTable getTable() {
       return table;
   }

    public class MyTableModel extends AbstractTableModel 
    {   


       private String[] columns = {"","Lunedì","Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"};


       private Object [][] 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","","","","","",""}};



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

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

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

            @Override
            public Object getValueAt(int row, int col) {
                return data[row][col];

            }

            @Override
            public void setValueAt(Object aValue, int row, int col) {
                data[row][col] = aValue;
                fireTableCellUpdated(row, col);

                }

            public String[] getLesson (int row, int column)
            {
                String value = (String) table.getValueAt(row, column);
                int start = value.indexOf("<b>");
                int end = value.indexOf("</b>");
                String insegnamento = value.substring(start + "<b>".length(), end);

                start = value.lastIndexOf("<b>");
                end = value.lastIndexOf("</b>");
                String aula = value.substring(start + "<b>".length(), end);

                return new String[] {insegnamento,aula};

            }
    }
}