如何更改jTabbedPane标头的颜色

时间:2014-07-04 15:17:38

标签: java swing jtabbedpane

我想改变jTabbed Pane标题的颜色(标题标题),我想摆脱分隔标题标题及其正文的行。我也喜欢Tabbed Pane的圆边。如何在摇摆中进行所有这些定制?这种内置的gui功能是否有任何外部罐子可用。我不想使用任何外观和感觉这样做,因为其中许多也不能根据我的需要进行定制。由于我是java新手,请给我一个详细的答案。提前谢谢。

1 个答案:

答案 0 :(得分:1)

对于更复杂的外观更改,您应该考虑编写自己的外观,如已经提到的glad3r。 This post also is a great example

对于一些基本的东西,您可能只想更改一些UIManager值,请参阅下面的示例并稍微了解这些值。

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.plaf.BorderUIResource;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.InsetsUIResource;

@SuppressWarnings("serial")
public class TabbedPaneTest extends JTabbedPane {

    public static void main(String[] args) {

        JFrame frame = new JFrame("TabbedPane");
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.RED);
        frame.getContentPane().add(new TabbedPaneTest());
        frame.setVisible(true);
    }

    public TabbedPaneTest() {

        UIManager.put("TabbedPane.contentBorderInsets", new InsetsUIResource(1, 0, 0, 0));
        UIManager.put("TabbedPane.contentAreaColor", new ColorUIResource(Color.GREEN));
        UIManager.put("TabbedPane.focus", new ColorUIResource(Color.ORANGE));
        UIManager.put("TabbedPane.selected", new ColorUIResource(Color.YELLOW));
        UIManager.put("TabbedPane.darkShadow", new ColorUIResource(Color.DARK_GRAY));
        UIManager.put("TabbedPane.borderHightlightColor", new ColorUIResource(Color.LIGHT_GRAY));
        UIManager.put("TabbedPane.light", new ColorUIResource(Color.WHITE));
        UIManager.put("TabbedPane.tabAreaBackground", new ColorUIResource(Color.CYAN));
        UIManager.put("ToolTip.background", Color.WHITE);
        UIManager.put("ToolTip.border", new BorderUIResource(new LineBorder(Color.BLACK)));
        this.updateUI();

        this.setBackground(Color.BLUE);

        JPanel testPanel = new JPanel();
        testPanel.setLayout(new BorderLayout());
        testPanel.add(new JLabel("Hello World"), BorderLayout.NORTH);
        testPanel.add(new JTextArea("Looks nice out there :)"), BorderLayout.CENTER);

        JPanel testPanel2 = new JPanel();
        testPanel2.setLayout(new BorderLayout());
        testPanel2.add(new JLabel("Good Bye World"), BorderLayout.NORTH);
        testPanel2.add(new JTextArea("AAAAAnnnnnd... I'm gone"), BorderLayout.CENTER);

        this.addTab("Hello World", testPanel);
        this.addTab("BB World", testPanel2);
    }
}