简体   繁体   English

设置JTabbedPane的背景颜色

[英]Set the Background Color for JTabbedPane

I am using Nimbus Look and feel. 我正在使用Nimbus外观和感觉。 I needs to change the Background color and foreground color of the tab in JTabbedPane but the color doesn't set in JTabbedPane. 我需要在JTabbedPane中更改选项卡的背景颜色和前景色,但是在JTabbedPane中没有设置颜色。 I tried setForeground(), setForegroundAt(), setBackground() and setBackgroundAt() methods but it isnt works.This is my code 我尝试了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(); }

}

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. 最简单的方法是更改​​UIManager中的一些属性,以更改Nimbus从其他颜色中获取的颜色。 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.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 . 有关实验的更多信息,请参阅此处有关于配置Nimbus的文章。 Be sure you look at his link titled "Nimbus UIDefaults Properties List". 请务必查看标题为“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. 除了将颜色按摩到类似于你想要的东西之外,你将不得不开始做一些混乱的事情,比如实现自定义绘画的Painter类。

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. 与许多基本组件(JTextField,JLabel等)不同,JTabbedPane在设置绘制时忽略前景色和背景色。 Instead it allows the UI delegate (as determined by the current Look and Feel) to choose the colors. 相反,它允许UI委托(由当前外观和感觉确定)选择颜色。

Some Look and Feels allow for color themes ( Substance comes to mind), but Nimbus seems to have those color values hard coded into the paint delegates. 一些外观和感觉允许颜色主题( 物质浮现在脑海中),但是Nimbus似乎将这些颜色值硬编码到油漆代表中。

More than likely you'll need to implement your own UI delegate based off of Nimbus's. 您很可能需要基于Nimbus实现自己的UI委托。 See here for more information. 有关更多信息,请参见此处 It's going to be a major pain, so ask yourself how much you want those colors changed. 这将是一个重大的痛苦,所以问问自己你想要多少颜色改变。

Lastly, if anyone tells you to simply subclass JTabbedPane and set the colors in your own "paintComponent" method, ignore it. 最后,如果有人告诉您只是将JTabbedPane子类化并在您自己的“paintComponent”方法中设置颜色,请忽略它。 You then becomes responsible for the painting of the entire component, and will lose any Nimbus-like visual attributes. 然后,您将负责整个组件的绘制,并将丢失任何类似Nimbus的视觉属性。

JTabbedPane在向其添加组件时起作用,例如添加5个JPanel并且它们将被选项卡。

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! 是工作!

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 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

If you want to change the actual content, there are two useful methods of the tabbed pane: setForegroundAt and setBackgroundAt . 如果要更改实际内容,选项卡式窗格有两种有用的方法: setForegroundAtsetBackgroundAt You can just loop through all the tabs and call these: 您可以遍历所有选项卡并调用它们:

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

You can also use getComponentAt as 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. 后一种方法更灵活 - 您可以稍后使用这样的代码对所有组件执行更复杂的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM