如何在java中更改禁用的JButton的外观

时间:2009-10-19 19:56:35

标签: java colors jbutton

我正在制作游戏,当我禁用setEnabled(false);按钮时,按钮变为灰色,与游戏中的其他颜色发生冲突。它们是一种在禁用按钮时更改按钮颜色的方法吗?

8 个答案:

答案 0 :(得分:8)

您可以在按钮中添加HTML编码,这样可以提供很大的灵活性,甚至是禁用按钮。

Example: button.setText("<html><font color = red>3</font></html>");

答案 1 :(得分:4)

如果您正在谈论文本,那么您可以使用UIManager更改禁用的前景色。查看UIManager Defaults

答案 2 :(得分:4)

在按钮上调用 setContentAreaFilled(false)会使按钮看起来像是被禁用,即使它不是。但是,它似乎并没有相反的方式。

如果你不需要你的按钮看起来启用,那么这是一个很好的解决方案。

Docs Oracle

答案 3 :(得分:1)

您需要修改正在使用的Look and Feel。有很多可供下载的,你当然可以自己制作。

答案 4 :(得分:1)

您还可以使用JButton.setDisableIcon()

设置禁用图标

答案 5 :(得分:1)

按钮的ButtonUI控制禁用的文字颜色。幸运的是,你可以改变它:

button1.setUI(new MetalButtonUI() {
    protected Color getDisabledTextColor() {
        return Color.BLUE;
    }
});
button1.setEnabled(false);
button2.setUI(new MetalButtonUI() {
    protected Color getDisabledTextColor() {
        return Color.RED;
    }
});
button2.setEnabled(false);

答案 6 :(得分:0)

最佳和简便的解决方案是:

yourButton.setContentAreaFilled(false);

只需将布尔值更改为true即可轻松还原:

yourButton.setContentAreaFilled(true);

答案 7 :(得分:-1)

我不是百分之百,但我认为你可以覆盖按钮玻璃板的绘画方法,用你选择的颜色覆盖它。

这是一个关于容器窗格的教程:

http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html

相关问题