保留JTabbedPane的指定颜色

时间:2020-02-06 10:29:20

标签: java swing jtabbedpane

我在JTabeedPane中有此布局,背景如图所示。背景颜色未更改为如图所示的颜色。我在设计时使用的是以下结构。

JFrame (Background color: [0,115,153])
 JPanel (Background color: [3,50,67])
  JPanel (Background color: [16,110,173])
   ...
  JPanel  (Background color: [3,50,67])
   JTabbedPane  (Background color: [3,50,67])
    Tab 1,2,3   (Background color: [3,50,67])

任何前景色或背景色均未启用白色,但仍可以在其中看到白色。我在保持组件 Opaque 并禁用 Opaque 的同时进行了测试,但是没有任何效果。如何删除它?

enter image description here

1 个答案:

答案 0 :(得分:2)

恐怕这并不像人们期望的那么简单,因为这种格式完全由外观进行管理。
但是您可以调用tabbedPane.setUI(yourCustomUI);来设置自定义UI。

我已经准备好一个UI供您使用,希望它可以使您安全一些(请参阅inline-comments以进行自定义):

// your code...

tabbedPane.setUI(new MinimalisticTabbedPaneUI());

// your code...

public static class MinimalisticTabbedPaneUI extends BasicTabbedPaneUI {

    // paints the border around the currently visible content
    @Override
    protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
        // do nothing
    }

    // paints the border around each tab
    @Override
    protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h, boolean isSelected) {
        // only paints a border if the tab is selected, edit this to add your personal formatting
        if(isSelected){
            super.paintTabBorder(g, tabPlacement, tabIndex, x, y, w, h, isSelected);
        }
    }
}