Java 设置文本字段的禁用背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22927451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:05:21  来源:igfitidea点击:

Set Text Field's Disabled Background Color

javanetbeanscolors

提问by Daksh Shah

I have a textfield which I set seteditable(false)and setEnabled(false)but the problem is that in this case the background color of it changes to something and I cannot change it back. enter image description here

我有我设置一个文本框seteditable(false)setEnabled(false),但问题是,在这种情况下,它的背景颜色改变的东西,我不能改回来。 在此处输入图片说明

See, the background color of the app and the background color of the 2 disabled text fields are different Question: How to change the background color of a disabled and non-editable text field.

看,应用程序的背景颜色和 2 个禁用文本字段的背景颜色不同 问题:如何更改禁用和不可编辑文本字段的背景颜色。

t5is the right text field (in the photo). What I have tried: Putting t5.setBackground(Color....), t5.setBackground(UIManager.getColor("t5.background")), t5.setBackground( null );at the end of the constructor. I have even read Background color of JTextField doesn't become 'grayed out' when disabled after the background color had been changed beforeand JTextField background color on enable/disablebut could not figure out a way to do what I want. I am using Netbeans 8 (Nimbus Theme). If I set the LaF to Windows, then the colors are same but how to make the colors same in Nimbus itself?

t5是正确的文本字段(在照片中)。我尝试过的:将t5.setBackground(Color....), t5.setBackground(UIManager.getColor("t5.background")),t5.setBackground( null );放在构造函数的末尾。我甚至已经阅读JTextField的背景颜色不会成为“变灰”禁用时的背景颜色已经改变之前后JTextField的背景色上启用/禁用,但不能想出办法做我想做的。我正在使用 Netbeans 8(Nimbus 主题)。如果我将 LaF 设置为 Windows,那么颜色是相同的,但是如何使 Nimbus 本身的颜色相同?

采纳答案by MadProgrammer

The "inactive" color is provided (generally) by the look and feel. For example, under Windows the property TextField.inactiveBackgroundcan be used to effect the non-editable background color...

“非活动”颜色(通常)由外观提供。例如,在 Windows 下,该属性TextField.inactiveBackground可用于影响不可编辑的背景颜色...

TextField

文本域

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;

public class TestTextField {

    public static void main(String[] args) {
        new TestTextField();
    }

    public TestTextField() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                UIManager.put("TextField.inactiveBackground", new ColorUIResource(new Color(255, 0, 0)));

                JTextField field = new JTextField("Hello", 10);
                field.setEditable(false);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new FlowLayout());
                frame.add(field);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

Updated with Nimbus example

更新了 Nimbus 示例

Nimbus just likes to be difficult...

Nimbus 就是喜欢难...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Painter;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.nimbus.AbstractRegionPainter;

public class TestTextField {

    public static void main(String[] args) {
        new TestTextField();
    }

    public TestTextField() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
//                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }


                JTextField field = new JTextField("Hello", 10);
                field.setEditable(false);
                field.setEnabled(false);
                UIDefaults overrides = new UIDefaults();
                overrides.put("TextField.background", new ColorUIResource(Color.RED));
                overrides.put("TextField[Enabled].backgroundPainter", new Painter<JTextField>() {

                    @Override
                    public void paint(Graphics2D g, JTextField field, int width, int height) {
                        g.setColor(Color.RED);
                        g.fill(new Rectangle(0, 0, width, height));
                    }

                });
                overrides.put("TextField[Disabled].backgroundPainter", new Painter<JTextField>() {

                    @Override
                    public void paint(Graphics2D g, JTextField field, int width, int height) {
                        g.setColor(Color.GREEN);
                        Insets insets = field.getInsets();
                        g.fill(new Rectangle(
                                insets.left, 
                                insets.top, 
                                width - (insets.left + insets.right), 
                                height - (insets.top + insets.bottom)));
                    }

                });
                field.putClientProperty("Nimbus.Overrides", overrides);
//                field.putClientProperty("Nimbus.Overrides.InheritDefaults",false);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new FlowLayout());
                frame.add(field);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

I've only shown two values (default and disabled), you'll need to play around with the others.

我只显示了两个值(默认值和禁用值),你需要和其他人一起玩。

TextField.background = DerivedColor(color=255,255,255 parent=nimbusLightBackground offsets=0.0,0.0,0.0,0 pColor=255,255,255
TextField.contentMargins = javax.swing.plaf.InsetsUIResource[top=6,left=6,bottom=6,right=6]
TextField.disabled = DerivedColor(color=214,217,223 parent=control offsets=0.0,0.0,0.0,0 pColor=214,217,223
TextField.disabledText = DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145
TextField.focusInputMap = javax.swing.plaf.InputMapUIResource@6a4ba620
TextField.font = javax.swing.plaf.FontUIResource[family=SansSerif,name=sansserif,style=plain,size=12]
TextField.foreground = DerivedColor(color=0,0,0 parent=text offsets=0.0,0.0,0.0,0 pColor=0,0,0
TextFieldUI = javax.swing.plaf.synth.SynthLookAndFeel
TextField[Disabled].backgroundPainter = javax.swing.plaf.nimbus.TextFieldPainter@c87b565
TextField[Disabled].borderPainter = javax.swing.plaf.nimbus.TextFieldPainter@21960050
TextField[Disabled].textForeground = DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145
TextField[Enabled].backgroundPainter = javax.swing.plaf.nimbus.TextFieldPainter@7eee9569
TextField[Enabled].borderPainter = javax.swing.plaf.nimbus.TextFieldPainter@61936199
TextField[Focused].borderPainter = javax.swing.plaf.nimbus.TextFieldPainter@12ecb5db
TextField[Selected].backgroundPainter = javax.swing.plaf.nimbus.TextFieldPainter@72974691
TextField[Selected].textForeground = DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255

Interestingly, if you use field.putClientProperty("Nimbus.Overrides.InheritDefaults",false);, then you tend to end up with a very simple field (no borders, etc).

有趣的是,如果您使用field.putClientProperty("Nimbus.Overrides.InheritDefaults",false);,那么您往往会得到一个非常简单的字段(无边框等)。

This approach only effects a single component...

这种方法只影响单个组件......