如何设置 JButton 的按钮颜色(不是背景颜色)
我有一个 JButton
,我想将其背景颜色更改为白色。使用金属外观和感觉时,我使用 setBackground
实现了所需的效果:
不幸的是,使用Windows LAF时“背景颜色”的概念是不同的;背景颜色是按钮周围绘制的颜色:
我会喜欢使用 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
:
Unfortunately, the concept of "background color" is different when using the Windows LAF; the background color is the color drawn around the button:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须决定是否值得付出努力,但您始终可以创建自己的
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.我在 XP 上使用 JDK 6。看起来 Window UI 不遵循正常绘画规则的方式多于 1。正如您所注意到的,setBackground() 不起作用。您应该能够通过告诉组件不要填充内容区域来进行自定义绘制:
当您按原样运行代码时,它似乎可以正常工作。也就是说,当您单击按钮时,您会看到边框发生变化。然而,如果您使用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:
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.
但我仍然认为(由 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)
最好的选择是使用 SwingX:
JXButton 允许您使用
.setBackgroundPainter(Painter)
设置背景 Painter,使用 MattePainter 完全可以实现您想要的效果。让 JXButton 扩展自 JButton,代码中的更改很小:将变为
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:would become
您可以做任何您试图以不同方式显示的指示,而不是弄乱按钮的背景颜色吗?
显示图标、将按钮设置为粗体而不是纯文本等。
仅通过不同的背景颜色来指示某些内容并不总是显而易见的,并且根据用户的系统颜色,可能会显得不和谐或不可见。
例如,如果用户在“高对比度”模式下运行窗口怎么办?
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?