我正在制作一个游戏,在使用 setEnabled(false);
禁用按钮时,按钮会变成灰色,这与游戏中的其他颜色不协调。有没有一种方法可以在禁用按钮时改变按钮的颜色?
您可以在按钮中添加HTML代码,这会给禁用的按钮带来更大的灵活性。
Example: button.setText("<html><font color = red>3</font></html>");
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);
NB: JButton默认使用ButtonUI类作为其用户界面类。
最好且简单的解决方案是:
yourButton.setContentAreaFilled(false);
只需将布尔值更改为 true,即可轻松恢复:
yourButton.setContentAreaFilled(true);
我不是很确定,但我认为您可以覆盖按钮的玻璃板的绘制方法,以将其叠加为您选择的颜色。
这里有一个关于容器面板的教程:
http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html