如何使用 Nimbus 外观和感觉更改 JPanel 的背景颜色?
我想为应用程序中的所有 JPanel 使用不同的背景颜色。使用 Nimbus 外观和感觉时如何做到这一点?
我按照更改颜色主题来更改组件的颜色Nimbus 外观和感觉。
它只是有时有效,随机。如果我在更改颜色之前设置 PropertyChagneListener
,则只会通知一次。
这是一些测试代码:
public class RedPanels extends JFrame {
public RedPanels() {
JPanel panel = new JPanel();
add(panel);
setPreferredSize(new Dimension(100, 100));
pack();
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
UIManager.getDefaults().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getPropertyName().equals("Panel.background")) {
System.out.println("color changed");
}
});
UIManager.put("Panel.background", new Color(255,0,0));
break;
}
}
} catch (Exception e) {
// Nimbus is not available.
}
new RedPanels();
}
});
}
}
I want to use a different background color for all my JPanels in an application. How can I do that when using Nimbus Look and Feel?
I follow Changing the Color Theme to change the color of components in Nimbus Look and Feel.
It only works sometimes, randomly. If I set a PropertyChagneListener
before I change the color, it is only notified once.
Here is some test code:
public class RedPanels extends JFrame {
public RedPanels() {
JPanel panel = new JPanel();
add(panel);
setPreferredSize(new Dimension(100, 100));
pack();
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
UIManager.getDefaults().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (event.getPropertyName().equals("Panel.background")) {
System.out.println("color changed");
}
});
UIManager.put("Panel.background", new Color(255,0,0));
break;
}
}
} catch (Exception e) {
// Nimbus is not available.
}
new RedPanels();
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有三种方法
1) 覆盖
nimbusBase
来设置DerivedColor
2) 创建自己的
Painter
,只有一个例子 -> aephyr 代码源,3)简单而肮脏的设置技巧直接颜色
there are three ways
1) override
nimbusBase
for setDerivedColor
2) create own
Painter
, only one example is there -> aephyr codesource,3) simple and dirty hack to set the Color directly
看起来像jdk6中的一个bug,Panel.background属性之一没有被采用。以下是jdk7中的工作(注意顺序:首先设置颜色,然后是LAF)
我的猜测是它仍然存在某种错误,因为Nimbus应该在接收管理器设置中的任何更改时更新其属性,因此将顺序反转为第一个设置Nimbus,然后设置颜色)应该也可以工作,但甚至在jdk7中也不起作用
似乎是特定于Panel.background(并且很可能是一堆其他),“控制”在两个jdks中都可以,设置 LAF 之前和之后。
Looks like a bug in jdk6, Panel.background one of the properties not taken. Following works in jdk7 (note the sequence: first set the color, then the LAF)
My guess is that it's still somehow buggy, as Nimbus is supposed to update its properties on receiving any change in the managers setting, so reversing the sequence to first set Nimbus, then put the color) should work as well, but doesn't even in jdk7
Seems to be specific to Panel.background (and most probably a bunch of others), "control" is okay in both jdks, both before and after setting the LAF.