如何在 Windows LookAndFeel 中更改 JTableHeader 的原色?

How to change the primary color of a JTableHeader in Windows LookAndFeel?

我已经创建了一个类似的 post,我在编辑中提出了这个问题。但我不认为很多人会这样看。我的问题是,我如何更改 JTableHeader 的背景颜色(即列的颜色)。我知道正常

table.getHeader().setBackground(Color.blue);

应该可以,但是当我在 Windows 外观上设置 LookAndFeel 时,这不起作用。 com.sun.java.swing.plaf.windows.WindowsLookAndFeel

推荐使用以下外观和费用。

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

我在下面提供了完整的可运行示例代码,您可以在其中看到蓝色 table header。

import javax.swing.*;
import java.awt.*;

public class TableHeaderExample {
  private JFrame jFrame;

  public TableHeaderExample() {
    jFrame = new JFrame();
  }

  private void setLookAndFeel() {
    try {
      UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
      //Do not use below.
//      UIManager.setLookAndFeel(
//              "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void createTableWithColorHeader() {
    setLookAndFeel();
    String data[][] = {{"Sambit", "23", "1000"}, {"John", "25", "200"}};
    String column[] = {"EMP NAME", "EMP AGE", "EMP SALARY"};
    JTable jTable = new JTable(data, column);
    jTable.getTableHeader().setBackground(Color.blue);
    jTable.setBounds(30, 40, 200, 300);
    JScrollPane sp = new JScrollPane(jTable);
    jFrame.add(sp);
    jFrame.setSize(300, 400);
    jFrame.setVisible(true);
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }


  public static void main(String[] args) {
    TableHeaderExample example = new TableHeaderExample();
    example.createTableWithColorHeader();
  }
}

见下图。

另见下文link。 http://leo.ugr.es/elvira/devel/Tutorial/Java/uiswing/misc/plaf.html 引用一行是

IManager.getCrossPlatformLookAndFeelClassName() Returns the string for the one look-and-feel guaranteed to work -- the Java Look & Feel. UIManager.getSystemLookAndFeelClassName() Specifies the look and feel for the current platform. On Win32 platforms, this specifies the Windows Look & Feel