SWT Button 类有一个 setForeground(Color)
方法,但它似乎没有效果(该方法实际上是在 Button 的超类中)。Javadoc 表示此方法是一个提示,可能会被平台覆盖。我的平台是 Windows。
- 这是否意味着在 Windows 上无法设置按钮前景颜色?
- 其他平台上可以吗?
- 是否有解决方法?
setForeground
没有效果。Button
上添加一个PaintListener
。在这个Listener的paintControl
方法中,获取生成的事件的GC
,并使用它重新编写你的Button
文本,使用你想要的颜色。Button
上绘制任何东西。chkAutorun = new Button(fCompositeLogin, SWT.CHECK);
Label lblAutorun = new Label(fCompositeLogin, SWT.NONE);
lblAutorun.setForeground(white);
lblAutorun.setText("Autorun");
以下是在SWT中实现按钮前景色的代码,同时允许显示助记键并通过按下Alt +“助记键”启用;
Button radioButton=new Button(parent,SWT.RADIO);
StringBuffer sb = new StringBuffer("I am a Coloured radio button");
String name=null;
String S = "I am a Coloured radio button";
String substr="C";
int i=S.indexOf(substr);
sb.insert(i,"&");
S=sb.toString();
name=sb.substring(i, i+2);
name=sb.toString();
String whiteSpace=" ";
final String TName=S;
for(int l=0;l<1000;l++)
whiteSpace=whiteSpace.concat(" ");
radioButton.setText(name+whiteSpace);
radioButton.addPaintListener(new PaintListener(){
String name=TName;
@Override
public void paintControl(PaintEvent e) {
// TODO Auto-generated method stub
e.gc.setForeground(hex2Col("ffffcc"));
int x=21;
int y=21;
e.gc.drawText(name, x,y,SWT.DRAW_MNEMONIC | SWT.TRANSPARENT);
}
});
注意:hex2Col是我自己编写的将十六进制颜色代码转换为颜色类型的方法
注意:这里ALT+C是我使用的助记键组合