2010-12-21 78 views
1

我想爲按鈕內的文字着色 我該怎麼做? 我提出的代碼:着色按鈕

for(int i=0; i<10 ;i++) 
    { 
     arr[i]=new Button(""+i); 
     arr[i]. 
     arr[i].setBackground(Color.LIGHT_GRAY); 
     p1.add(arr[i]); 
     this.arr[i].addActionListener(this); 
    } 

按鈕文本顏色爲黑色。 如果我想將按鈕中的文本從黑色變爲藍色,我需要寫什麼?

回答

4

arr [i] .setForeground(Color.BLUE);

1

結束HTML中每個按鈕的文本。例如,

JButton button = new new JButton("<html><font color=#ffffdd>Caption</font></html>"); 

可以找到更多的信息here

+0

設置前景是一個更簡單的選項。 – jzd 2010-12-21 12:33:22

+0

@jzd同意 - 更簡單,但使用HTML可以更靈活地設置文本樣式(按鈕標題,標籤等)。因此,這個建議。 – 01es 2010-12-22 12:45:45