找到了答案……天哪,这让我想起了为什么我认为 Java Swing 是 Java 的一个非常糟糕的领域。
如果您想为应用程序中的每个 JTable 更改它,您可以使用 UIManager 设置下拉线的颜色:
UIManager.put("Table.dropLineColor", Color.cyan);
UIManager.put("Table.dropLineShortColor", Color.cyan);
如果您只想为一个表设置它,那么您必须为您的表设置自定义 UI:
myTable.setUI(new CustomTableUI());
CustomTableUI 然后确保在 UIManager 中,dropLine 的默认颜色在绘制线条之前更改。之后,恢复默认值:
private class CustomTableUI extends BasicTableUI {
@Override
public void paint(Graphics g, JComponent c) {
// Store defaults
Color dropLineColor = UIManager.getColor("Table.dropLineColor");
Color dropLineShortColor = UIManager.getColor("Table.dropLineShortColor");
// Set your custom colors here
UIManager.put("Table.dropLineColor", Color.cyan);
UIManager.put("Table.dropLineShortColor", Color.cyan);
// Allow the table to be painted
super.paint(g, c);
// Restore the defaults
UIManager.put("Table.dropLineColor", dropLineColor);
UIManager.put("Table.dropLineShortColor", dropLineShortColor);
}
}