12

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

4

8 回答 8

8

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

Example: button.setText("<html><font color = red>3</font></html>");
于 2011-09-05T18:38:15.903 回答
4

If you are talking about the text, then you might be able to use the UIManager to change the disabled foreground color. Check out the UIManager Defaults.

于 2009-10-19T20:20:47.313 回答
4

在按钮上调用setContentAreaFilled(false)将使按钮看起来好像被禁用,即使它不是。然而,它似乎并没有反过来工作。

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

文档甲骨文

于 2012-06-07T19:05:14.800 回答
2

您还可以使用JButton.setDisableIcon()设置禁用图标

于 2009-10-19T20:07:10.933 回答
1

您需要修改正在使用的外观。有大量可供下载,您当然可以自己制作。

于 2009-10-19T20:01:32.427 回答
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);
于 2017-11-01T00:22:43.840 回答
0

最好和简单的解决方案是:

yourButton.setContentAreaFilled(false);

只需将布尔值更改为 true 即可轻松恢复:

yourButton.setContentAreaFilled(true);
于 2020-09-04T06:10:11.813 回答
-1

i am not 100% on this, but i think you can overwrite the paint method of the button's glasspane to overlay it with the color of your choice.

here is a tutorial on the panes of a container:

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

于 2009-10-19T20:18:09.347 回答