Java 更改JTabbedPane中的活动选项卡颜色

Java 更改JTabbedPane中的活动选项卡颜色,java,swing,jtabbedpane,Java,Swing,Jtabbedpane,选择时如何更改选项卡的颜色?它的边界呢?在这种情况下,它的Arbitros选项卡是蓝色的,我如何更改它?我正在使用JTabbedPane内部JFrame我找到了这个,但它不起作用UIManager.put(“TabbedPane.selected”,Color.white)我做错了什么 public VentanaPrincipal_vista() { super("Ventana Principal"); this.setDefaultCloseOperation(JFra

选择时如何更改选项卡的颜色?它的边界呢?在这种情况下,它的
Arbitros
选项卡是蓝色的,我如何更改它?我正在使用
JTabbedPane
内部
JFrame
我找到了这个,但它不起作用
UIManager.put(“TabbedPane.selected”,Color.white)我做错了什么

public VentanaPrincipal_vista() {

    super("Ventana Principal");

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(1000, 500);
    this.setResizable(false);
    this.setUndecorated(true);
    this.setBackground(Color.BLUE);
    // add tabbedPane and tabs .

    tabs = new JTabbedPane();
    tabs.setBackground(new Color(83, 83, 83));
    tabs.setForeground(new Color(255, 255, 255));
    tabs.setBorder(null);
    UIManager.put("TabbedPane.selected", Color.white);
    this.add(tabs);

    menuBar = new BGMenuBar();
    menuBar.setColor(new Color(83, 83, 83));
    this.setJMenuBar(menuBar);

    menu = new JMenu("File");
    menu.setForeground(Color.white);
    menuBar.add(menu);

    close = new JMenuItem("Close");

    menu.add(close);
    close.addActionListener(this);
    close.setBackground(new Color(83, 83, 83));
    close.setForeground(new Color(255, 255, 255));

    op1 = new JMenuItem("option 1");
    op1.setBackground(new Color(83, 83, 83));
    op1.setForeground(new Color(255, 255, 255));

    menu.add(op1);

    this.setLocationRelativeTo(null);
    this.setVisible(true);

}// end of constructor

请检查java文档中的以下方法,以获得JTabbedPane

void setBackgroundAt(int,Color)

尝试以下方法:

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class TabHighlight extends JPanel
{
    private static final int MAX = 5;
    private JTabbedPane pane = new JTabbedPane();

    public TabHighlight()
    {
        for (int i = 0; i < MAX; i++)
        {
            Color color     = Color.black;   //Set default tab background to black
            pane.add("Tab " + String.valueOf(i), new TabContent(pane, i, color));

            pane.setBackgroundAt(i, color);
            pane.setForegroundAt(i, Color.white);
        }

        this.add(pane);
    }

    private static class TabContent extends JPanel
    {
        private TabContent(JTabbedPane panel, int i, Color color)
        {
            //Active and inactive tab coloring must be done
            //when rendering the CONTENT of the JTabbedPane object

            //Override these default settings to allow for active and inactive
            //tab background color changing

            panel.setUI(new BasicTabbedPaneUI()
            {
                @Override
                protected void installDefaults()
                {
                    super.installDefaults();

                    highlight       = Color.lightGray;
                    lightHighlight  = Color.white;

                    shadow          = Color.gray;
                    darkShadow      = Color.darkGray;

                    //Active tab background is white
                    //so set the focus color to white
                    //to hide the active tab focus
                    //marker seeing that we want the
                    //active tab backgound to be different
                    focus           = Color.black;
                }
            });

            //Set the active tab background to white
            UIManager.put("TabbedPane.selected", Color.gray);

            UIManager.put("TabbedPane.unselectedTabBackground", Color.black);

            //Remove borders
            UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));

            setOpaque(true);

            setBackground(color);
            setForeground(Color.white);

            add(new JLabel("Tab content " + String.valueOf(i)));
        }

        @Override
        public Dimension getPreferredSize()
        {
            return new Dimension(320, 240);
        }
    }

    public void display()
    {
        JFrame f = new JFrame("TabHighlight");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    //Use this function here to change the look and feel to Java's default
    //If using a mac with OS X Yosemite or another recent Mac OS X release
    public static void initLookAndFeel()
    {
        try
        {
            UIManager.setLookAndFeel(
              UIManager.getCrossPlatformLookAndFeelClassName()
              );
        } 
        catch(UnsupportedLookAndFeelException e)
        {

        }
        catch(ClassNotFoundException e)
        {

        }
        catch(InstantiationException e)
        {

        }
        catch(IllegalAccessException e)
        {

        }
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    initLookAndFeel();
                    new TabHighlight().display();
                }
            });
    }
}
在本部分代码中,请执行以下操作:

  • 使用下面的这段代码覆盖UI

    panel.setUI(新的BasicTabbedPaneUI()
    {
    //将步骤2中的代码放在此处
    }

  • 输入该部分的以下属性:

    highlight=Color.lightGray;
    lightHighlight=Color.white;
    阴影=颜色。灰色;
    暗阴影=Color.darkGray;
    焦点=颜色。黑色;

    就像在第一个代码片段中一样,您也可以更改这些属性 提示:每个选项卡的标签周围都有一个小点框,用于标记焦点。如果 要隐藏此标记,只需将其颜色设置为与活动选项卡的颜色匹配, 现在有焦点了

  • 使用
    UIManager
    更改以下属性:
    UIManager.put(“TabbedPane.selected”,Color.gray);
    UIManager.put(“选项卡窗格。未选择的选项卡背景”,颜色。黑色);

    这将允许您在程序运行期间更改活动和非活动选项卡背景 运行时

    您使用的是
    UIManager
    ,这是实现您尝试的目标的最佳方式
    在这里使用这段代码可以让你做出你想要的改变 对于,但在进行任何更改之前,必须执行步骤2 效果。
    UIManager.put(“TabbedPane.selectedForeground”,Color.xxx)
    UIManager.put(“TabbedPane.unselectedTabForeground”,Color.xxx)
    不要更改 前景色,前景色保持不变。
    UIManager.put(“TabbedPane.selected”,Color.xxx)
    将更改背景颜色 活动选项卡和
    UIManager.put(“TabbedPane.unselectedTabBackground”)
    将 更改非活动选项卡的背景色

  • 将这两段代码复制并粘贴到您的文件中,并根据需要进行更改

    我建议您复制顶部的整个源代码,并将其粘贴到编译器中,然后首先运行它。这样,您将能够看到此程序的功能,并且当您返回到正在使用的原始代码时,您将知道将这些代码片段放在何处

    希望这有帮助

    如果您对此有问题,请在您的回复中发布SSCCE,这样我就可以看到您的问题所在


    谢谢。

    对CodeyX1回答的评论: 对我来说,uimanager未选择状态与这些值一起工作(中间没有“Tab”字):


    对于我来说,下面的解决方案有效,我只是在创建JTabbedPane对象之前设置UImanager的TabbedPane.selected颜色属性

     UIManager.put("TabbedPane.selected", Color.red);
    
          tabbedPane = new JTabbedPane();
    
    参考这个链接,我相信它也会为你工作


    可能喜欢?正如我所说,我想知道如何更改正在选择的选项卡,而不是所有选项卡的背景一般或特定方式选择突出显示由UI代理处理。正如我所说,我想知道如何更改正在选择的选项卡,而不是所有选项卡的背景一般或特定方式
        private TabContent(JTabbedPane panel, int i, Color color)
        {
            //Active and inactive tab coloring must be done
            //when rendering the CONTENT of the JTabbedPane object
    
            //Override these default settings to allow for active and inactive
            //tab background color changing
    
            panel.setUI(new BasicTabbedPaneUI()
            {
                @Override
                protected void installDefaults()
                {
                    super.installDefaults();
    
                    highlight       = Color.lightGray;
                    lightHighlight  = Color.white;
    
                    shadow          = Color.gray;
                    darkShadow      = Color.darkGray;
    
                    //Active tab background is white
                    //so set the focus color to white
                    //to hide the active tab focus
                    //marker seeing that we want the
                    //active tab backgound to be different
                    focus           = Color.black;
                }
            });
    
            //Set the active tab background to white
            UIManager.put("TabbedPane.selected", Color.gray);
    
            //Set the inactive tab background to black
            UIManager.put("TabbedPane.unselectedTabBackground", Color.black);
    
            //Remove borders
            UIManager.put("TabbedPane.contentBorderInsets"
                          , new Insets(0, 0, 0, 0));
    
            setOpaque(true);
    
            setBackground(color);
            setForeground(Color.white);
    
            add(new JLabel("Tab content " + String.valueOf(i)));
        }
    
    UIManager.put("TabbedPane.unselectedForeground", Color.xxx)
    UIManager.put("TabbedPane.selectedBackground", Color.xxx)
    
     UIManager.put("TabbedPane.selected", Color.red);
    
          tabbedPane = new JTabbedPane();