首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Synth LaF JLabel禁用颜色

Synth LaF JLabel禁用颜色
EN

Stack Overflow用户
提问于 2010-01-07 00:42:58
回答 2查看 1.3K关注 0票数 1

使用Synth LaF,我无法将JLabel的前景颜色设置为禁用状态。有没有人成功地做到了这一点?下面是我的标签在LaF.xml文件中的样式定义。

代码语言:javascript
复制
    <style id="whiteLabelStyle">
        <opaque value="false"/>
        <font name="Bitstream Vera Sans" size="16" />
        <state>
            <color type="FOREGROUND" value="WHITE"/>
        </state>
        <state value="DISABLED">
            <color type="FOREGROUND" value="BLACK"/>
        </state>
    </style>
    <bind style="whiteLabelStyle" type="name" key="WhiteOrbitLabel"/>

请注意,我的LaF.xml文件中定义的所有其他样式都在我的应用程序中正确呈现,包括我的标签的白色正常状态颜色(当我执行lbl.setEnabled(false)时,它永远不会变成黑色)

此外,通过Synth代码,我在SynthStyle.getColor中找到了以下注释

代码语言:javascript
复制
        if ((context.getComponentState() & SynthConstants.DISABLED) != 0) {
        //This component is disabled, so return the disabled color.
        //In some cases this means ignoring the color specified by the
        //developer on the component. In other cases it means using a
        //specified disabledTextColor, such as on JTextComponents.
        //For example, JLabel doesn't specify a disabled color that the
        //developer can set, yet it should have a disabled color to the
        //text when the label is disabled. This code allows for that.
        if (c instanceof JTextComponent) {
            JTextComponent txt = (JTextComponent)c;
            Color disabledColor = txt.getDisabledTextColor();
            if (disabledColor == null || disabledColor instanceof UIResource) {
                return getColorForState(context, type);
            }
        } else if (c instanceof JLabel 
                && (type == ColorType.FOREGROUND || type == ColorType.TEXT_FOREGROUND)){
            return getColorForState(context, type);
        }

但是我想不出如何为JLabel设置禁用的颜色

谢谢你的帮忙!

EN

回答 2

Stack Overflow用户

发布于 2010-05-12 09:24:26

我知道这个问题很老了,但也许有人仍然需要答案:

要自定义Synth L&F中的文本颜色,您需要将颜色类型设置为"TEXT_FOREGROUND",如下所示:

代码语言:javascript
复制
<state value="DISABLED">
    <color type="TEXT_FOREGROUND" value="BLACK"/>
</state>
票数 0
EN

Stack Overflow用户

发布于 2010-05-13 10:01:55

我发现SynthStyle.getColor的源代码与您的不同(我的源代码来自Sun JDK1.5):

代码语言:javascript
复制
/**
 * Returns the color for the specified state. This gives precedence to
 * foreground and background of the <code>JComponent</code>. If the
 * <code>Color</code> from the <code>JComponent</code> is not appropriate,
 * or not used, this will invoke <code>getColorForState</code>. Subclasses
 * should generally not have to override this, instead override
 * {@link #getColorForState}.
 *
 * @param context SynthContext identifying requester
 * @param type Type of color being requested.
 * @return Color
 */
public Color getColor(SynthContext context, ColorType type) {
    JComponent c = context.getComponent();
    Region id = context.getRegion();
    int cs = context.getComponentState();
    // For the enabled state, prefer the widget's colors
    if (!id.isSubregion() && cs == SynthConstants.ENABLED) {
        if (type == ColorType.BACKGROUND) {
            return c.getBackground();
        }
        else if (type == ColorType.FOREGROUND) {
            return c.getForeground();
        }
        else if (type == ColorType.TEXT_FOREGROUND) {
            // If getForeground returns a non-UIResource it means the
            // developer has explicitly set the foreground, use it over
            // that of TEXT_FOREGROUND as that is typically the expected
            // behavior.
            Color color = c.getForeground();
            if (!(color instanceof UIResource)) {
                return color;
            }
        }
    }
    // Then use what we've locally defined
    Color color = getColorForState(context, type);
    if (color == null) {
        // No color, fallback to that of the widget.
        if (type == ColorType.BACKGROUND ||
                    type == ColorType.TEXT_BACKGROUND) {
            return c.getBackground();
        }
        else if (type == ColorType.FOREGROUND ||
                 type == ColorType.TEXT_FOREGROUND) {
            return c.getForeground();
        }
    }
    return color;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2014529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档