我想做一个计分板。我正在使用表来显示数据。数据将显示,但表头将不显示。我已经检查了其他文章,并且也输入了JScrollPane
。
import javax.swing.*;
public class Table {
JFrame car;
Table() {
car = new JFrame();
car.setTitle("Career");
car.setSize(500, 500);
car.setVisible(true);
car.setLayout(null);
String[] column = {"No.", "User Name", "Total Score"};
String row[][] = {{"101", "Amit", "670000"},
{"102", "Jai", "780000"},
{"101", "Sachin", "700000"}};
JTable jt = new JTable(row, column);
jt.setBounds(20, 100, 225, 200);
jt.setRowHeight(25);
JScrollPane sp = new JScrollPane(jt);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
car.add(sp);
car.add(jt);
}
public static void main(String[] args) {
new Table();
}
}
在其他网站上,我看不到任何缺少的代码。有什么问题吗?
car.add(jt);
。该表位于滚动窗格中,并且该滚动窗格已添加到GUI中。car.setLayout(null);
和jt.setBounds(20,100,225,200);
。空布局会弄乱很多事情,包括滚动窗格的工作方式。 setVisible
之后调用pack()
。import javax.swing.*;
public class Table {
JFrame car;
Table() {
car = new JFrame();
car.setTitle("Career");
// this is just a guess. Add components then pack()
//car.setSize(500, 500);
//car.setLayout(null);
String[] column = {"No.", "User Name", "Total Score"};
String row[][] = {{"101", "Amit", "670000"},
{"102", "Jai", "780000"},
{"101", "Sachin", "700000"}};
JTable jt = new JTable(row, column);
//jt.setBounds(20, 100, 225, 200);
jt.setRowHeight(25);
JScrollPane sp = new JScrollPane(jt);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
car.add(sp);
//car.add(jt);
car.pack();
// this statement should be last, immediately after pack()
car.setVisible(true);
}
public static void main(String[] args) {
new Table();
}
}
我找到了我想要的另一种方式。