Java 如何更改JTextField的不可编辑前景

Java 如何更改JTextField的不可编辑前景,java,swing,jtextfield,look-and-feel,Java,Swing,Jtextfield,Look And Feel,我想更改应用程序中所有文本字段的不可编辑前景(以避免不可理解JTextComponent.setediate(false)),但找不到相应的属性键以执行此操作。是否可以全局更改不可编辑的前景?在“正常”外观下,您可以使用“TextField.inactiveBackground”键,对于文本,您可以使用“TextField.inactiveForeground” 比如说 UIManager.put("TextField.inactiveBackground", new ColorUIResour

我想更改应用程序中所有文本字段的不可编辑前景(以避免不可理解
JTextComponent.setediate(false)
),但找不到相应的属性键以执行此操作。是否可以全局更改不可编辑的前景?

在“正常”外观下,您可以使用
“TextField.inactiveBackground”
键,对于文本,您可以使用
“TextField.inactiveForeground”

比如说

UIManager.put("TextField.inactiveBackground", new ColorUIResource(Color.RED));
从外观和感觉上看,Nimbus可能需要一些“额外”的工作

在“普通”外观下,您可以使用
“TextField.inactiveBackground”
键,对于文本,您可以使用
“TextField.inactiveForeground”

比如说

UIManager.put("TextField.inactiveBackground", new ColorUIResource(Color.RED));
从外观和感觉上看,Nimbus可能需要一些“额外”的工作

找到的解决方案:

import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.synth.SynthTextFieldUI;

/**
 * <code>ExtSynthTextFieldUI</code>.
 *
 * @author SMedvynskyy
 */
public class ExtSynthTextFieldUI extends SynthTextFieldUI {

    /** Handler to change foreground when editable status changed. */
    private final PropertyChangeListener listener = new PropertyChangeListener() {

        /** Save the old color here. */
        private Color standardColor = UIManager.getColor(getPropertyPrefix() + ".foreground");

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if ("editable".equals(evt.getPropertyName())) {
                if (Boolean.TRUE.equals(evt.getNewValue())) {
                    getComponent().setForeground(standardColor);
                } else {
                    standardColor = getComponent().getForeground();
                    // set the "inactive foreground"
                    getComponent().setForeground(new ColorUIResource(0xA0A0A0));
                }
            }
        }
    };

    /**
     * Creates a new UI object for the given component.
     *
     * @param table component to create UI object for
     * @return the UI object
     */
    public static ComponentUI createUI(JComponent table) {
        return new ExtSynthTextFieldUI();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void installListeners() {
        super.installListeners();
        getComponent().addPropertyChangeListener("editable", listener);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void uninstallListeners() {
        getComponent().removePropertyChangeListener("editable", listener);
        super.uninstallListeners();
    }
}
安装L&F后,我只需重置默认UI:

UIManager.put("TextFieldUI", ExtSynthTextFieldUI.class.getName());
找到的解决方案:

import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.synth.SynthTextFieldUI;

/**
 * <code>ExtSynthTextFieldUI</code>.
 *
 * @author SMedvynskyy
 */
public class ExtSynthTextFieldUI extends SynthTextFieldUI {

    /** Handler to change foreground when editable status changed. */
    private final PropertyChangeListener listener = new PropertyChangeListener() {

        /** Save the old color here. */
        private Color standardColor = UIManager.getColor(getPropertyPrefix() + ".foreground");

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if ("editable".equals(evt.getPropertyName())) {
                if (Boolean.TRUE.equals(evt.getNewValue())) {
                    getComponent().setForeground(standardColor);
                } else {
                    standardColor = getComponent().getForeground();
                    // set the "inactive foreground"
                    getComponent().setForeground(new ColorUIResource(0xA0A0A0));
                }
            }
        }
    };

    /**
     * Creates a new UI object for the given component.
     *
     * @param table component to create UI object for
     * @return the UI object
     */
    public static ComponentUI createUI(JComponent table) {
        return new ExtSynthTextFieldUI();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void installListeners() {
        super.installListeners();
        getComponent().addPropertyChangeListener("editable", listener);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void uninstallListeners() {
        getComponent().removePropertyChangeListener("editable", listener);
        super.uninstallListeners();
    }
}
安装L&F后,我只需重置默认UI:

UIManager.put("TextFieldUI", ExtSynthTextFieldUI.class.getName());


哪个外观和感觉?主要目标是Synthetica,但知道它是否可能很有趣。@MadProgrammer我从Jyloo那里得到了答案:Synthetica将在下一个版本中支持它。这确实是textfield和JTextComponents的显著区别,因为普通(JTexField/Area)是@MadProgrammer的有效答案,因为rest是不同的,并且按typeDYM taht standard disableXxxColor键入不起作用,或者是/不是这个/那些从JTextComponents实现的嵌套/继承方法,我怀疑,这个L&F与实质(有自己的渲染器/画师)相比较?主要目标是合成,但是知道它是否可能是很有趣的。@MadProgrammer我从Jyloo那里得到了答案:Synthetica将在下一个版本中支持它。这确实是textfield和JTextComponents的显著区别,因为plain(JTexField/Area)是@MadProgrammer的有效答案,因为rest是不同的,按类型键入DYM taht标准disableXxxColor不起作用,或者是/不是这/那些嵌套/继承从JTextComponents实现的方法,我对此表示怀疑,这个L&F与Substance(有自己的渲染器/画师)相当,有两种状态可编辑和启用,我不确定哪一种操作意味着使用setDisabledTextColorI“不可编辑的前景”是指可编辑状态,但在使用“JTextComponent.setEditable(false)”后,使用所述的
disabledTextColor
属性切换前景也是一个好主意“@SergiyMedvynskyy是的,正如我所说,你可以使用
TextField.inactiveForeground
还有两种状态可编辑和启用,我不确定哪一种操作意味着设置DisabledTextColori采用了“不可编辑的前景”“意思是可编辑状态,但提到的
disabledTextColor
属性也是在使用'JTextComponent.setEditable(false)'@SergiymedvynskyYep'后切换前景的一个好主意,正如我所说,您可以使用
TextField.inactiveForeground
也没有必要的属性ChangeListener,非常无用,只有在你想包装的情况下,在Html内部用一些特殊的东西装饰Swing,这是在rumtime构建的,不稳定的,cached@mKorbelUI必须安装侦听器,以便在包装对象的属性更改时收到通知。否则,我需要用我的扩展类替换应用程序中的所有JTextField对象。没有必要的PropertyChangeListener,非常无用,只是在您想要包装的情况下,在Html内部用一些特殊的东西装饰Swing,这是在rumtime构建的,不稳定的, cached@mKorbelUI必须安装侦听器,以便在包装对象的属性更改时收到通知。否则,我需要用扩展类替换应用程序中的所有JTextField对象。