I am making a game and when I disable a button with 开发者_StackOverflow中文版setEnabled(false);
the buttons turns grey which clashes with the other colors in the game. Is their a way to change the color of the button when it is disabled?
You can add HTML coding to your button which gives a large range of flexibility, even to disabled buttons.
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.
Calling setContentAreaFilled(false) on a button will make a button look as if it is disabled, even if it is not. It doesn't, however, seem to work the other way around.
If you don't need your button to look enabled, then it is a nice fix.
Docs Oracle
You can also set a disable icon with JButton.setDisableIcon()
You'll want to modify the Look and Feel you are using. There are tons available for download and you can of course make your own.
The button's ButtonUI
controls the disabled text color. Luckily, you can change it:
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);
The best and easy solution is:
yourButton.setContentAreaFilled(false);
Easily reverted by just changing the boolean value to 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
精彩评论