簡體   English   中英

如何設置SWT按鈕前景色?

[英]How to set SWT button foreground color?

SWT Button類有一個setForeground(Color)方法,但似乎沒有效果(該方法實際上是在Button的超類上)。 javadoc說這個方法是一個提示,可能會被平台覆蓋。 我的平台是Windows。

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

在Windows上, Buttons setForeground沒有任何效果。

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

事實上,您可以在Button繪制任何內容。

如果您需要具有樣式SWT.CHECK的Button,您可以嘗試使用不帶文本的Button並添加Label元素。 例:

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

這是在SWT中按鈕實現FOreground Color的代碼,同時允許顯示助記鍵並按Alt +“Mnemonic Key”啟用;

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是我使用的助記鍵組合

在Windows上,setForeground也不適用於Group。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM