当前位置: 首页 > 面试题库 >

在Nimbus LaF中更改JButton的默认键值

牛智志
2023-03-14
问题内容

我的程序中有两个按钮

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

我尝试更改按钮的LaF,我可以使用以下代码更改按钮背景色

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

但是,当我尝试更改其他键值(如)时"Button.disabled""Button[Default+Focused+Pressed].backgroundPainter"代码对我不起作用。有人可以帮助我如何更改使用Painter的默认键值吗?


问题答案:

要爱雨云。

首先,您将要保持这些值接近…

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

这些基本上是Nimbus用来绘制标准按钮的默认键/值。

基本上,您要做的就是提供自己的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));
    }
}

然后替换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));

例如…

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

}

这是全局更改,因此所有按钮都将受到此更改的影响。我相信有一种方法可以做到,只有要更改的控件才会受到影响,但是您需要自己进行一些研究;)



 类似资料:
  • 问题内容: 如何更改默认外壳?该命令当前显示: 我想将其更改为Bash。 问题答案: 尝试linux命令。 详细命令为。它将提示您输入密码。您的默认登录Shell 现在是。 您必须注销并重新登录才能看到此更改。 手册页中引用了以下内容: chsh命令更改用户登录外壳。这确定了用户初始登录命令的名称。普通用户只能为自己的帐户更改登录外壳,超级用户可以为任何帐户更改登录外壳 此命令将永久更改默认登录外

  • 你好,我想更改默认activity。但当我这样做的时候,应用程序启动时不会出现任何错误 这样Mainactivy就可以毫无问题地启动了。当我按下按钮时,它也会转到设备activity。所以一切正常。现在我需要DevcieACtivty成为默认的一个。 我就这样改变舱单 但现在应用程序没有上线。没有错误。控制台中没有任何内容。只是什么都做不到,会有什么问题吗? 开始写入以下错误ActivityMan

  • 我不小心把我的本地主推到gitlab上的一个分支,现在它是默认的。有没有办法重命名这个分支或设置一个新的主分支为主?

  • VBox中有两个按钮。如果单击第一个按钮,然后按“tab”,焦点框架将移动到第二个按钮。所以我的问题是,如果我想使用“tab”来实现其他功能,我该如何更改、取消或阻止JavaFX中默认的“tab”功能呢?

  • 字体Monospaced映射到不同系统上的不同字体。在Windows上,它被映射到“Courier”,在Linux上,它被映射到“Lucida Typewriter”。 http://ui.netbeans.org/docs/ui/editor_fonts_colors/editor_fonts_and_colors.htm 我在windows上,想将单倍行距字体映射到“consolas”而不是“

  • 问题内容: 尝试更改列的数据类型并设置新的默认值时遇到以下错误: 错误1064(42000):您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以在第1行的’VARCHAR(255)NOT NULL SET DEFAULT’{}’‘附近使用正确的语法 问题答案: 同样的第二种可能性(感谢juergen_d):