合成 LaF JLabel 禁用颜色

发布于 2024-08-16 23:53:02 字数 1874 浏览 8 评论 0原文

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

    <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 中找到了以下评论,

        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 设置禁用颜色,

感谢您的帮助!

Using the Synth LaF, I am unable to set a JLabel's FOREGROUND color for the DISABLED state. has anybody succeeded in doing this? Here is my label's style definition in my LaF.xml file.

    <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"/>

Please not that all the other styles defined in my LaF.xml file are rendered properly in my application including my label's WHITE normal state color (it just never goes to black when I do lbl.setEnabled(false)

Also, going through the Synth code, I have found the following comment in SynthStyle.getColor

        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);
        }

But I could not figure out how to set a disabled color for a JLabel

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉梦枕江山 2024-08-23 23:53:02

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

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

<state value="DISABLED">
    <color type="TEXT_FOREGROUND" value="BLACK"/>
</state>

I know this question is old, but maybe someone still need the answer:

To customize the text color in Synth L&F, you need to set the color type to "TEXT_FOREGROUND", like this:

<state value="DISABLED">
    <color type="TEXT_FOREGROUND" value="BLACK"/>
</state>
十六岁半 2024-08-23 23:53:02

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

/**
 * 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;
}

I found the source code for SynthStyle.getColor is different than yours (mine is from Sun JDK 1.5):

/**
 * 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文