Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

java - Set the Background Color for JTabbedPane

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 setForeground(), setForegroundAt(), setBackground() and setBackgroundAt() methods but it isnt works.This is my code


public class TabbedPaneDemo extends JFrame
{
    TabbedPaneDemo()
    {
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        }
        catch(Exception ex) {}

    setLayout(new BorderLayout());
    setBounds(100, 100, 800, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTabbedPane jt = new JTabbedPane();
    jt.addTab("Tab1", new JPanel());
    jt.addTab("Tab2", new JPanel());
    jt.addTab("Tab3", new JPanel());
    jt.addTab("Tab4", new JPanel());

    for( int i = 0; i < jt.getComponentCount(); i++)
    {
        jt.setForegroundAt(i, Color.RED);
        jt.setBackgroundAt(i, Color.BLACK);
    }

    add(jt);

    setVisible(true);
}

public static void main(String args[])
{
    new TabbedPaneDemo();
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...