2013-03-13 30 views
6

Basit bir mayın tarama gemisi oyunu yazıyorum ve şimdi çalışıyor, ancak her bir sayıyı farklı bir renk yapmak gibi güzel detaylar üzerinde çalışıyorum.JButton'un metin rengi nasıl değiştirilir

Metin rengini JButton'da ayarlamaya çalıştığımda hatalarla karşılaşmaya devam ediyorum. Metni kolayca ve arka plan rengini değiştirebilirim, ancak metin rengini özel olarak değil. Tüm mucked elde tutar

parçasıdır: Söylemek değiştirmek zaman derlemeye çalıştığınızda zaman

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

Bu oluşur, ancak:

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

Nedense benim hatadır setBackgroundColor yerine setTextColor çalışır. JButton için tanımlanmamış

+0

Sana 'setForegroundColor()' istediğini düşünüyorum bunda da koyarak çalıştı, ama yine de bana aynı hatayı (Sadece setForegroundColor ile SetTextColor değiştirilmesi veya yanlış şey yapmak olduğunu ben?) Verir – MikeTheLiar

+0

@mikeTheLiar –

+0

üzgünüm, bu SetForeground() 'olması gerekiyordu [belgelere bakın] (http://www.java2s.com/Tutorial/0Java/0240__Swing/SetFontandforegroundcolorforaJLabel.htm) – MikeTheLiar

cevap

17

setTextColor tanımlanmamıştır. JButton metin rengini ayarlamak için setForeground'u kullanabilirsiniz.

button.setForeground(Color.RED); 
+0

bunu koda ekleyebilir misiniz? Bunu yaptığımda hala aynı hatayı alıyorum ama nasıl kullanıyorum: jb.setForegroundColor (Color.red); –

+0

Çok teşekkür ederim! –

İlgili konular