如何设置Jbutton的颜色 [英] how to set color of Jbutton

查看:178
本文介绍了如何设置Jbutton的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置JButton的颜色?

How can I set the color of a JButton ?

我已经尝试过了:

button.setBackground(Color.red);

,但没有成功.这只是改变按钮边框的颜色. 然后,我尝试覆盖paintComponents

but with no success. This just changes the color of button's border. I then tried to override paintComponents

public void paintComponent(Graphics g) {
   g.setColor(Color.GREEN);
   g.fillRect(0, 0, getSize().width, getSize().height);
}

但是现在我看不到JButton上的文本

but now I don't see the text on the JButton

推荐答案

为按钮着色的最佳方法是使用ImageIcons而不是文本. 您可以使用 Gimp 进行设计.

The best way to color your buttons is to use ImageIcons instead of text. You could use Gimp in order to design them.

确保背景是透明的

button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/myimage.png")));

这是一个禁用的button.setDisabledIcon(...按钮:

这是一个启用的按钮,未按下:

This is an enabled button, not pressed:

这是一个按下的启用按钮:

This is a pressed enabled button:

按下后,背景色由Swing完成.您只需要2张图片即可.

The background color-change after pressing is done by Swing. You need just 2 images for this.

这篇关于如何设置Jbutton的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆