Set the Background Color for JTabbedPane

后端 未结 6 853
傲寒
傲寒 2021-01-06 13:20

I am using Nimbus Look and feel. I needs to change the Background color and foreground color of the tab in JTabbedPane but the color doesn\'t set in JTabbedPane. I tried set

相关标签:
6条回答
  • 2021-01-06 13:58

    There are a few different things you can do, depending upon how much control you want over the exact color. The easiest way is to change some of the properties in the UIManager to change the colors that Nimbus derives its other colors from. I played around a little with your code, and found that if I put the following code after the call to UIManager.setLookAndFeel(), it would approximate the red-on-black look you attempted in your example:

         UIManager.put("nimbusBase", new ColorUIResource(0, 0, 0));
         UIManager.put("textForeground", new ColorUIResource(255, 0, 0));
    

    I'll leave it to you to experiment. For more information to experiment with, there is a good article on configuring Nimbus here. Be sure you look at his link titled "Nimbus UIDefaults Properties List". Outside of just massaging the colors to something similar to what you want, you'll have to start doing messy things like implementing Painter classes that do custom painting.

    0 讨论(0)
  • 2021-01-06 14:06

    Actually not true

    new JTabbedPane().setUI(new YourUI());
    

    and then you create your simple UI

    public class YourUI extends BasicTabbedPaneUI{
    
    .....
    
    private class ScrollableTabPanel extends JPanel implements UIResource {
    public ScrollableTabPanel() {
        setLayout(null);
    }
    
    public void paintComponent(Graphics g) {
    
    super.paintComponent(g);
    
    g.setColor(Color.red);
    g.fillRect(tabPane.getBounds());            
    
            }
        }
    
    ....
    
    }
    

    Jan Cajthaml

    There is how it should look like (don't mind the QuadSpline tabs and close bars thats another add-ons of my UI implementation), my reputation here is none so there is a link to the image

    http://a5.sphotos.ak.fbcdn.net/hphotos-ak-ash3/166028_2974620064454_896181702_n.jpg

    0 讨论(0)
  • 2021-01-06 14:07

    If you want to change the actual content, there are two useful methods of the tabbed pane: setForegroundAt and setBackgroundAt. You can just loop through all the tabs and call these:

    for (int i = 0; i < pane.getTabCount(); i++) {
        pane.setForegroundAt(i, foregroundColor);
        pane.setBackgroundAt(i, backgroundColor);
    }
    

    You can also use getComponentAt as well, similarly:

    for (int i = 0; i < pane.getTabCount(); i++) {
        pane.getComponentAt(i).setForeground(foregroundColor);
        pane.getComponentAt(i).setBackground(backgroundColor);
    }
    

    The latter approach is more flexible--you can later do more complex things to all the components using code like this.

    0 讨论(0)
  • 2021-01-06 14:16

    JTabbedPane works when you add components to it, e.g add 5 JPanels and they will be tabbed.

    0 讨论(0)
  • 2021-01-06 14:17

    I hate to break it to you, but you're going to have a very hard time getting the foreground and background colors changed. Unlike many basic components (JTextField, JLabel, etc), the JTabbedPane ignores the foreground and background colors when setting up to paint. Instead it allows the UI delegate (as determined by the current Look and Feel) to choose the colors.

    Some Look and Feels allow for color themes (Substance comes to mind), but Nimbus seems to have those color values hard coded into the paint delegates.

    More than likely you'll need to implement your own UI delegate based off of Nimbus's. See here for more information. It's going to be a major pain, so ask yourself how much you want those colors changed.

    Lastly, if anyone tells you to simply subclass JTabbedPane and set the colors in your own "paintComponent" method, ignore it. You then becomes responsible for the painting of the entire component, and will lose any Nimbus-like visual attributes.

    0 讨论(0)
  • 2021-01-06 14:17

    Sorry for my english.

    I tried resolve this problem 2 days.. There are "work around" solve.

    Need set tab component. And then set color for this component.

    /*
      ...
      Setting LAF Nimbus
      ...
    */
    
    JTabbedPane tp = new JTabbedPane();
    
    /*
       ...
       add tabs in TabbedPane
       ...
    */
    

    then for specified tabs need do:

            JLabel title = new JLabel(tp.getTitleAt(tabIndex));
            title.setForeground(Color.RED);
            tp.setTabComponentAt(tabIndex, title);
    

    OMG!! its work!

    0 讨论(0)
提交回复
热议问题