着色按钮

问题描述:

我想为按钮内的文字着色 我该怎么做? 我提出的代码:着色按钮

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

按钮文本颜色为黑色。 如果我想将按钮中的文本从黑色变为蓝色,我需要写什么?

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

结束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