用JTable画的表格看不见怎么办啊

2024-12-30 07:14:18
推荐回答(2个)
回答1:

你是这样写的吗???
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JFrame;

public class test extends JFrame{
//JFrame 需要加内容窗格
//JScrollPane 在事例化的时候就添加表格对象
public test(){
JTable ta=new JTable(5,5);
JScrollPane sp=new JScrollPane();
sp.add(ta);
add(sp);
this.setSize(500,400);
show();
}
public static void main(String[] args) {
new test();
}
}

应该这样写 或者类似这样写
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JFrame;

public class test extends JFrame{
public test(){
JTable ta=new JTable(5,5);
JScrollPane sp=new JScrollPane(ta);
getContentPane().add(sp);
pack();
show();
}
public static void main(String[] args) {
new test();
}
}

回答2:

String[] tableName = {"IP","主机名"};
Object [][] lanwa={};
Class []datatype={ String.class, String.class };
DefaultTableModel tableModel = new DefaultTableModel(lanwa,tableName);
JTable table = new JTable(tableModel);
JScrollPane jScrollPane1 = new JScrollPane(table);

记着import