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

2023-11-21

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

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

t5是右侧的文本字段(在照片中)。我尝试过的:放置t5.setBackground(Color....), t5.setBackground(UIManager.getColor("t5.background")), t5.setBackground( null );在构造函数的末尾。我什至读过在之前更改背景颜色后禁用时,JTextField 的背景颜色不会变为“灰色” and 启用/禁用时的 JTextField 背景颜色但无法找到一种方法来做我想做的事。我正在使用 Netbeans 8(Nimbus 主题)。如果我将LaF设置为Windows,那么颜色是相同的,但是如何使Nimbus本身的颜色相同?


“非活动”颜色(通常)由外观和感觉提供。例如,在 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);
            }
        });
    }

}

更新了 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);,那么你往往会得到一个非常简单的字段(没有边框等)。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

设置文本字段的禁用背景颜色 的相关文章

随机推荐

  • 如何确保在实体框架中使用存储库模式时创建代理?

    我的 SurveyController 类中有这个方法 public ActionResult AddProperties int id int propertyids int page 1 var survey uow SurveyRep
  • 如何对未签出的分支进行非快进 git 合并?

    我在树枝上a 我想合并分支b进入分支c 合并不是快进 但也不需要手动解析 也就是说 这不是最简单的情况 但也不是最困难的情况 因此这是 Git 无需人工即可自行完成的合并 有没有办法让我进行此合并b to c无需检查任何分支机构 如何 UP
  • Google Analytics v4 - 数据未显示在仪表板上

    我一直致力于将 Google Analytics v4 集成到我的应用程序中 我已经遵循了这个 https developers google com analytics devguides collection android v4 但是
  • 大量使用ViewBag

    我在 MVC 应用程序中大量使用 ViewBag 这被认为是不好的做法吗 我不确定是否要花时间创建 ViewModel 但我认为这更适合 MVVM 而不是 MVC 还是继续大量使用 ViewBag 支持和反对这一点的论据是什么 示例控制器方
  • 使用 PHP 通过表单发布 URL 时触发错误 403

    我在 apache 共享主机上使用 PHP 通过表单发布 URL 时遇到 403 Forbidden 错误 从现有的问题 表单提交时出现 403 错误 这很可能是由服务器上安装的 mod security 引起的 由于我使用共享托管 我无法
  • 找不到模块的声明文件

    我正在尝试使用我创建的 nodejs 包 有人可以指出我做错了什么吗 包结构如下 node modules my commons dist src helpers d ts helpers js index d ts index js no
  • 安卓。 SQLite异常:没有这样的列_id

    我在尝试获取存储在数据库中的信息并将其显示在 ListView 中时遇到一些麻烦 这是我想显示行的 ListActivity public class Prueba extends ListActivity Called when the
  • 如何在不使用数据库的情况下轻松存储持久数据?

    我正在尝试使用 Android 应用程序类 MyApplication java 将数据存储在一些字符串和整数的 ArrayList 中 我希望这些数据能够像数据库一样永久存储 但不使用数据库 简化我的应用程序 目前数据已存储 当我退出应用
  • 列出 sbt 1.2.8 中资源目录中的文件

    我有一个 Scala 应用程序 它处理来自某个目录的二进制文件resources 我想得到这个目录java io File并列出所有内容 在最新的 sbt 中 我无法直接做到这一点 我已经针对我的问题创建了最小的存储库 https gith
  • Symfony2 FOSUserBundle 角色实体

    我目前正在尝试找出将教义持久角色实体实现为与 FOSUserBundle 兼容的 M2M 关系的最佳方法 以前 我仅使用默认实现的字符串 并使用学说数组映射来持久化它 现在我需要将角色作为单独的实体 因为我们想要构建一个管理后端 其他人可以
  • 比较 ruby​​ 中的对象

    考虑一下 class Aaa attr accessor a b end x Aaa new x a x b 1 2 y Aaa new y a y b 1 2 puts x y gt false 有没有办法检查相同类型的类中的所有公共属性
  • 从之前的位置恢复 webapp?

    我有一个网络应用程序 它存储用户的数据 以便他们在 iPhone 上的主屏幕应用程序模式下单击外部链接并离开应用程序以查看网页或类似内容 当他们返回网络应用程序时 如何在同一位置恢复 目前它不会到达相同的位置 它重定向到主页我希望它从以前的
  • C 和 C++ 执行时间的差异

    我最近发现了这个名为 codechef 的网站 您可以在其中提交问题的解决方案 我为一个问题提交了两个答案 一个用 C 语言 另一个用 C 语言 两个代码几乎相同 但是当我用C提交的代码在4 89s执行时 我用C 提交的代码超时了 超过8秒
  • 创建文件 获取文件时间 设置文件时间

    我在使用 GetFileTime 和 SetFileTime 时遇到问题 当涉及到目录时 具体来说我认为我的问题是 我是 WinAPI 的新手 我认为我没有得到 正确处理 有两种情况 首先 我只需要一个句柄来获取文件或目录 时间戳 创建 访
  • 类型错误:格式字符串参数不足 - 使用 %Y-%m 时 Python SQL 连接

    with engine connect as con rs con execute SELECT datediff STR TO DATE CONCAT year month day Y m d current date from TABL
  • .setVisible(true) 立即重绘

    在一个简短的方法中 我使用 setVisible false 隐藏 JFrame 然后我截取屏幕截图并使用 setVisible true 恢复 JFrame 再次可见后 窗口应该显示与之前不同的图片 假设截取的屏幕截图的一部分 问题是 在
  • 如何从命令行使用 MSBuild 构建所有内容?

    这是有效的吗 MSBuild t all configuration all 我想使用 Visual Studio 2008 中的 MSBuild 从命令行构建 sln 文件等中所有项目的所有配置 我不想在调用 MSBuild 时指定它们
  • 如何在 cypress 测试中公开/访问 Redux 等数据存储?

    The 赛普拉斯文档说你可以 公开数据存储 如 Redux 中 以便您可以直接从测试代码以编程方式更改应用程序的状态 我还观看了 Kent C Dodds 先生的测试课程 他提到可以使用 Cypress 中的现有数据来初始化 redux 存
  • C++ 中的并行赋值

    有没有办法在C 中进行并行赋值 目前 以下编译 带有警告 include
  • 设置文本字段的禁用背景颜色

    I have a textfield which I set seteditable false and setEnabled false but the problem is that in this case the backgroun