Java JTextField的背景色不为';不要变成';灰显';在之前更改背景色后禁用时

Java JTextField的背景色不为';不要变成';灰显';在之前更改背景色后禁用时,java,swing,background,jtextfield,Java,Swing,Background,Jtextfield,通常,当您使用setEditable(false)或setEnabled(false)时,JTextField的背景/前景颜色变为“灰色”。但是,如果以前使用setBackground(color)(例如,使用white)设置了背景色,则调用setEnabled或setEditable将不再影响背景色。相反,它被先前设置的颜色覆盖 在WinForms(.NET)中,这可以通过将背景颜色“重置”为非覆盖默认值来解决,即color.Empty。这将导致文本框恢复标准行为。然而,我还没有为JTextF

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

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

如何还原JTextField以使用默认颜色

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

使用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();
            }
        });
    }
}

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

默认UI委托正在查找
UIResource
,以确定用于给定字段的正确着色(基于安装的外观)

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

JTextField\setBackground(UIManager.getColor(“TextField.background”))

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


查看更多详细信息。

或使用JTextField.setDisabled。。。。(JSpinner、JFormattedTextField、可编辑JComboBox)而不是任何woodoo@mKorbel我只看过JTextComponent,但据我所知,它只允许您设置禁用的文本颜色,此外,为什么要让事情变得简单;)当您按以下顺序调用它时,它会起作用:
textField.setBackground(UIManager.getColor(…);textField.setEditable(false)
,但由于某些原因,当您首先调用
setEditable(false)
时,背景仍为白色。不管怎样,都可以。当您调用setEditable时,UI委托会检查背景颜色,并根据结果是什么类型的对象做出决定。但是当设置背景色时,它只会重新打印组件,这就是您需要在
textField.setEnabled(false)
时首先设置背景色的原因。这会很好地使字段变灰(前景色),但有一个副作用,这对我的情况来说是不可接受的:它使条目不可聚焦。例如,这意味着您不能选择和复制该值。
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();
            }
        });
    }
}