17

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.

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.

The part that keeps getting all mucked up is:

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

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

Radiodef
  • 37,180
  • 14
  • 90
  • 125
Kurt E
  • 367
  • 1
  • 2
  • 9
  • I think you want `setForegroundColor()` – Mike G Mar 13 '13 at 18:25
  • @mikeTheLiar I tried putting that in, but it still gives me the same error(I am just replacing setTextColor with setForegroundColor or is that the wrong thing to do?) – Kurt E Mar 13 '13 at 18:27
  • sorry, that's supposed to be `SetForeground()` Check out the [documentation](http://www.java2s.com/Tutorial/Java/0240__Swing/SetFontandforegroundcolorforaJLabel.htm) – Mike G Mar 13 '13 at 18:32

1 Answers1

45

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

button.setForeground(Color.RED);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • can you put that into code? When i do it i still get the same error but here is how I am using it: jb.setForegroundColor(Color.red); – Kurt E Mar 13 '13 at 18:29