如何设置 JButton 的按钮颜色(不是背景颜色)

发布于 2024-11-14 09:53:34 字数 451 浏览 2 评论 0原文

我有一个 JButton,我想将其背景颜色更改为白色。使用金属外观和感觉时,我使用 setBackground 实现了所需的效果:

Metal Look-and-带有白色背景的feel-styled JButton

不幸的是,使用Windows LAF时“背景颜色”的概念是不同的;背景颜色是按钮周围绘制的颜色:

Windows Look-and-feel-styled JButton with a White Background

​​ 我会喜欢使用 Windows LAF,但允许将此 JButton 的按钮颜色更改为白色。我该怎么做?

I have a JButton that I would like to change the background color of to white. When using the Metal Look And Feel, I achieve the desired effect with setBackground:

Metal look-and-feel-styled JButton with a white background

Unfortunately, the concept of "background color" is different when using the Windows LAF; the background color is the color drawn around the button:

Windows look-and-feel-styled JButton with a white background

I would like to use the Windows LAF, but allow the button color of this JButton to be changed to white. How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

似狗非友 2024-11-21 09:53:34

您必须决定是否值得付出努力,但您始终可以创建自己的 ButtonUI,如 示例来自@mKorbel。

You'll have to decide if it's worth the effort, but you can always create youe own ButtonUI, as shown in this example due to @mKorbel.

暮色兮凉城 2024-11-21 09:53:34

我在 XP 上使用 JDK 6。看起来 Window UI 不遵循正常绘画规则的方式多于 1。正如您所注意到的,setBackground() 不起作用。您应该能够通过告诉组件不要填充内容区域来进行自定义绘制:

import java.awt.*;
import javax.swing.*;

public class ButtonBackground extends JFrame
{
    public ButtonBackground()
    {
        setLayout( new FlowLayout() );

        JButton normal = new JButton("Normal");
        add(normal);

        JButton test1 = new JButton("Test 1")
        {
            @Override
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                g.setColor( Color.GREEN );
                g.fillRect(0, 0, getSize().width, getSize().height);
            }
        };
        test1.setContentAreaFilled(false);
        add(test1);
    }

    public static void main(String[] args)
    {
        try
        {
//          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }
        catch (Exception e2) {}

        ButtonBackground frame = new ButtonBackground();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

当您按原样运行代码时,它似乎可以正常工作。也就是说,当您单击按钮时,您会看到边框发生变化。然而,如果您使用Windows XP LAF运行,则边框永远不会改变,您看不到按钮单击效果。

因此,我猜问题出在 WindowUI 上,您需要自定义 UI,这可能太复杂,所以我没有解决方案。

I use JDK 6 on XP. It looks like the Window UI doesn't follow the normal painting rules in more ways than 1. As you noticed setBackground() doesn't work. You should be able to do custom painting by telling the component not to fill in the content area:

import java.awt.*;
import javax.swing.*;

public class ButtonBackground extends JFrame
{
    public ButtonBackground()
    {
        setLayout( new FlowLayout() );

        JButton normal = new JButton("Normal");
        add(normal);

        JButton test1 = new JButton("Test 1")
        {
            @Override
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                g.setColor( Color.GREEN );
                g.fillRect(0, 0, getSize().width, getSize().height);
            }
        };
        test1.setContentAreaFilled(false);
        add(test1);
    }

    public static void main(String[] args)
    {
        try
        {
//          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }
        catch (Exception e2) {}

        ButtonBackground frame = new ButtonBackground();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

When you run the code as is it seems to work properly. That is when you click on the button you see the Border change. However is you run with the Windows XP LAF, the Border never changes to you don't see the button click effect.

Therefore, I guess the issue is with the WindowUI and you would need to customize the UI which is probably too complex to do so I don't have a solution.

§普罗旺斯的薰衣草 2024-11-21 09:53:34

但我仍然认为(由 Darryl 修改)是正确的 UIManager.get (“Button.gradient”),因为将是跨平台

编辑:正确的答案是 - Nimbus 或一些自定义 L&F,为什么要重新发明轮子(Rob)

but I still think that (modified but by Darryl) is correct UIManager.get("Button.gradient"), because would be crossplatform

EDIT: correct answer would be - Nimbus or some Custom L&F, why reinvent the wheel (by Rob)

三生殊途 2024-11-21 09:53:34

最好的选择是使用 SwingX:

JXButton 允许您使用 .setBackgroundPainter(Painter) 设置背景 Painter,使用 MattePainter 完全可以实现您想要的效果。让 JXButton 扩展自 JButton,代码中的更改很小:

Color bg = new Color(new Random().nextInt(16777215)); // Random color

JButton button = new JButton();
button.setBackground(bg);

将变为

JXButton button = new JXButton();
button.setBackgroundPainter(new MattePainter(bg));

Your best option is using SwingX:

JXButton allows you to set a Painter for the background with .setBackgroundPainter(Painter) using a MattePainter achieves exactly what you want. Having that JXButton extends from JButton, the changes are minimal in your code:

Color bg = new Color(new Random().nextInt(16777215)); // Random color

JButton button = new JButton();
button.setBackground(bg);

would become

JXButton button = new JXButton();
button.setBackgroundPainter(new MattePainter(bg));
蒲公英的约定 2024-11-21 09:53:34

您可以做任何您试图以不同方式显示的指示,而不是弄乱按钮的背景颜色吗?

显示图标、将按钮设置为粗体而不是纯文本等。

仅通过不同的背景颜色来指示某些内容并不总是显而易见的,并且根据用户的系统颜色,可能会显得不和谐或不可见。

例如,如果用户在“高对比度”模式下运行窗口怎么办?

instead of messing with the button's background color, could you do whatever indication you're trying to show a different way?

displaying an Icon, making the button bold instead of plain text, etc.

Indicating something only through a different background color isn't always obvious, and depending on the user's system colors, may be jarring, or invisible.

for example, what if the user is running windows in "high contrast" mode?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文