我们能否在 Java 的 JTable 中隐藏表头?
是的,我们可以隐藏表格的表头。使用 setTableHeader() 方法并将它设为 null -
table.setTableHeader(null);
上面的 table 是我们的 JTable -
JTable table = new JTable(marks, col)
以下是一个隐藏表头的示例 -
示例
package my;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
public class SwingDemo {
public static void main(String[] argv) throws Exception {
Integer[][] marks = {
{ 70, 66, 76, 89, 67, 98 },
{ 67, 89, 64, 78, 59, 78 },
{ 68, 87, 71, 65, 87, 86 },
{ 80, 56, 89, 98, 59, 56 },
{ 75, 95, 90, 73, 57, 79 },
{ 69, 49, 56, 78, 76, 77 }
};
String col[] = { "S1", "S2", "S3", "S4", "S5", "S6"};
JTable table = new JTable(marks, col);
Font font = new Font("Verdana", Font.PLAIN, 12);
table.setFont(font);
table.setRowHeight(30);
table.setBackground(Color.blue);
table.setForeground(Color.white);
table.setTableHeader(null);
JFrame frame = new JFrame();
frame.setSize(600, 400);
frame.add(new JScrollPane(table));
frame.setVisible(true);
}
}以下是输出。我们设置了上面的表头,但它不可见,因为它后来被设为 null -
输出
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP