如何在Java(NetBeans)中将禁用按钮的文本颜色更改为黑色?
我正在使用NetBeans用Java开发一个GUI,我喜欢将禁用按钮的文本颜色更改为黑色 以下命令在组合框中运行良好:如何在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
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,但对我来说,这是一个奇怪的行为,因为它在组合框中运行良好。有可能的工作吗?