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

5

如何在选中时更改选项卡的颜色和边框?在这种情况下,是蓝色的Arbitros选项卡,我该如何更改它?我正在JFrame中使用JTabbedPane。我找到了这个解决方案,但它没有起作用: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

enter image description here


也许像这个问题(https://dev59.com/Q17Va4cB1Zd3GeqPO_kC)一样? - trashgod
就像我说的那样,我想知道如何更改当前选定的选项卡,而不是一般或特定方式下所有选项卡的背景。 - user3363537
选择高亮显示由UI委托处理。 - trashgod
5个回答

8
对我来说,下面的解决方案有效,我只是在创建JTabbedPane对象之前设置了UImanager的TabbedPane.selected颜色属性。
 UIManager.put("TabbedPane.selected", Color.red);

      tabbedPane = new JTabbedPane();

0

我知道这是一个旧帖子,但我也遇到了这个问题,按钮被聚焦了, 所以这对我有用:

UIManager.put("TabbedPane.selected", Color.red); 
+
tabbedPane.setFocusable(false);
or 
UIManager.put("TabbedPane.focus", Color.red);

0
CodeyX1的回答评论: 对我来说,uimanager未选定状态使用这些值(中间没有“Tab”一词)有效:
UIManager.put("TabbedPane.unselectedForeground", Color.xxx)
UIManager.put("TabbedPane.selectedBackground", Color.xxx)

-1

试试这个:

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

让我们来看一下这段代码片段,在程序启动时添加了5个选项卡:

public TabHighlight()
{
    for (int i = 0; i < MAX; i++)
    {
       Color color     = Color.black;   //Set default tab background to black
       Color color2    = Color.white;   //Set default tab foreground to white

       pane.add("Tab " + String.valueOf(i), new TabContent(pane, i, color));

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

    this.add(pane);
}

你请求程序帮助的第一个区域是“for”块。我会尝试以一种你能理解的方式来解释。
前两行定义了每个非活动选项卡的默认背景和前景颜色。您可以将其更改为您喜欢的颜色。
第三行添加了一个具有以下属性的选项卡:
  1. 定义选项卡上显示的文本的字符串
  2. 选项卡号码,如果不想包含,则不必包含
  3. 创建我将在此之后介绍的第二个代码片段
第四行应用默认背景颜色。
最后一行应用默认前景颜色。注意:这些属性无法使用UIManager更改。我尝试了几种替代方法,但没有成功。该死,我希望有一种更简单的方法来实现这一点。
现在让我们仔细看一下定义活动和非活动选项卡外观的代码片段。
    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)));
    }

在代码的这一部分中,按照以下步骤进行:
  1. 通过使用以下代码块覆盖UI

    panel.setUI(new BasicTabbedPaneUI() { //place the code from step 2 here }

  2. 输入该部分的这些属性:

    highlight = Color.lightGray; lightHighlight = Color.white; shadow = Color.gray; darkShadow = Color.darkGray; focus = Color.black;

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

  3. 使用UIManager更改以下属性: UIManager.put("TabbedPane.selected", Color.gray); UIManager.put("TabbedPane.unselectedTabBackground", Color.black);

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

    您正在使用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,以便我可以看到您遇到问题的地方。

谢谢。


-2

就像我说的那样,我想知道如何更改当前选定的选项卡,而不是一般或特定方式下所有选项卡的背景。 - user3363537

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接