Java 如何更改JTabbedPane的背景色?

Java 如何更改JTabbedPane的背景色?,java,swing,background-color,jtabbedpane,Java,Swing,Background Color,Jtabbedpane,我知道你能做到,但你怎么能不这样做就做到这一点呢?我问这个问题只是因为挫折似乎不起作用 请注意,我希望更改以下属性: TabbedPane.background(或TabbedPane.contentAreaColor?) TabbedPane.tabAreaBackground 例如,setBackgroundAt()似乎有效: private void initTabComponent(int i) { pane.setTabComponentAt(i, new ButtonTabC

我知道你能做到,但你怎么能不这样做就做到这一点呢?我问这个问题只是因为挫折似乎不起作用

请注意,我希望更改以下属性:

  • TabbedPane.background
    (或
    TabbedPane.contentAreaColor
    ?)
  • TabbedPane.tabAreaBackground
  • 例如,
    setBackgroundAt()
    似乎有效:

    private void initTabComponent(int i) {
        pane.setTabComponentAt(i, new ButtonTabComponent(pane));
        pane.setBackgroundAt(i, Color.getHSBColor((float)i/tabNumber, 1, 1));
    }
    
    附录:正如@camickr有益地观察到的,目标组件必须是

    导入java.awt.Color;
    导入java.awt.Dimension;
    导入java.awt.EventQueue;
    导入javax.swing.JFrame;
    导入javax.swing.JLabel;
    导入javax.swing.JPanel;
    导入javax.swing.JTabbedPane;
    /**@见http://stackoverflow.com/questions/8752037 */
    公共类选项卡扩展了JPanel{
    专用静态最终整数最大值=5;
    专用最终JTabbedPane窗格=新JTabbedPane();
    公共颜色(){
    对于(int i=0;i
    您还可以执行以下操作:

    for (int i = 0; i < this.getTabCount(); i++) {
        this.getComponentAt(i).setBackground(Color.DARK_GRAY);
    }
    
    for(int i=0;i

    for(int i=0;i

    对于选项卡和面板背景

    您是指选项卡本身的颜色吗?我指的是选项卡标题(即标题所在的位置)和内容区域。我没有看到setContentAreaBackground()方法,因此看起来您需要创建自定义UI来完成此操作。但这只设置选项卡标题颜色,而不是内容区域…对吗?对。正如@camickr之前所评论的,目标组件必须是。似乎不透明又让我失望了!:[我很困惑,我以为“内容区”是显示您添加到选项卡的组件的区域。我的建议是使组件不透明,以便您可以看到内容区域的背景。但是,这不起作用,因此我删除了我的评论。我不明白此建议如何影响内容区域。也许有人可以发布SSCCE(本来应该对原始问题进行此操作)来演示解决方案。很好的示例。看起来唯一的解决方案是设置作为选项卡添加的每个组件的背景色。在Windows上使用金属LAF时,
    setBackground()
    方法会更改所有选项卡的背景(但不是内容区域)。在我看来,这个UI有点混乱。
    for (int i = 0; i < this.getTabCount(); i++) {
        this.getComponentAt(i).setBackground(Color.DARK_GRAY);
    }
    
    for (int i = 0; i < this.getTabCount(); i++) {
                this.setBackgroundAt(i, Color.DARK_GRAY);
                this.getComponentAt(i).setBackground(Color.DARK_GRAY);
    }