【发布时间】:2023-03-11 18:50:01
【问题描述】:
我正在为描述性标题而苦恼。
我正在尝试在我的自定义组件中使用来自 NimbusLookAndFeel 的 swing.plaf 颜色。 我从 UIManager 获得的颜色看起来不错,但是 setBackground 和 setForeground 方法不适用于我获得的颜色。
以下示例显示了非常奇怪的行为
package syscolorbug;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SysColorBug {
public static void main(String[] args) throws Exception {
javax.swing.UIManager.
setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
Color background = javax.swing.UIManager.getDefaults().
getColor("List[Selected].textBackground");
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new FlowLayout());
jf.setSize(400,300);
JLabel l = new JLabel("Text");
l.setOpaque(true);
jf.add(l);
System.err.println(background);
// This needs to happen for the color to be set.
//24: background = new Color(background.getRGB());
System.err.println(background);
l.setBackground(background);
jf.setVisible(true);
}
}
如果我注释掉 24:它会按预期工作。
后台调试输出显示
DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138
和
java.awt.Color[r=57,g=105,b=138]
进一步调查表明,该问题似乎并未影响其他 LookAndFeels。
例如在 Metal LAF 上制作
javax.swing.plaf.ColorUIResource[r=255,g=255,b=255]
对于'List.background' 键。
这是 Nimbus plaf 中的错误吗?
我非常想避免多余的background = new Color(background.getRGB()); 有一个简单的解决方法吗?
Java版本是openjdk java 1.8.0。
【问题讨论】: