我正在制作游戏,当我禁用按钮时 setEnabled(false);
,按钮变为灰色,与游戏中的其他颜色发生冲突。他们是一种在禁用按钮时更改按钮颜色的方法吗?
8 回答
您可以将 HTML 编码添加到您的按钮,这提供了很大的灵活性,甚至是禁用的按钮。
Example: button.setText("<html><font color = red>3</font></html>");
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.
在按钮上调用setContentAreaFilled(false)将使按钮看起来好像被禁用,即使它不是。然而,它似乎并没有反过来工作。
如果您不需要您的按钮看起来已启用,那么这是一个很好的解决方案。
您需要修改正在使用的外观。有大量可供下载,您当然可以自己制作。
该按钮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);
最好和简单的解决方案是:
yourButton.setContentAreaFilled(false);
只需将布尔值更改为 true 即可轻松恢复:
yourButton.setContentAreaFilled(true);
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