簡體   English   中英

如何在Java中更改實際JButton的顏色? 不是背景,不是文字

[英]How do I change the color of an actual JButton in Java? Not the background and not the text

button[currRow][currCol].setBackground(Color.RED);
button[currRow][currCol].setContentAreaFilled(true);
button[currRow][currCol].setOpaque(true);

這就是我現在的四連網游戲,代表紅色球員的舉動。

https://i.stack.imgur.com/E12ZI.png

目前,它只為背景着色,如果我將代碼更改為button [currRow] [currCol] .setForeground(Color.RED),那么整個事情似乎就不會改變。 我該如何解決?

這是不容易實現的。 問題在於,可插入的外觀會繪制按鈕內容,並且會以其認為合適的方式進行繪制。 例如,某些L&F可能會繪制不使用背景色的漸變。

我建議您使用自定義圖像( JButton.setIcon() )而沒有內容區域( JButton.setContentAreaFilled(false) )。

另外,您可以創建一個自定義組件來繪制元素本身,從而覆蓋JComponent.paintComponent()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM