【问题标题】:Set Text Field's Disabled Background Color设置文本字段的禁用背景颜色
【发布时间】:2014-05-20 14:00:08
【问题描述】:

我有一个文本字段,我设置了seteditable(false) 和setEnabled(false),但问题是在这种情况下,它的背景颜色会改变,我无法将它改回来。

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

t5 是正确的文本字段(在照片中)。我尝试过:将t5.setBackground(Color....)、t5.setBackground(UIManager.getColor("t5.background"))、t5.setBackground( null ); 放在构造函数的末尾。我什至读过Background color of JTextField doesn't become 'grayed out' when disabled after the background color had been changed before 和JTextField background color on enable/disable,但无法找到一种方法来做我想做的事。我正在使用 Netbeans 8(Nimbus 主题)。如果我将 LaF 设置为 Windows,那么颜色是相同的,但是如何使 Nimbus 本身的颜色相同?

【问题讨论】:

  • 添加到@MadProgrammer 的答案,我相信你正在使用 Nimbus。你应该看看Nimbus Defaults
  • @peeskillet 但是我该如何设置呢? MadProgrammer 的方式对我不起作用,可能是因为我将它放在构造函数的末尾?

标签: java netbeans colors


【解决方案1】:

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

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

}

更新了 Nimbus 示例

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

我只显示了两个值(默认值和禁用),您需要尝试其他值。

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

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

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

【讨论】:

  • 在我的情况下这似乎不起作用,这是我的构造函数代码pastebin.com/hy8fP7iQ
  • 关键是绝对的 UIManager.put("t5.inactiveBackground", new ColorUIResource(new Color(255, 0, 0))); 不起作用,它必须是 TextField.inactiveBackground - 假设您使用的是 Windows 外观
  • 我正在使用 Nimbus 外观
  • 你必须让生活变得困难:P
猜你喜欢
  • 1970-01-01
  • 2013-04-02
  • 2017-04-23
  • 2013-07-29
  • 2021-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多