0

这是我的代码:

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

 class Test{ 
        static CardLayout cardLayout;  
        static JPanel card = new JPanel();

        public static void main(String[] args) {
            JFrame frame = new JFrame("AddressBook");
            JPanel contentPane = (JPanel)frame.getContentPane();
            card.setLayout(cardLayout = new CardLayout());                          

            JPanel cardTop = new JPanel();
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("Name");
            model.addColumn("Number");
            String[] John = {"John", "1234"};
            model.addRow(John);
            String[] Beth = {"Beth", "4444"};
            model.addRow(John);
            JTable table = new JTable(model);       
            JScrollPane jsp = new JScrollPane(table);
            cardTop.add(jsp);


            card.add("Card Top", cardTop);
            contentPane.add(card);

            frame.setVisible(true);
            frame.setSize(507, 191);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
        }
    }

当我尝试编译时,它说它无法识别 DefaultTableModel。此外,上面的代码是我的主要代码的一部分,我确信我已经导入了正确的库。

这是错误:

Test.java:15: error: cannot find symbol
            DefaultTableModel model = new DefaultTableModel();
            ^
  symbol:   class DefaultTableModel
  location: class Test
Test.java:15: error: cannot find symbol
            DefaultTableModel model = new DefaultTableModel();
                                          ^
  symbol:   class DefaultTableModel
  location: class Test
2 errors

请帮忙?

4

1 回答 1

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

还不够。

DefaultTableModel存在于javax.swing.table包中,您需要将其包含在您的导入中...

import javax.swing.table.*
于 2012-12-13T02:42:35.043 回答