0

我是一名 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);
    }
});

但是当我点击那个按钮时,表格没有出现。

4

1 回答 1

2

带有 SpringLayout 的简单应用程序

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

frame.getContentPane().add(scrollPane); 

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

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

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

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

于 2014-10-21T16:23:01.087 回答