gpt4 book ai didi

java - JTable 不显示

转载 作者:行者123 更新时间:2023-12-02 05:21:23 25 4
gpt4 key购买 nike

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

但是当我单击该按钮时,表格不会显示。

最佳答案

simple app with a SpringLayout

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

frame.getContentPane().add(scrollPane); 

使用 SpringLayout 时,您需要指定一些约束。阅读 Swing 教程中关于 How to Use SpringLayout 的部分了解更多信息。

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

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

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

关于java - JTable 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26490226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com