简体   繁体   中英

Keeping the specified color for JTabbedPane

I am having this Layout in JTabeedPane with the background as shown. The background color is not changing to the color as shown. I am the following structure while designing.

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])

The white color isn't enabled in any of the Foreground or Background colors but it's still visible there. I tested it while keeping the components Opaque and disabling Opaque but no effect. How do I remove it?

在此处输入图片说明

I'm afraid this is not as trivial as one might expect because this formatting is completely managed by the Look and Feel.
However you can call tabbedPane.setUI(yourCustomUI); to set a custom UI.

I have prepared a UI for you to use, hope it safes you some time (see inline-comments to customize):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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