java 为什么我不能使用 DefaultTableModel?我错过了一些明显的东西吗?(爪哇)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13851788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:10:54  来源:igfitidea点击:

Why can't I use DefaultTableModel? Am I missing something Obvious? (Java)

javaswingjtabledefaulttablemodel

提问by Giga Tocka

Here is my code:

这是我的代码:

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);
        }
    }

When I try to compile, it says it doesn't recognize DefaultTableModel. Also, the code above is part of my main and I'm sure I've imported the right libraries.

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

Here is the error:

这是错误:

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

Help please?

请帮忙?

回答by MadProgrammer

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

Isn't enough.

还不够。

DefaultTableModellives in the javax.swing.tablepackage, you need to include it in with your imports...

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

import javax.swing.table.*