改变JButton的背景 [英] Changing the background of JButton

查看:79
本文介绍了改变JButton的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Swing JButton,我也在为我的项目使用以下代码:

I have a Swing JButton and I'm also using the following code for my project:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

现在尝试使用 btnNewButton.setBackground更改一个按钮的背景时( Color.RED); 它不变为红色,只有边框变红。

Now when trying to change the background for one button using btnNewButton.setBackground(Color.RED); it doesn't turn red, only the borders turn red.

如何在静止时将此背景变为红色使用 UIManager.getSystemLookAndFeelClassName()用于其余的组件/项目?

How can I turn this background to red while still using UIManager.getSystemLookAndFeelClassName() for the rest of the components/project?

推荐答案

查看错误 4880747:XP L& F:REGRESSION:setBackground在JButton上设置Windows XP中的边框颜色。评估部分说明:

Take a look at bug 4880747 : XP L&F: REGRESSION: setBackground on JButton sets border color in Windows XP. Evaluation section states:


更改按钮的外观总是会导致与当前L& F实施的
冲突。用于Swing的Windows L& F试图尽可能接近本机显示器
。在XP上,我们使用内置的
位图资源作为按钮。这些不能着色,只需
就像本机API一样。

Changing the appearance of a button can always cause conflicts with the current L&F implementation. The Windows L&F for Swing tries to be as close as possible to the native display. On XP, we use the built-in bitmap resources for the buttons. These can not be colorized, just like in the native API.

你应该在按钮上调用setContentAreaFilled(false)以避免
L& F绘制它的装饰品。这有副作用,
按钮的opaque属性设置为false,所以你需要跟随调用setOpaque(true)调用的

You should call setContentAreaFilled(false) on the button to avoid having the L&F paint its decorations. This has the side effect that the button's opaque property is set to false, so you need to follow that call with a call to setOpaque(true).

这不是错误,将被关闭。

This is not a bug and will be closed.

如上所述, setContentAreaFilled(false) setOpaque(true)可以解决问题,但按钮看起来会有所不同。

As stated, setContentAreaFilled(false) and setOpaque(true) will do the trick, but the button will look different.

如果值得这么麻烦,你可以创建自己的 ButtonUI 。这是@mKorbel的一个很棒的示例,您可能会发现它很有用。

If it worth the trouble you can create you own ButtonUI. Here is a great example by @mKorbel that you may find useful.

这篇关于改变JButton的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆