2011-01-20 24 views
5

SWT Button類有一個setForeground(Color)方法,但它似乎沒有效果(該方法實際上是Button的超類)。 javadoc說這個方法是一個提示,可能會被平臺覆蓋。我的平臺是Windows。如何設置SWT按鈕的前景色?

  • 這是否意味着無法在Windows上設置按鈕前景色?
  • 它在其他平臺上工作嗎?
  • 是否有解決方法?

回答

5

在Windows上,setForeground對於Buttons沒有影響。

作爲解決方法,請在您的Button中添加PaintListener。在這個Listener的paintControl方法中,獲取生成的事件的GC,並使用它,使用所需的顏色重寫Button的文本。

事實上,你可以在你的Button上畫任何東西。

+1

感謝馬里奧。花了一點時間來弄清位置,但是你的建議完成了這項工作。 – 2011-01-20 14:13:10

0

在窗口上,setForeground不適用於組。

如果你能說服你的用戶使用經典主題,setForeground將奇蹟般地工作。

2

如果你需要按鈕樣式SWT.CHECK,你可以嘗試使用沒有文本的按鈕,並添加標籤元素。例如:

chkAutorun = new Button(fCompositeLogin, SWT.CHECK); 
Label lblAutorun = new Label(fCompositeLogin, SWT.NONE); 
lblAutorun.setForeground(white); 
lblAutorun.setText("Autorun"); 
0

這是落實前景色在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是記憶鍵組合我已經使用