[英]how to set color of Jbutton
如何設置JButton的顏色?
我試過這個:
button.setBackground(Color.red);
但沒有成功。 這只會改變按鈕邊框的顏色。 然后我試圖覆蓋paintComponents
public void paintComponent(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(0, 0, getSize().width, getSize().height);
}
但是現在我沒有看到JButton上的文字
為按鈕着色的最佳方法是使用ImageIcons而不是文本。 你可以使用Gimp來設計它們。
確保背景透明 !
button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/myimage.png")));
這是一個禁用的button.setDisabledIcon(...
按鈕:
這是一個啟用的按鈕,沒有按下:
這是一個按下啟用按鈕:
按下后的背景顏色變化由Swing完成。 你只需要2張圖片。
背景顏色的影響取決於您的外觀和感覺。 請參閱http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/index.html
您應該選擇以您想要的方式使用背景顏色的外觀。 我不知道你現在在用什么。 如果你有默認的LAF設置,顏色和刪除邊框就足夠了:
button.setBackground(color);
button.setBorder(null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.