【问题标题】:How to set the button color of a JButton (not background color)如何设置 JButton 的按钮颜色(不是背景颜色)
【发布时间】:2011-06-06 18:37:31
【问题描述】:

我有一个JButton,我想将其背景颜色更改为白色。在使用 Metal Look And Feel 时,我使用 setBackground 达到了预期的效果:

很遗憾,在使用 Windows LAF 时,“背景颜色”的概念有所不同;背景颜色是按钮周围绘制的颜色:

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

【问题讨论】:

  • 尝试覆盖JButtonpaintComponent(Graphics g) 方法,并在那里进行自定义绘画。
  • "我想使用 Windows LAF" 我想知道您的 OSX 和 Linux 用户希望看到什么?我敢打赌这不是 Windows LAF(即使那是实用的)。 ;)
  • @Andrew Thompson:别担心。我正在使用系统 LAF,但目前只要求 Windows。 :)
  • 我的第一个问题是“为什么?”那么这个按钮要求它是不同的颜色并且看起来与其他按钮不同呢?

标签: java swing jbutton


【解决方案1】:

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

【讨论】:

  • 我认为我必须这样做。谢谢你的链接。
【解决方案2】:

我在 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)
            {
                g.setColor( Color.GREEN );
                g.fillRect(0, 0, getSize().width, getSize().height);
                super.paintComponent(g);
            }
        };
        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 运行,Border 永远不会改变到你看不到按钮点击效果。

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

【讨论】:

  • 作为参考,在 Mac OS X 上,BackgroundButton 在选择 Test 1 时看起来像 this
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    最好的选择是使用 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));
    

    【讨论】:

      【解决方案5】:

      除了弄乱按钮的背景颜色,你能做任何你试图以不同方式显示的指示吗?

      显示一个图标,使按钮变为粗体而不是纯文本等。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-07
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 2011-01-06
        • 2021-10-29
        相关资源
        最近更新 更多