我正在创建一个包含3列的JTable,并将标题的颜色设置为color.CYAN。
问题是在JTable结束后,该行的其余部分仍然是彩色的CYAN,即使根本不应该有一个表。
我已经尝试修改列数,但似乎无法做到。
以下是我的代码:
class SimpleTableExample extends JFrame {
public static JFrame frame = new JFrame();
public static void frameGui(JPanel panel, String name){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.setSize(500,200);
frame.setVisible(true);
}
public static JPanel Table(){
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
String columnNames[] = { "Column 1", "Column 2", "Column 3" };
String dataValues[][] =
{
{ "12", "234", "67" },
{ "-123", "43", "853" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "93", "89.2", "109" },
{ "279", "9033", "3092" }
};
JTable table = new JTable( dataValues, columnNames );
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
JTableHeader header = table.getTableHeader();
header.setBackground(Color.cyan);
JScrollPane scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );
return topPanel;
}
public static void main( String args[] )
{
frameGui(Table(),"TEST");
}
}
答案 0 :(得分:3)
这将是预期的行为,因为JTableHeader
会扩展JScrollPane
标题区域的整个宽度。
相反,更改JTableHeade
r的渲染器,就像JTable
的dell渲染器一样