java 设置 JTabbedPane 的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2445565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:11:24  来源:igfitidea点击:

Set the Background Color for JTabbedPane

javaswingjtabbedpanenimbus

提问by Ram

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

我正在使用 Nimbus 外观和感觉。我需要在 JTabbedPane 中更改选项卡的背景颜色和前景色,但颜色没有在 JTabbedPane 中设置。我尝试了 setForeground()、setForegroundAt()、setBackground() 和 setBackgroundAt() 方法,但它不起作用。这是我的代码


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

}

}

回答by Rob Heiser

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 中的某些属性,以更改 Nimbus 从中派生其他颜色的颜色。我对您的代码进行了一些研究,发现如果我在调用 UIManager.setLookAndFeel() 之后放置以下代码,它将近似于您在示例中尝试的红底黑字外观:

     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.

我会留给你去试验。欲了解更多信息,进行实验,对配置雨云的好文章在这里。请务必查看他的标题为“Nimbus UIDefaults 属性列表”的链接。除了将颜色按摩到与您想要的相似的颜色之外,您还必须开始做一些杂乱的事情,例如实现进行自定义绘画的 Painter 类。

回答by Jason Nichols

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.

我不想告诉你,但是你将很难改变前景色和背景色。与许多基本组件(JTextField、JLabel 等)不同,JTabbedPane 在设置绘制时会忽略前景色和背景色。相反,它允许 UI 委托(由当前的外观决定)选择颜色。

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

一些外观和感觉允许颜色主题(想到Substance),但 Nimbus 似乎将这些颜色值硬编码到油漆代表中。

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

您很可能需要基于 Nimbus 实现自己的 UI 委托。请参阅此处了解更多信息。 这将是一个很大的痛苦,所以问问自己你希望这些颜色改变多少。

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.

最后,如果有人告诉您简单地继承 JTabbedPane 并在您自己的“paintComponent”方法中设置颜色,请忽略它。然后,您将负责整个组件的绘制,并且将失去任何类似 Nimbus 的视觉属性。

回答by Halo

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

JTabbedPane 在您向其中添加组件时起作用,例如添加 5 个 JPanel,它们将被标记。

回答by Serge Maslyakov

Sorry for my english.

对不起我的英语不好。

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

我尝试解决这个问题 2 天.. 有“变通办法”解决。

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!

我的天啊!!这是工作!

回答by jan.cajhtaml

Actually not true

其实不是真的

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

and then you create your simple UI

然后创建简单的 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

它应该是什么样子的(不要介意 QuadSpline 选项卡和关闭栏,这是我的 UI 实现的另一个附加组件),我在这里的声誉没有,所以有一个指向图像的链接

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

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

回答by Tikhon Jelvis

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

如果要更改实际内容,选项卡式窗格有两种有用的方法:setForegroundAtsetBackgroundAt。您可以循环浏览所有选项卡并调用它们:

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

You can also use getComponentAtas well, similarly:

您也可以getComponentAt类似地使用:

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.

后一种方法更灵活——稍后您可以使用这样的代码对所有组件执行更复杂的操作。