Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java(NetBeans)中将禁用按钮的文本颜色更改为黑色?_Java_User Interface_Swing_Button_Colors - Fatal编程技术网

如何在Java(NetBeans)中将禁用按钮的文本颜色更改为黑色?

如何在Java(NetBeans)中将禁用按钮的文本颜色更改为黑色?,java,user-interface,swing,button,colors,Java,User Interface,Swing,Button,Colors,我正在使用NetBeans用Java开发一个GUI,我喜欢将禁用按钮的文本颜色更改为黑色 以下命令在组合框中运行良好: UIManager.getDefaults().put("ComboBox.disabledForeground", Color.BLACK); UIManager.getDefaults().put("Button.disabledForeground", Color.BLACK); UIManager.getDefaults().put("Button.disabled

我正在使用NetBeans用Java开发一个GUI,我喜欢将禁用按钮的文本颜色更改为黑色

以下命令在组合框中运行良好:

UIManager.getDefaults().put("ComboBox.disabledForeground", Color.BLACK);
UIManager.getDefaults().put("Button.disabledForeground", Color.BLACK);
UIManager.getDefaults().put("Button.disabledText", Color.BLACK);
通过按钮,以下命令无效:

UIManager.getDefaults().put("ComboBox.disabledForeground", Color.BLACK);
UIManager.getDefaults().put("Button.disabledForeground", Color.BLACK);
UIManager.getDefaults().put("Button.disabledText", Color.BLACK);

UIManager.getDefaults().put("ComboBox.disabledForeground", Color.BLACK);
UIManager.getDefaults().put("Button.disabledForeground", Color.BLACK);
UIManager.getDefaults().put("Button.disabledText", Color.BLACK);
我希望有人能帮助我

提前谢谢你。
斯特芬

为我工作

对于组合框来说效果很好

将显示可以为LAF更改的属性

有可能的工作吗


不容易。这是UI的一部分。因此,您需要创建并安装自己的按钮UI。有时这相对容易,但大多数时候你需要访问受保护的或私有的方法。

这个问题已经很老了。然而,我遇到了同样的问题,没有找到令人满意的答案

所以我最终调试了swing代码。颜色由
SynthStyle.class
中的
getColor
方法确定。此方法仅对
JTextComponent
JLabel
使用禁用状态的默认值。 看起来默认的“
按钮。disabledText
”和“
按钮[Disabled].textForeground
”没有使用,至少在Nimbus LaF中是这样

最后,我扩展了
JButton
并覆盖
getForegroundColor

public class PatchedButton extends JButton {

    public PatchedButton(){
        super();
    }

    public PatchedButton(Icon icon){
        super(icon);
    }

    public PatchedButton(String text){
        super(text);
    }

    public PatchedButton(String text, Icon icon){
        super(text, icon);
    }

    @Override
    public Color getForeground() {
        //workaround
        if (!isEnabled()) {
                return  (Color)UIManager.getLookAndFeelDefaults().get("Button.disabledText");
        }
        return super.getForeground();
    }
}

这很奇怪,它在我的程序中不起作用。但例如“UIManager.getDefaults().put”(“Button.disabledShadow”,Color.RED);”正在工作。但不是文字…试着用红色。可能在您的系统中,很难区分灰色和不同颜色。但是它不起作用。我还创建了一个简单的测试应用程序来避免副作用。这是否值得,取决于使用的外观和感觉。不是所有的L&F都支持这个OK,但对我来说,这是一个奇怪的行为,因为它在组合框中运行良好。有可能的工作吗?