[英]How to change the background color for JPanels with Nimbus Look and Feel?
[英]How to deal with derived color in Nimbus Look and Feel?
我想要的是使不可编辑的文本区域的背景与其禁用的背景相同。
我知道UIManager
使用TextArea.disabled
键获得颜色:
DerivedColor(color=214,217,223 parent=control offsets=0.0,0.0,0.0,0 pColor=214,217,223
我首先尝试过:
textArea.setBackground(UIManager.getColor("TextArea.disabled"));
它什么都没改变,背景仍然是白色的。
然后我尝试了:
textArea.setBackground(new Color(UIManager.getColor("TextArea.disabled").getRGB()));
背景确实发生了变化,但与看似更亮的残疾背景并不完全相同。
处理这种衍生颜色的正确方法是什么?
@Zhao Yi写道,无可辩驳的背景没有关键
再次参阅关于外观的Oracle教程 ,覆盖JTextArea的正确密钥
Keys value
适用于Painter
TextArea[Disabled+NotInScrollPane].backgroundPainter
TextArea[Disabled].backgroundPainter
Java6
代码,必须更改imports for Java7
import com.sun.java.swing.Painter;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class TestNimbusUIManagerTextArea {
private static JFrame frame = new JFrame("Nimbus UIDeafaults");
private JTextArea testEnableTextArea = new JTextArea("enabled JTextArea");
private JTextArea testDisableTextArea = new JTextArea("disabled JTextArea");
public TestNimbusUIManagerTextArea() {
testDisableTextArea.setEnabled(false);
frame.setLayout(new GridLayout(2, 0, 20, 20));
frame.add(testEnableTextArea);
frame.add(testDisableTextArea);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(200, 105);
frame.pack();
frame.setVisible(true);
}
private static void customizeNimbusLaF() {
UIManager.getLookAndFeelDefaults().put(
"TextArea[Enabled+NotInScrollPane].backgroundPainter",
new FillPainter(new Color(127, 255, 191)));
UIManager.getLookAndFeelDefaults().put(
"TextArea[Disabled+NotInScrollPane].backgroundPainter",
new FillPainter(new Color(127, 255, 191)));
SwingUtilities.updateComponentTreeUI(frame);
}
public static void main(String arg[]) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
customizeNimbusLaF();
break;
}
}
} catch (Exception e) {
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TestNimbusUIManagerTextArea tNUIM = new TestNimbusUIManagerTextArea();
}
});
}
}
class FillPainter implements Painter<JComponent> {
private final Color color;
FillPainter(Color c) {
color = c;
}
@Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
g.setColor(color);
g.fillRect(0, 0, width - 1, height - 1);
}
}
我找到了答案。 用于禁用后台的颜色不是UIManager.getColor("TextArea.disabled")
,而是在TextAreaPainter
类中进行了硬编码:
private Color color1 = decodeColor("nimbusBlueGrey", -0.015872955f, -0.07995863f, 0.15294117f, 0);
使用这种颜色解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.