如何在 UIManager 中使用默认 Nimbus 颜色?
我有一个自定义 ListCellRenderer,并且想使用默认的 Nimbus 选择背景颜色。我可以使用以下方式查找颜色:
Color selectionBackground = UIManager.getColor("nimbusSelectionBackground");
如果我打印它,它的值与 Nimbus 默认颜色。但是当我在 JPanel 上使用它时,我得到了不同的灰色,如何使用 UIManager 中的颜色?
当我这样做时:
setBackground(Color.RED);
JPanels 背景显示为红色,但是当我这样做时:
setBackground(selectionBackground);
“selectionBackground”颜色不使用,而是灰色。
以下是示例和屏幕截图:
背景应为:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class PanelColor {
public static void main(String[] args) {
// switch to Nimbus Look And Feel
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
try {
UIManager.setLookAndFeel(info.getClassName());
} catch (Exception e) { e.printStackTrace(); }
break;
}
}
Color selectionBackground = UIManager.getColor("nimbusSelectionBackground");
JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(300,50));
panel.add(new JLabel(selectionBackground.toString()), BorderLayout.NORTH);
// is not showing the selectionBackground color
panel.setBackground(selectionBackground);
JFrame frame = new JFrame();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
I have a custom ListCellRenderer and would like to use the default Nimbus selection backround color. I can lookup the color with:
Color selectionBackground = UIManager.getColor("nimbusSelectionBackground");
and if I print it, it has the same values as on Nimbus default colors. But when I use it on a JPanel I get a different gray color, how can I use the color from UIManager?
When I do:
setBackground(Color.RED);
The JPanels backround is shown in red, but when I do:
setBackground(selectionBackground);
The "selectionBackground" color is not used, but a gray color.
Here is an example and screenshot:
The background should be:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class PanelColor {
public static void main(String[] args) {
// switch to Nimbus Look And Feel
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
try {
UIManager.setLookAndFeel(info.getClassName());
} catch (Exception e) { e.printStackTrace(); }
break;
}
}
Color selectionBackground = UIManager.getColor("nimbusSelectionBackground");
JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(300,50));
panel.add(new JLabel(selectionBackground.toString()), BorderLayout.NORTH);
// is not showing the selectionBackground color
panel.setBackground(selectionBackground);
JFrame frame = new JFrame();
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为 Nimbus “抵制”设置颜色。它错误地假设您没有覆盖默认值,因为 UIManager.getColor() 返回 ColorUIResource 的实例。
ColorUIResource 只是一个实现 UIResource 标记接口的 Color。根据 Javadoc,L&F“使用此接口来决定属性值是否已被覆盖”。 Nimbus 检查背景颜色,注意到您没有覆盖它,然后依靠一些您不期望的内部行为。
I don't think Nimbus "resists" setting the color. It wrongfully assumes that you haven't overriden the default because UIManager.getColor() returns an instance of ColorUIResource.
ColorUIResource is simply a Color that implements the UIResource marker interface. According to the Javadoc, L&Fs "use this interface to decide if a properties value has been overridden". Nimbus checks the background color, noticed that you haven't overridden it, and falls back on some internal behavior that you're not expecting.
Nimbus 显然拒绝将其颜色用于其他地方。我不久前也偶然发现了这个问题,当时我最好的解决方案是使用组件(您可以查询)创建一个新的
Color
并使用它。当然,即使 L&F 发生变化,您也会坚持使用该颜色。我知道这就是从 UIManager 返回的
DerivedColor
的全部意义。不过我还没有找到更好的解决方案。对于其他 L&F 和其他事物也是如此。例如,GTK L&F 会很乐意为您提供您想要的图标,但它们不会在您自己的控件中绘制。我想这一切的部分原因是 Swing 是(a)非常复杂,(b)没有 L&F 真正遵守合同,甚至 Nimbus 也没有,尽管它是最新、最酷的。
Nimbus apparently resists that its colors are used elsewhere. I stumbled across this a while ago too and back then my best solution was to create a new
Color
using the components (you can query) and use that. Of course, then you stick with that color even if the L&F is changed.I know that's the whole point of a
DerivedColor
you get back from the UIManager. I haven't found a better solution though.This goes similarly for other L&Fs and other things as well. E.g. the GTK L&F will happily give you icons you want to have but they won't draw in your own controls. I guess part of all this is that Swing is (a) horribly complex and (b) no L&F out there actually adheres to the contracts, not even Nimbus, despite being the newest and coolest one.