我正在编写一个简单的扫雷游戏,它现在可以运行了,但我正在处理一些漂亮的细节,比如让每个数字都具有不同的颜色。
当我尝试在 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);
出于某种原因,我的错误是:
<em>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</em>
每当我尝试编译时都会发生这种情况,但是当我将其更改为说 setBackgroundColor
而不是 setTextColor
时,它工作得很好。
最佳答案
setTextColor
未为 JButton 定义。要设置JButton
文本颜色,您可以使用setForeground
。
button.setForeground(Color.RED);
关于java - 如何更改 JButton 的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15393385/