如何更改 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);

这就是我现在的 connect 4 游戏中表示红色玩家移动的内容。

目前,它只为背景着色,如果我将我的代码更改为 button[currRow][currCol].setForeground(Color.RED) 那么整个事情似乎没有改变。我该如何解决这个问题?

这不容易实现。问题是可插拔的外观和感觉会绘制按钮内容,并且它会以它认为合适的任何方式进行绘制。例如,一些 L&F 可能会绘制不使用背景颜色的渐变。

对于像您这样的情况,我建议使用自定义图像 (JButton.setIcon()) 并且不使用内容区域 (JButton.setContentAreaFilled(false))。

或者,您可以创建一个自定义组件来绘制元素本身,覆盖 JComponent.paintComponent()