![](/img/trans.png)
[英]How do I change the color of an actual JButton in Java? Not the background and not the text
[英]How to change text color of a JButton
我正在编写一个简单的扫雷游戏,它现在可以正常工作,但我正在研究漂亮的细节,比如让每个数字变成不同的颜色。
当我尝试在JButton
上设置文本颜色时,我一直遇到错误。 我可以很容易地改变文本和背景,但不能特别改变文本颜色。
不断变得混乱的部分是:
total = Integer.toString(count);
jb.setText(total);
if(count == 1)
jb.setTextColor(Color.blue);
if(count == 2)
jb.setTextColor(Color.green);
if(count == 3)
jb.setTextColor(Color.red);
出于某种原因,我的错误是:
MS.java:109: error: cannot find symbol jb.setTextColor(Color.blue); ^ symbol: method setTextColor(Color) location: variable jb of type JButton MS.java:112: error: cannot find symbol jb.setTextColor(Color.green); ^ symbol: method setTextColor(Color) location: variable jb of type JButton MS.java:114: error: cannot find symbol jb.setTextColor(Color.red); ^ symbol: method setTextColor(Color) location: variable jb of type JButton 3 errors Process javac exited with code 1
每当我尝试编译时都会发生这种情况,但是当我将其更改为setBackgroundColor
而不是setTextColor
它可以正常工作。
对于JButton, setTextColor
是未定义的。 要设置JButton
文本颜色,可以使用setForeground
。
button.setForeground(Color.RED);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.