JTable does not show up



我是Java初学者,正在使用Eclipse创建一个简单的应用程序,其中包含SpringLayout和一个按钮。我把这个按钮称为"btnTABLE",这是它的actionPerformed代码:

btnTABLE.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        String[] columnNames = {"First Name",
                "Last Name",
                "Sport",
                "# of Years",
                "Vegetarian"};
        Object[][] data = {
                {"Kathy", "Smith",
                 "Snowboarding", new Integer(5), new Boolean(false)},
                {"John", "Doe",
                 "Rowing", new Integer(3), new Boolean(true)},
                {"Sue", "Black",
                 "Knitting", new Integer(2), new Boolean(false)},
                {"Jane", "White",
                 "Speed reading", new Integer(20), new Boolean(true)},
                {"Joe", "Brown",
                 "Pool", new Integer(10), new Boolean(false)}
            };
        JTable table = new JTable(data, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
    }
});

但当我点击那个按钮时,表格就不会出现了。

带有SpringLayout 的简单应用程序

SpringLayout是一个复杂的布局管理器。你可以使用简单的代码,比如:

frame.getContentPane().add(scrollPane); 

使用SpringLayout时,您需要指定一些约束。有关更多信息,请阅读Swing教程中关于如何使用SpringLayout的部分。

此外,如果您试图动态地将组件添加到可见的GUI中,那么基本代码是:

panel.add(...);
panel.revalidate();
panel.repaint();

我建议您应该为面板使用不同的布局管理器。

最新更新