这是我的第一篇文章,所以任何关于如何使我的文章更好的建议都会很棒!
在这个网站的研究帮助下,我能够生成一个表格,我可以更改其字体、更改对齐方式并保持当前的外观和感觉。但是,我不能更改颜色并且不确定该怎么做。任何建议将不胜感激!
创建 JTable 后,我调用:
table.getTableHeader().setDefaultRenderer(new HeaderRenderer(table));
private class HeaderRenderer implements TableCellRenderer {
DefaultTableCellRenderer renderer;
public HeaderRenderer(JTable table) {
setOpaque(true);//this is an attempt to change color
renderer = (DefaultTableCellRenderer)
table.getTableHeader().getDefaultRenderer();
renderer.setHorizontalAlignment(JLabel.CENTER);
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
Component cell = renderer.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);
setBackground(headerColor);//this is an attempt to change color
//cell.setBackground(headerColor);//this is an attempt to change color
cell.setFont(headerFont);//this successfully changes font
return cell;
}
}
谁能指出我的方向或帮助我创建一个可以着色、居中并具有“Nimbus”外观和感觉的表头?