我有一个自定义ButtonUI类涂料的按钮。绘制文本之前,paint方法检查是否按钮被设置自定义颜色,以便用这个来代替UIDefaults的#GET(“Button.foreground”)。

if ((b.getForeground() != null)) {
    colText = b.getForeground();
}

有一看到的java.awt.Component类产生一个问题:

public Color getForeground() {
    Color foreground = this.foreground;
    if (foreground != null) {
        return foreground;
    }
    Container parent = this.parent;
    return (parent != null) ? parent.getForeground() : null;
}

所以,检查按钮的getForeground()针对空没有帮助,它更将返回按钮被放置在该组件的前景色。

问题是:我如何检查是否按钮已被明确设置自定义前景色

把一个PropertyChangedListener上的按钮可能是一个解决方案,但不知何故,我认为必须有一个更简单的方法。

有帮助吗?

解决方案

Component可以找到isBackgroundSet()方法。

其他提示

覆盖的方法和使其返回this.foreground;或添加新的方法,该方法返回该

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top