How to change text color of a JButton

橙三吉。 提交于 2019-12-17 19:42:18

问题


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.


回答1:


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

button.setForeground(Color.RED);


来源:https://stackoverflow.com/questions/15393385/how-to-change-text-color-of-a-jbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!