【问题标题】:How to change background color of JTabbedPane?如何更改 JTabbedPane 的背景颜色?
【发布时间】:2012-02-03 20:09:57
【问题描述】:

我知道你可以modify the LaF properties,但是如果不这样做,你如何做到这一点?我只是问,因为setBackground 似乎没有这样做。

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

  1. TabbedPane.background(或TabbedPane.contentAreaColor?)
  2. TabbedPane.tabAreaBackground

【问题讨论】:

  • 你的意思是标签本身的颜色
  • 我的意思是标签页眉(即标题所在的位置)和内容区域。
  • 我没有看到 setContentAreaBackground() 方法,所以看起来您需要创建一个自定义 UI 来执行此操作。

标签: java swing background-color jtabbedpane


【解决方案1】:

您还可以执行以下操作:

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);
}

标签和面板背景

【讨论】:

    【解决方案2】:

    TabComponentsDemo 为例,setBackgroundAt() 似乎可以工作:

    private void initTabComponent(int i) {
        pane.setTabComponentAt(i, new ButtonTabComponent(pane));
        pane.setBackgroundAt(i, Color.getHSBColor((float)i/tabNumber, 1, 1));
    }
    

    附录:正如@camickr 所言,目标组件必须是opaque

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    
    /** @see http://stackoverflow.com/questions/8752037 */
    public class TabColors extends JPanel {
    
        private static final int MAX = 5;
        private final JTabbedPane pane = new JTabbedPane();
    
        public TabColors() {
            for (int i = 0; i < MAX; i++) {
                Color color = Color.getHSBColor((float) i / MAX, 1, 1);
                pane.add("Tab " + String.valueOf(i), new TabContent(i, color));
                pane.setBackgroundAt(i, color);
            }
            this.add(pane);
        }
    
        private static class TabContent extends JPanel {
    
            private TabContent(int i, Color color) {
                setOpaque(true);
                setBackground(color);
                add(new JLabel("Tab content " + String.valueOf(i)));
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        }
    
        private void display() {
            JFrame f = new JFrame("TabColors");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TabColors().display();
                }
            });
        }
    }
    

    【讨论】:

    • 但这只是设置标签页眉颜色,而不是内容区域...对吗?
    • 对。正如@camickr 之前评论的那样,目标组件必须是opaque
    • 似乎不透明又吸引了我! :[
    • 我很困惑。我认为“内容区域”是显示您添加到选项卡的组件的区域。我的建议是使组件不透明,以便您可以看到内容区域的背景?但是,这没有用,所以我删除了我的评论。我不明白这个建议如何影响内容区域。也许有人可以发布一个 SSCCE(无论如何都应该用原始问题完成)来展示解决方案。
    • 很好的例子。看起来唯一的解决方案是设置作为选项卡添加的每个组件的背景颜色。在 Windows 上使用 Metal LAF 时,setBackground() 方法会更改所有选项卡的背景(但不会更改内容区域)。在我看来,这个 UI 有点混乱。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2018-02-13
    相关资源
    最近更新 更多