【问题标题】:How to deal with derived color in Nimbus Look and Feel?如何处理 Nimbus Look and Feel 中的派生颜色?
【发布时间】:2013-01-10 09:56:11
【问题描述】:

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

我知道颜色可以从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()));

背景确实发生了变化,但与看起来更亮的禁用背景并不完全相同。

处理这种派生颜色的正确方法是什么?

【问题讨论】:

  • 查看关于外观和感觉的 Oracle 教程,覆盖 JTextArea 的正确键
  • 无法编辑背景没有键。

标签: java swing user-interface look-and-feel nimbus


【解决方案1】:

@Zhao Yi 写了 不可编辑的背景没有键

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);
    }
}

【讨论】:

  • 感谢代码,但这不是我想要的。我只想使不可编辑(未禁用)的文本区域具有与其禁用背景相同的背景(Nimbus 默认)。此外,Painter 不是公共 API,因此像这样自定义 Nimbus 并不是一个好习惯。
  • 如果你是 JTextArea.setEditable(false),那么什么都没有发生,颜色方案保持不变,使用(不适用于 Nimbus)仅用于 JTextComponent
  • +1,Nimbus 比其他 LAF 更复杂。看来我需要更新 UIManagerDefaults 以包含属性的新 Nimbus 语法。
【解决方案2】:

我找到了答案。用于禁用背景的颜色不是UIManager.getColor("TextArea.disabled"),而是硬编码在TextAreaPainter类中:

private Color color1 = decodeColor("nimbusBlueGrey", -0.015872955f, -0.07995863f, 0.15294117f, 0);

使用这种颜色解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    相关资源
    最近更新 更多