加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何在Nimbus外观中处理派生颜色?

发布时间:2020-12-14 19:23:25 所属栏目:Java 来源:网络整理
导读:我想要的是使不可编辑的文本区域的背景与其禁用的背景相同. 我知道UIManager可以使用TextArea.disabled键获得颜色: DerivedColor(color=214,217,223 parent=control offsets=0.0,0.0,0 pColor=214,223 我首先尝试过: textArea.setBackground(UIManager.get

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

我知道UIManager可以使用TextArea.disabled键获得颜色:

DerivedColor(color=214,217,223 parent=control offsets=0.0,0.0,0 pColor=214,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

>再次看到Oracle Tutorial about Look and Feel,覆盖proper Keys for JTextArea
>键值适用于Painter
> TextArea [Disabled NotInScrollPane] .backgroundPainter
> TextArea [Disabled] .backgroundPainter

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,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",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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读