java 在之前更改背景颜色后禁用 JTextField 的背景颜色不会变为“变灰”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15693497/
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-10-31 20:32:28  来源:igfitidea点击:

Background color of JTextField doesn't become 'grayed out' when disabled after the background color had been changed before

javaswingbackgroundjtextfield

提问by dialer

Normally when you use setEditable(false)or setEnabled(false), the background/foreground color of the JTextField becomes 'grayed out'. However, if a background color had previously been set using setBackground(color)(for example to white), then the call to setEnabledor setEditablewill not affect the background color anymore. Instead, it is overridden by the previously set color.

通常,当您使用setEditable(false)或 时setEnabled(false),JTextField 的背景/前景色会变成“变灰”。但是,如果先前已使用setBackground(color)(例如 to white)设置背景颜色,则对setEnabledor的调用setEditable将不再影响背景颜色。相反,它被先前设置的颜色覆盖。

In WinForms (.NET) this is solved by "resetting" the background color to a non-overriding default value, namely Color.Empty. That would cause a text box to regain the standard behavior. However, I haven't found a similar "default value" for JTextField. How do I revert the JTextField to use the default colors and automatically switch the color when it is being disabled or set to read only? The foreground color has a similar issue.

在 WinForms (.NET) 中,这是通过将背景颜色“重置”为非覆盖默认值来解决的,即Color.Empty. 这将导致文本框重新获得标准行为。但是,我还没有为 JTextField 找到类似的“默认值”。如何恢复 JTextField 以使用默认颜色并在它被禁用或设置为只读时自动切换颜色?前景色也有类似的问题。

回答by MadProgrammer

You need to reset the background color of the field to its default value.

您需要将字段的背景颜色重置为其默认值。

The default UI delegate is looking for a UIResourcein order to determine the correct shadings to use for the given field (based on the installed look and feel).

默认 UI 委托正在寻找 aUIResource以确定用于给定字段的正确阴影(基于安装的外观)。

You can reset the background color using:

您可以使用以下方法重置背景颜色:

JTextField#setBackground(UIManager.getColor("TextField.background"))

JTextField#setBackground(UIManager.getColor("TextField.background"))

Alternatively, you could construct a custom UIResourcefor your custom background.

或者,您可以UIResource为自定义背景构建自定义。

Take a look at ColorUIResourcefor more details.

看看ColorUIResource更多细节。

回答by camickr

How do I revert the JTextField to use the default colors

如何恢复 JTextField 以使用默认颜色

textField.setBackground( null );

automatically switch the color when it is being disabled or set to read only?

在禁用或设置为只读时自动切换颜色?

Use a PropertyChangeListener:

使用 PropertyChangeListener:

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;

public class SSCCE extends JPanel implements PropertyChangeListener
{
    public SSCCE()
    {
        JTextField textField = new JTextField("Some Text");
        // Uncomment and run again to see the difference
        //textField.addPropertyChangeListener( this );
        textField.setBackground(Color.RED);
        textField.setEditable(false);
        add(textField);
    }

    public void propertyChange(PropertyChangeEvent e)
    {
        System.out.println(e.getPropertyName());
        JTextField textField = (JTextField)e.getSource();

        if ("editable".equals(e.getPropertyName()))
            textField.setBackground( null );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SSCCE() );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}