gpt4 book ai didi

java - JTable 未出现 - 包含 addRow

转载 作者:行者123 更新时间:2023-11-29 03:11:52 24 4
gpt4 key购买 nike

代码运行没有错误,但 JTable 没有出现。

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.Dimension;

public class CreateTable {

public static void main(String[] args) {
//init table
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
model.addColumn("col1");
model.addColumn("col2");
model.addColumn("col3");
model.addColumn("col4");

for (int i=1;i<=100;i++){
model.addRow(new Object[]{i,1,2,3}); //output jtable row
}

table.setPreferredScrollableViewportSize(new Dimension(500,50));
table.setFillsViewportHeight(true);
table.setSize(600, 200);
table.setVisible(true);
}
}

最佳答案

您可以查看教程或示例(http://docs.oracle.com/javase/tutorial/uiswing/examples/components/)

public static void main(String[] args) {
//init frame - main component
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JPanel panel = new JPanel();
frame.setContentPane(panel);

DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
model.addColumn("col1");
model.addColumn("col2");
model.addColumn("col3");
model.addColumn("col4");

for (int i=1;i<=100;i++){
model.addRow(new Object[]{i,1,2,3}); //output jtable row
}
table.setSize(600, 200);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}

关于java - JTable 未出现 - 包含 addRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28905216/

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