2013-03-13 23 views
6

Piszę prostą grę trałową i działa ona teraz, ale pracuję nad pięknymi detalami jak nadawanie każdej liczbie innego koloru.Jak zmienić kolor tekstu JButton'a

Podczas próby ustawienia koloru tekstu na JButton ciągle pojawiają się błędy. Mogę łatwo zmienić tekst i tło, ale nie kolor tekstu.

Część, która staje się coraz wszystko spasowane up jest:

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

Z jakiegoś powodu mojego błędu:

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

Dzieje się tak, gdy próbuję skompilować, ale kiedy go zmienić powiedzieć setBackgroundColor zamiast setTextColor działa dobrze.

+0

myślę chcesz 'setForegroundColor()' – MikeTheLiar

+0

@mikeTheLiar Próbowałem oddanie że, ale wciąż daje mi ten sam błąd (jestem po prostu zastępując SetTextColor z setForegroundColor czy to coś złego zrobić?) –

+0

Przepraszam, to powinno być 'SetForeground()' Sprawdź [dokumentacja] (http://www.java2s.com/Tutorial/Java/0240__Swing/SetFontandforegroundcolorforaJLabel.htm) – MikeTheLiar

Odpowiedz

17

setTextColor jest niezdefiniowany dla JButton. Aby ustawić kolor tekstu JButton, możesz użyć setForeground.

button.setForeground(Color.RED); 
+0

czy możesz umieścić to w kodzie? Kiedy to robię, wciąż mam ten sam błąd, ale oto jak go używam: jb.setForegroundColor (Color.red); –

+0

Dziękuję bardzo! –

Powiązane problemy