我正在编写一个简单的扫雷游戏,现在它可以正常运行,但我正在处理优化细节,如将每个数字设置为不同的颜色。
当我尝试在 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
时,它就可以正常工作。
setForegroundColor()
。 - Mike GSetForeground()
。请查看文档。 - Mike G