每个组件实例覆盖Swing Nimbus L&F主色

我试图为JButton / JTabbedPane的特定实例覆盖“nimbusBase”颜色而没有运气。 只有组件的特定属性,例如“Button.background”,才有效。 任何想法?

UIDefaults dialogTheme = new UIDefaults(); // dialogTheme.put(“nimbusBase”, Color.orange); // dialogTheme.put("nimbusBlueGrey", Color.blue); dialogTheme.put("Button.background", Color.yellow); JButton dialogButton = new JButton("North"); dialogButton.putClientProperty("Nimbus.Overrides.InheritDefaults", true); dialogButton.putClientProperty("Nimbus.Overrides", dialogTheme); 

您可以将颜色放到所有组件“nimbusBase”的LookAndFeelDefaults

 UIDefaults defaults = UIManager.getLookAndFeelDefaults(); defaults.put("nimbusBase", new ColorUIResource(210, 0, 210)); 

或者在编写时使用特定的Component。 也许更新componentTreeUI。

 ... dialogTheme.put("Button.background", new ColorUIResource(0, 0, 210)); ... dialogButton.putClientProperty("Nimbus.Overrides", dialogTheme); SwingUtilities.updateComponentTreeUI(dialogButton); 

我永远不会覆盖特定组件的"nimbusBase"

我找到了一个不错的Nimbus Theme Creator,可以显示将Nimbus默认颜色更改为所有组件的效果: http : //aephyr.googlecode.com/svn/trunk

编辑

您可以“复制”Painter,修改它们并通过UIDefaults设置它们:

使用nimbusOrange JTabbedPane和普通GUI的示例GUI。 在此处输入图像描述

 public class Example { static public void main( String[] s ) throws Exception { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { UIManager.setLookAndFeel(info.getClassName()); break; } } EventQueue.invokeLater( new Runnable() { @Override public void run() { JFrame frame = new JFrame(); frame.getContentPane().setLayout( new BorderLayout() ); frame.setBounds( 50, 50, 600, 600 ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); JTabbedPane pane = new JTabbedPane(); pane.addTab( "Lorem", new JLabel("Lorem") ); pane.addTab( "Ipsum", new JLabel("Ipsum") ); pane.addTab( "Dolor", new JLabel("Dolor") ); pane.addTab( "Sit", new JLabel("Sit") ); pane.addTab( "Amet", new JLabel("Amet") ); UIDefaults dialogTheme = new UIDefaults(); dialogTheme.put("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter", new Painter(Painter.BACKGROUND_ENABLED)); dialogTheme.put("TabbedPane:TabbedPaneTab[Disabled].backgroundPainter", new Painter(Painter.BACKGROUND_DISABLED)); dialogTheme.put("TabbedPane:TabbedPaneTab[Enabled+MouseOver].backgroundPainter", new Painter(Painter.BACKGROUND_ENABLED_MOUSEOVER)); dialogTheme.put("TabbedPane:TabbedPaneTab[Enabled+Pressed].backgroundPainter", new Painter(Painter.BACKGROUND_ENABLED_PRESSED)); dialogTheme.put("TabbedPane:TabbedPaneTab[Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED)); dialogTheme.put("TabbedPane:TabbedPaneTab[Disabled+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_DISABLED)); dialogTheme.put("TabbedPane:TabbedPaneTab[Focused+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_FOCUSED)); dialogTheme.put("TabbedPane:TabbedPaneTab[MouseOver+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_MOUSEOVER)); dialogTheme.put("TabbedPane:TabbedPaneTab[Focused+MouseOver+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_MOUSEOVER_FOCUSED)); dialogTheme.put("TabbedPane:TabbedPaneTab[Pressed+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_PRESSED)); dialogTheme.put("TabbedPane:TabbedPaneTab[Focused+Pressed+Selected].backgroundPainter", new Painter(Painter.BACKGROUND_SELECTED_PRESSED_FOCUSED)); dialogTheme.put("TabbedPane:TabbedPaneTabArea[Disabled].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_DISABLED)); dialogTheme.put("TabbedPane:TabbedPaneTabArea[Enabled+MouseOver].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_ENABLED_MOUSEOVER)); dialogTheme.put("TabbedPane:TabbedPaneTabArea[Enabled+Pressed].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_ENABLED_PRESSED)); dialogTheme.put("TabbedPane:TabbedPaneTabArea[Enabled].backgroundPainter", new AreaPainter(AreaPainter.BACKGROUND_ENABLED)); pane.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE); pane.putClientProperty("Nimbus.Overrides", dialogTheme); JTabbedPane secondPane = new JTabbedPane(); secondPane.addTab( "Lorem", new JLabel("Lorem") ); secondPane.addTab( "Ipsum", new JLabel("Ipsum") ); secondPane.addTab( "Dolor", new JLabel("Dolor") ); secondPane.addTab( "Sit", new JLabel("Sit") ); secondPane.addTab( "Amet", new JLabel("Amet") ); frame.getContentPane().setLayout( new BorderLayout() ); frame.getContentPane().add( pane, BorderLayout.NORTH ); frame.getContentPane().add( secondPane, BorderLayout.CENTER ); frame.setVisible( true ); } }); } } 

画家类与nimbusOrange
注意:我在此Painter示例中删除了paint方法。 因为这段代码很长。 只需通过com.sun.java.swing.plaf.nimbus.TabbedPaneTabbedPaneTabPainter copy和past添加它们。

 /* * TabbedPaneTabbedPaneTabPainter.java %E% * * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; import com.sun.java.swing.plaf.nimbus.AbstractRegionPainter; /** */ public final class Painter extends AbstractRegionPainter { //package private integers representing the available states that //this painter will paint. These are used when creating a new instance //of TabbedPaneTabbedPaneTabPainter to determine which region/state is being painted //by that instance. static final int BACKGROUND_ENABLED = 1; static final int BACKGROUND_ENABLED_MOUSEOVER = 2; static final int BACKGROUND_ENABLED_PRESSED = 3; static final int BACKGROUND_DISABLED = 4; static final int BACKGROUND_SELECTED_DISABLED = 5; static final int BACKGROUND_SELECTED = 6; static final int BACKGROUND_SELECTED_MOUSEOVER = 7; static final int BACKGROUND_SELECTED_PRESSED = 8; static final int BACKGROUND_SELECTED_FOCUSED = 9; static final int BACKGROUND_SELECTED_MOUSEOVER_FOCUSED = 10; static final int BACKGROUND_SELECTED_PRESSED_FOCUSED = 11; private int state; //refers to one of the static final ints above private PaintContext ctx; //the following 4 variables are reused during the painting code of the layers private Path2D path = new Path2D.Float(); private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0); private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0); private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0); //All Colors used for painting are stored here. Ideally, only those colors being used //by a particular instance of TabbedPaneTabbedPaneTabPainter would be created. For the moment at least, //however, all are created for each instance. private Color color1 = decodeColor("nimbusOrange", 0.032459438f, -0.55535716f, -0.109803945f, 0); private Color color2 = decodeColor("nimbusOrange", 0.08801502f, 0.3642857f, -0.4784314f, 0); private Color color3 = decodeColor("nimbusOrange", 0.08801502f, -0.63174605f, 0.43921566f, 0); private Color color4 = decodeColor("nimbusOrange", 0.05468172f, -0.6145278f, 0.37647057f, 0); private Color color5 = decodeColor("nimbusOrange", 0.032459438f, -0.5953556f, 0.32549018f, 0); private Color color6 = decodeColor("nimbusOrange", 0.032459438f, -0.54616207f, -0.02352941f, 0); private Color color7 = decodeColor("nimbusOrange", 0.08801502f, -0.6317773f, 0.4470588f, 0); private Color color8 = decodeColor("nimbusOrange", 0.021348298f, -0.61547136f, 0.41960782f, 0); private Color color9 = decodeColor("nimbusOrange", 0.032459438f, -0.5985242f, 0.39999998f, 0); private Color color10 = decodeColor("nimbusOrange", 0.08801502f, 0.3642857f, -0.52156866f, 0); private Color color11 = decodeColor("nimbusOrange", 0.027408898f, -0.5847884f, 0.2980392f, 0); private Color color12 = decodeColor("nimbusOrange", 0.035931647f, -0.5553123f, 0.23137254f, 0); private Color color13 = decodeColor("nimbusOrange", 0.029681683f, -0.5281874f, 0.18039215f, 0); private Color color14 = decodeColor("nimbusOrange", 0.03801495f, -0.5456242f, 0.3215686f, 0); private Color color15 = decodeColor("nimbusOrange", 0.032459438f, -0.59181184f, 0.25490195f, 0); private Color color16 = decodeColor("nimbusOrange", 0.05468172f, -0.58308274f, 0.19607842f, 0); private Color color17 = decodeColor("nimbusOrange", 0.046348333f, -0.6006266f, 0.34509802f, 0); private Color color18 = decodeColor("nimbusOrange", 0.046348333f, -0.60015875f, 0.3333333f, 0); private Color color19 = decodeColor("nimbusOrange", 0.004681647f, -0.6197143f, 0.43137252f, 0); private Color color20 = decodeColor("nimbusOrange", 7.13408E-4f, -0.543609f, 0.34509802f, 0); private Color color21 = decodeColor("nimbusOrange", -0.0020751357f, -0.45610264f, 0.2588235f, 0); private Color color22 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.43866998f, 0.24705881f, 0); private Color color23 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.44879842f, 0.29019606f, 0); private Color color24 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.08776909f, -0.2627451f, 0); private Color color25 = decodeColor("nimbusOrange", 0.06332368f, 0.3642857f, -0.4431373f, 0); private Color color26 = decodeColor("nimbusOrange", 0.004681647f, -0.6198413f, 0.43921566f, 0); private Color color27 = decodeColor("nimbusOrange", -0.0022627711f, -0.5335866f, 0.372549f, 0); private Color color28 = decodeColor("nimbusOrange", -0.0017285943f, -0.4608264f, 0.32549018f, 0); private Color color29 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.4555341f, 0.3215686f, 0); private Color color30 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.46404046f, 0.36470586f, 0); private Color color31 = decodeColor("nimbusOrange", -0.57865167f, -0.6357143f, -0.54901963f, 0); private Color color32 = decodeColor("nimbusOrange", -4.2033195E-4f, -0.38050595f, 0.20392156f, 0); private Color color33 = decodeColor("nimbusOrange", 0.0013483167f, -0.16401619f, 0.0745098f, 0); private Color color34 = decodeColor("nimbusOrange", -0.0010001659f, -0.01599598f, 0.007843137f, 0); private Color color35 = decodeColor("nimbusOrange", 0.0f, 0.0f, 0.0f, 0); private Color color36 = decodeColor("nimbusOrange", 0.0018727183f, -0.038398862f, 0.035294116f, 0); private Color color37 = decodeColor("nimbusRed", 0.0f, 0.0f, 0.0f, 0); //Array of current component colors, updated in each paint call private Object[] componentColors; public Painter(int state) { super(); this.state = state; Insets insets = null; switch( state ) { case BACKGROUND_ENABLED: insets = new Insets(7, 7, 1, 7); break; case BACKGROUND_ENABLED_MOUSEOVER: insets = new Insets(7, 7, 1, 7); break; case BACKGROUND_ENABLED_PRESSED: insets = new Insets(7, 6, 1, 7); break; case BACKGROUND_DISABLED: insets = new Insets(6, 7, 1, 7); break; case BACKGROUND_SELECTED_DISABLED: insets = new Insets(7, 7, 0, 7); break; case BACKGROUND_SELECTED: insets = new Insets(7, 7, 0, 7); break; case BACKGROUND_SELECTED_MOUSEOVER: insets = new Insets(7, 9, 0, 9); break; case BACKGROUND_SELECTED_PRESSED: insets = new Insets(7, 9, 0, 9); break; case BACKGROUND_SELECTED_FOCUSED: insets = new Insets(7, 7, 3, 7); break; case BACKGROUND_SELECTED_MOUSEOVER_FOCUSED:insets = new Insets(7, 9, 3, 9); break; case BACKGROUND_SELECTED_PRESSED_FOCUSED: insets = new Insets(7, 9, 3, 9); break; } this.ctx = new AbstractRegionPainter.PaintContext(insets, new Dimension( 10 , 20 ), false ); } @Override protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) { //populate componentColors array with colors calculated in getExtendedCacheKeys call componentColors = extendedCacheKeys; //generate this entire method. Each state/bg/fg/border combo that has //been painted gets its own KEY and paint method. switch(state) { case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break; case BACKGROUND_ENABLED_MOUSEOVER: paintBackgroundEnabledAndMouseOver(g); break; case BACKGROUND_ENABLED_PRESSED: paintBackgroundEnabledAndPressed(g); break; case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break; case BACKGROUND_SELECTED_DISABLED: paintBackgroundSelectedAndDisabled(g); break; case BACKGROUND_SELECTED: paintBackgroundSelected(g); break; case BACKGROUND_SELECTED_MOUSEOVER: paintBackgroundSelectedAndMouseOver(g); break; case BACKGROUND_SELECTED_PRESSED: paintBackgroundSelectedAndPressed(g); break; case BACKGROUND_SELECTED_FOCUSED: paintBackgroundSelectedAndFocused(g); break; case BACKGROUND_SELECTED_MOUSEOVER_FOCUSED: paintBackgroundSelectedAndMouseOverAndFocused(g); break; case BACKGROUND_SELECTED_PRESSED_FOCUSED: paintBackgroundSelectedAndPressedAndFocused(g); break; } } @Override protected final PaintContext getPaintContext() { return ctx; } // ... paintBackgroundEnabled( ... // ... } 

AreaPainter with numbusOrange

 /* * TabbedPaneTabbedPaneTabAreaPainter.java %E% * * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Insets; import java.awt.Paint; import java.awt.PaintContext; import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; import javax.swing.JComponent; import com.sun.java.swing.plaf.nimbus.AbstractRegionPainter; /** */ public final class AreaPainter extends AbstractRegionPainter { //package private integers representing the available states that //this painter will paint. These are used when creating a new instance //of TabbedPaneTabbedPaneTabAreaPainter to determine which region/state is being painted //by that instance. static final int BACKGROUND_ENABLED = 1; static final int BACKGROUND_DISABLED = 2; static final int BACKGROUND_ENABLED_MOUSEOVER = 3; static final int BACKGROUND_ENABLED_PRESSED = 4; private int state; //refers to one of the static final ints above private PaintContext ctx; //the following 4 variables are reused during the painting code of the layers private Path2D path = new Path2D.Float(); private Rectangle2D rect = new Rectangle2D.Float(0, 0, 0, 0); private RoundRectangle2D roundRect = new RoundRectangle2D.Float(0, 0, 0, 0, 0, 0); private Ellipse2D ellipse = new Ellipse2D.Float(0, 0, 0, 0); //All Colors used for painting are stored here. Ideally, only those colors being used //by a particular instance of TabbedPaneTabbedPaneTabAreaPainter would be created. For the moment at least, //however, all are created for each instance. private Color color1 = new Color(255, 200, 0, 255); private Color color2 = decodeColor("nimbusOrange", 0.08801502f, 0.3642857f, -0.4784314f, 0); private Color color3 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.45471883f, 0.31764704f, 0); private Color color4 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.4633005f, 0.3607843f, 0); private Color color5 = decodeColor("nimbusOrange", 0.05468172f, -0.58308274f, 0.19607842f, 0); private Color color6 = decodeColor("nimbusOrange", -0.57865167f, -0.6357143f, -0.54901963f, 0); private Color color7 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.4690476f, 0.39215684f, 0); private Color color8 = decodeColor("nimbusOrange", 5.1498413E-4f, -0.47635174f, 0.4352941f, 0); private Color color9 = decodeColor("nimbusOrange", 0.0f, -0.05401492f, 0.05098039f, 0); private Color color10 = decodeColor("nimbusOrange", 0.0f, -0.09303135f, 0.09411764f, 0); //Array of current component colors, updated in each paint call private Object[] componentColors; public AreaPainter(int state) { super(); this.state = state; this.ctx = new AbstractRegionPainter.PaintContext(new Insets(0, 5, 6, 5), new Dimension(5, 24), false ); } @Override protected void doPaint(Graphics2D g, JComponent c, int width, int height, Object[] extendedCacheKeys) { //populate componentColors array with colors calculated in getExtendedCacheKeys call componentColors = extendedCacheKeys; //generate this entire method. Each state/bg/fg/border combo that has //been painted gets its own KEY and paint method. switch(state) { case BACKGROUND_ENABLED: paintBackgroundEnabled(g); break; case BACKGROUND_DISABLED: paintBackgroundDisabled(g); break; case BACKGROUND_ENABLED_MOUSEOVER: paintBackgroundEnabledAndMouseOver(g); break; case BACKGROUND_ENABLED_PRESSED: paintBackgroundEnabledAndPressed(g); break; } } @Override protected final PaintContext getPaintContext() { return ctx; } private void paintBackgroundEnabled(Graphics2D g) { rect = decodeRect1(); g.setPaint(color1); g.fill(rect); rect = decodeRect2(); g.setPaint(decodeGradient1(rect)); g.fill(rect); } private void paintBackgroundDisabled(Graphics2D g) { rect = decodeRect2(); g.setPaint(decodeGradient2(rect)); g.fill(rect); } private void paintBackgroundEnabledAndMouseOver(Graphics2D g) { rect = decodeRect2(); g.setPaint(decodeGradient3(rect)); g.fill(rect); } private void paintBackgroundEnabledAndPressed(Graphics2D g) { rect = decodeRect2(); g.setPaint(decodeGradient4(rect)); g.fill(rect); } private Rectangle2D decodeRect1() { rect.setRect(decodeX(0.0f), //x decodeY(1.0f), //y decodeX(0.0f) - decodeX(0.0f), //width decodeY(1.0f) - decodeY(1.0f)); //height return rect; } private Rectangle2D decodeRect2() { rect.setRect(decodeX(0.0f), //x decodeY(2.1666667f), //y decodeX(3.0f) - decodeX(0.0f), //width decodeY(3.0f) - decodeY(2.1666667f)); //height return rect; } private Paint decodeGradient1(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float)bounds.getX(); float y = (float)bounds.getY(); float w = (float)bounds.getWidth(); float h = (float)bounds.getHeight(); return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f }, new Color[] { color2, decodeColor(color2,color3,0.5f), color3, decodeColor(color3,color4,0.5f), color4, decodeColor(color4,color2,0.5f), color2}); } private Paint decodeGradient2(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float)bounds.getX(); float y = (float)bounds.getY(); float w = (float)bounds.getWidth(); float h = (float)bounds.getHeight(); return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f }, new Color[] { color5, decodeColor(color5,color3,0.5f), color3, decodeColor(color3,color4,0.5f), color4, decodeColor(color4,color5,0.5f), color5}); } private Paint decodeGradient3(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float)bounds.getX(); float y = (float)bounds.getY(); float w = (float)bounds.getWidth(); float h = (float)bounds.getHeight(); return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f }, new Color[] { color6, decodeColor(color6,color7,0.5f), color7, decodeColor(color7,color8,0.5f), color8, decodeColor(color8,color2,0.5f), color2}); } private Paint decodeGradient4(Shape s) { Rectangle2D bounds = s.getBounds2D(); float x = (float)bounds.getX(); float y = (float)bounds.getY(); float w = (float)bounds.getWidth(); float h = (float)bounds.getHeight(); return decodeGradient((0.5f * w) + x, (0.0f * h) + y, (0.5f * w) + x, (1.0f * h) + y, new float[] { 0.08387097f,0.09677419f,0.10967742f,0.43709677f,0.7645161f,0.7758064f,0.7870968f }, new Color[] { color2, decodeColor(color2,color9,0.5f), color9, decodeColor(color9,color10,0.5f), color10, decodeColor(color10,color2,0.5f), color2}); } }