设置面板的 backgroundColor 在 JTabbedPane 外部有效,但在内部无效

Setting backgroundColor of panel works outside of JTabbedPane but not inside

我遇到问题,在我的 JTabbedPane 中时,我的内部面板的背景颜色未设置(或未显示)

我有以下结构:

JPanel BorderLayout:
   JTabbedPane (BorderLayout.Center):
      JPanel Default
      ..

当我将内部 JPanel 添加到位于 BorderLayout.CENTER 的 borderLayout 而不是 JTabbedPane 时,我可以看到背景颜色,但是如果我的内部面板位于选项卡式窗格中并且选项卡式窗格被添加到中心那么背景颜色是默认的并且似乎覆盖了它。

panel1.setBackground(Color.WHITE);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add(panel1);

//add tabbed pane to panel with borderlayout
//Background color of this panel is also set previously
add(tabbedPane, BorderLayout.CENTER);

如何使所需组件透明

package javaapplication31;

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

import java.awt.Dimension;


public class JavaApplication31
{
    public static void main(String[] args)
    {
        Runnable r = new Runnable()
        {
            @Override
            public void run()
            {
                //let's make all TabbedPanes transparent by default
                UIManager.put("TabbedPane.contentOpaque", false);

                JFrame frame = new JFrame();

                JPanel parentPanel = new JPanel();
                parentPanel.setBackground(Color.WHITE);
                frame.add(parentPanel);

                JTabbedPane tp = new JTabbedPane();               
                parentPanel.add(tp);

                //Make transparent panel
                JPanel panelTransparent = new JPanel();
                panelTransparent.setOpaque(false); // this makes panel transparent
                panelTransparent.setPreferredSize(new Dimension(200, 200));
                tp.addTab("Transparent", panelTransparent);

                //Make panel (by default panel is opaque)
                JPanel panelOpaque = new JPanel();
                tp.addTab("Opaque", panelOpaque);

                frame.setPreferredSize(new Dimension(300, 300));
                frame.pack();
                frame.setVisible(true);
            }
        };
        EventQueue.invokeLater(r);
    }
}