繁体   English   中英

JTable没有显示

[英]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();

我建议您为面板使用其他布局管理器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM