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

时间:2015-01-14 12:14:48

标签: java jtabbedpane

enter image description here

嗨,我上面有一个JTabbedPane。我想在“选择标签”选项卡后面删除或更改背景颜色。我已经搜索了很长时间,但找不到任何信息

1 个答案:

答案 0 :(得分:0)

标签后面是......外部容器。在外部容器上使用setBackground(Color)方法。 见下面的代码。

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