gpt4 book ai didi

java - 如何处理 Nimbus Look and Feel 中的衍生颜色?

转载 作者:搜寻专家 更新时间:2023-10-31 20:13:36 24 4
gpt4 key购买 nike

我想要的是使不可编辑的文本区域的背景与其禁用的背景相同。

我知道颜色可以从 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 wrote There is no key for uneditable background

enter image description here

Java6 的代码,必须更改 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);
}
}

关于java - 如何处理 Nimbus Look and Feel 中的衍生颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14255050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com