简体   繁体   English

更改 Nimbus LaF 中 JButton 的默认键值

[英]Changing the defauls key values for JButton in Nimbus LaF

I have a two Buttons in my program我的程序中有两个按钮

JButton button1 = new JButton();
button1.setText("First Button");
JButton button2 = new JButton("Second Button");

I have tried to change the LaF of the button, I am able to change the button background color using the following code我尝试更改按钮的 LaF,我可以使用以下代码更改按钮背景颜色

UIManager.put(Button.background new color(134,201,236));

But when i tried to change the other key values like "Button.disabled" , "Button[Default+Focused+Pressed].backgroundPainter" the code did not work for me.但是当我尝试更改其他键值时,例如"Button.disabled""Button[Default+Focused+Pressed].backgroundPainter" ,代码对我不起作用。 Could someone help me how to change the default key values which uses Painter?有人可以帮助我如何更改使用 Painter 的默认键值吗?

Gotta love Nimbus. 要爱雨云。

Okay to start with, you're going to want to keep these values close... 首先,您将要保持这些值接近...

Button.background = DerivedColor(color=214,217,223 parent=control offsets=0.0,0.0,0.0,0 pColor=214,217,223
Button.contentMargins = javax.swing.plaf.InsetsUIResource[top=6,left=14,bottom=6,right=14]
Button.defaultButtonFollowsFocus = false
Button.disabled = DerivedColor(color=214,217,223 parent=control offsets=0.0,0.0,0.0,0 pColor=214,217,223
Button.disabledText = DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145
Button.focusInputMap = javax.swing.plaf.InputMapUIResource@70e4bd3a
Button.font = javax.swing.plaf.FontUIResource[family=SansSerif,name=sansserif,style=plain,size=12]
Button.foreground = DerivedColor(color=0,0,0 parent=text offsets=0.0,0.0,0.0,0 pColor=0,0,0
ButtonUI = javax.swing.plaf.synth.SynthLookAndFeel
Button[Default+Focused+MouseOver].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@3e5d2085
Button[Default+Focused+Pressed].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@78662669
Button[Default+Focused].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@2988e80b
Button[Default+MouseOver].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@7c508d6d
Button[Default+Pressed].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@2b5ec36a
Button[Default+Pressed].textForeground = DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255
Button[Default].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@62c2ed06
Button[Disabled].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@c6499e5
Button[Disabled].textForeground = DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145
Button[Enabled].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@742746e1
Button[Focused+MouseOver].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@293f9e9c
Button[Focused+Pressed].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@5ce0ec60
Button[Focused].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@7463fda8
Button[MouseOver].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@3a3dad8b
Button[Pressed].backgroundPainter = javax.swing.plaf.nimbus.ButtonPainter@6f231f2e

These are basically the default key/values used by Nimbus to paint a standard button... 这些基本上是Nimbus用来绘制标准按钮的默认键/值。

Basically, what you have to do is provide your own Painter , for example... 基本上,您要做的就是提供自己的Painter ,例如...

public class ButtonPainter implements Painter {

    private Color light, dark;
    private GradientPaint gradPaint;

    public ButtonPainter(Color light, Color dark) {
        this.light = light;
        this.dark = dark;
    }

    @Override
    public void paint(Graphics2D g, Object c, int w, int h) {
        System.out.println("...");
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gradPaint = new GradientPaint((w / 2.0f), 0, light, (w / 2.0f), (h / 2.0f), dark, true);
        g.setPaint(gradPaint);
        g.fillRect(2, 2, (w - 5), (h - 5));

        Color outline = new Color(0, 85, 0);
        g.setColor(outline);
        g.drawRect(2, 2, (w - 5), (h - 5));
        Color trans = new Color(outline.getRed(), outline.getGreen(), outline.getBlue(), 100);
        g.setColor(trans);
        g.drawRect(1, 1, (w - 3), (h - 3));
    }
}

And then replace the UIManager values 然后替换UIManager

UIManager.getLookAndFeelDefaults().put("Button[Enabled].backgroundPainter", new ButtonPainter(Color.YELLOW, Color.RED));
UIManager.getLookAndFeelDefaults().put("Button[Focused].backgroundPainter", new ButtonPainter(Color.YELLOW, Color.RED));

For example... 例如...

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Painter;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;

public class TestNimbus {

    public static void main(String[] args) {
        new TestNimbus();
    }

    public TestNimbus() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                System.out.println(UIManager.get("Button[Default+Focused].backgroundPainter"));

                UIManager.getLookAndFeelDefaults().put("Button[Enabled].backgroundPainter", new ButtonPainter(Color.YELLOW, Color.RED));
                UIManager.getLookAndFeelDefaults().put("Button[Focused].backgroundPainter", new ButtonPainter(Color.YELLOW, Color.RED));

                System.out.println(UIManager.get("Button[Default+Focused].backgroundPainter"));


                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(new JButton("First Button"));
                frame.add(new JButton("Second Button"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ButtonPainter implements Painter {

        private Color light, dark;
        private GradientPaint gradPaint;

        public ButtonPainter(Color light, Color dark) {
            this.light = light;
            this.dark = dark;
        }

        @Override
        public void paint(Graphics2D g, Object c, int w, int h) {
            System.out.println("...");
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            gradPaint = new GradientPaint((w / 2.0f), 0, light, (w / 2.0f), (h / 2.0f), dark, true);
            g.setPaint(gradPaint);
            g.fillRect(2, 2, (w - 5), (h - 5));

            Color outline = new Color(0, 85, 0);
            g.setColor(outline);
            g.drawRect(2, 2, (w - 5), (h - 5));
            Color trans = new Color(outline.getRed(), outline.getGreen(), outline.getBlue(), 100);
            g.setColor(trans);
            g.drawRect(1, 1, (w - 3), (h - 3));
        }
    }

}

This is a global change, so all buttons will be affected by this change. 这是全局更改,因此所有按钮都将受到此更改的影响。 I believe there's a way to do so only those controls you want to change can be affected, but you will need to do some more research into that yourself ;) 我相信有一种方法可以做到,只有您要更改的控件才会受到影响,但是您需要自己进行一些研究;)

Here's a hack that works:这是一个有效的技巧:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.plaf.synth.SynthButtonUI;
import javax.swing.plaf.synth.SynthGraphicsUtils;

public class FlatButton extends JButton {

    private static final SynthButtonUI UI = new SynthButtonUI();
    private static final SynthGraphicsUtils UTIL = new SynthGraphicsUtils();

    public FlatButton() {
    }

    public FlatButton(Icon icon) {
        super(icon);
    }

    public FlatButton(String text) {
        super(text);
    }

    public FlatButton(Action a) {
        super(a);
    }

    public FlatButton(String text, Icon icon) {
        super(text, icon);
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setPaint(this.getBackground());
        final Dimension d = this.getPreferredSize();
        g2d.fillRect(0,0, d.width, d.height);
        this.paintBorder(g);

        g2d.setColor(this.getForeground());
        g2d.setFont(this.getFont());
        UTIL.paintText(
          UI.getContext(this), g, this.getText(), this.getIcon(),
          this.getHorizontalAlignment(), this.getVerticalAlignment(),
          this.getHorizontalTextPosition(), this.getVerticalTextPosition(),
          this.getIconTextGap(), this.getDisplayedMnemonicIndex(), 0);

    }
}

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

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