如何更改JButton的文本颜色 [英] How to change text color of a JButton

查看:152
本文介绍了如何更改JButton的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个简单的扫雷游戏,现在工作,但我正在处理漂亮的细节,使每个数字的颜色不同。

I am writing a simple minesweeper game and it works now, but I am working on the pretty details like making each number a different color.

当我尝试在 JButton 上设置文本颜色时出现错误。

I keep running into errors when I try to set the text color on the JButton. I can change the text easily enough and the background, but not the text color specifically.

不断得到的部分是:

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);

出于某种原因,我的错误是:

For some reason my error is:

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 它可以正常工作。

This occurs whenever I try to compile, but when I change it to say setBackgroundColor instead of setTextColor it works just fine.

推荐答案

setTextColor 未定义用于JButton。要设置 JButton 文本颜色,可以使用 setForeground

setTextColor is undefined for JButton. To set the JButton text color, you can use setForeground.

button.setForeground(Color.RED);

这篇关于如何更改JButton的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆