简体   繁体   English

如何删除JtabbedPane后面的背景或更改其颜色

[英]How can I remove the background behind the JtabbedPane, or change its colour

在此处输入图片说明

Hi, I have a JTabbedPane as above. 嗨,我有一个如上所述的JTabbedPane。 I want to remove, or change the colour of the background, behind the tab "Select Tab." 我想删除或更改标签“选择标签”后面的背景颜色。 I've been searching for a long time and cannot find any info on it 我已经搜索了很长时间,找不到任何信息

Behind the tab means.. outer container. 标签的后面是指外部容器。 Use setBackground(Color) method on outer container. 在外部容器上使用setBackground(Color)方法。 See below code. 参见下面的代码。

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

public class TabbedPaneTabColor extends JPanel {
    public TabbedPaneTabColor() {
        initializeUI();
    }

    private void initializeUI() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(400, 200));


        JTabbedPane pane = new JTabbedPane();
        setBackground(Color.BLACK);// This line sets the JPANEL(container) color to black

        pane.addTab("A Tab", new JPanel());
        pane.addTab("B Tab", new JPanel());
        pane.addTab("C Tab", new JPanel());
        pane.addTab("D Tab", new JPanel());


        pane.setForeground(Color.BLACK);


        pane.setBackgroundAt(0, Color.RED);
        pane.setBackgroundAt(1, Color.GREEN);
        pane.setBackgroundAt(2, Color.YELLOW);
        pane.setBackgroundAt(3, Color.ORANGE);

        this.add(pane, BorderLayout.CENTER);
    }

    public static void showFrame() {
        JPanel panel = new TabbedPaneTabColor();
        panel.setOpaque(true);

        JFrame frame = new JFrame("JTabbedPane Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TabbedPaneTabColor.showFrame();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM