我的列名没有出现在我的 JTable 中

My column names doesn't show up in my JTable

当我 运行 这个程序时,我的列名没有显示。 我试图在 google 上搜索错误,但似乎没有人遇到此错误。 我已经为数据和列名创建了一个成功的数组,但它仍然不起作用。

public class graphProgram extends JFrame {

    private JPanel inputPanel, graphPanel, buttonPane;
    private JTextField equationInput;
    private JLabel equationLabel;
    private JTable x_yValues;
    private JButton calculateButton;

    public graphProgram() {
        super("Graph Calculator");
        setSize(500, 500);
        setLayout(new BorderLayout());
        addComponents();
        setVisible(true);
    }
    public void addComponents() {
        String[] columnNames = {"X Values", "Y Values"};


        Object[][] data = {{new Integer(-2), ""},
                {new Integer(-1), ""},
                {new Integer(0), ""},
                {new Integer(1), ""},
                {new Integer(2), ""},
                {new Integer(3 ), ""}};

        equationInput = new JTextField();
        calculateButton = new JButton("Calculate");
        equationLabel = new JLabel("Equation: ");
        x_yValues = new JTable(data, columnNames);

        inputPanel = new JPanel(new GridLayout(0, 2));
        graphPanel = new JPanel(new GridLayout(0, 2, 5, 0));
        buttonPane = new JPanel(new GridLayout(0, 1));

        add(inputPanel, BorderLayout.NORTH);
        add(graphPanel, BorderLayout.CENTER);
        add(buttonPane, BorderLayout.SOUTH);

        inputPanel.add(equationLabel);
        inputPanel.add(equationInput);

        graphPanel.add(x_yValues);

        buttonPane.add(calculateButton);
    }
}

你必须把你的 JTable 放在 ScrollPane:

x_yValues = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(x_yValues);

尝试使用 JScrollPane 作为 table 的容器。

JScrollPane scrollPane = new JScrollPane(x_yValues);
graphPanel.add(scrollPane); // replaces graphPanel.add(x_yValues);