2012-07-12 83 views
1

我在想如果有一種方法可以將一個像this這樣的樣式應用到swt複選框上。
我查找了自定義組件,但沒有找到任何東西。
謝謝你們定製一個複選框

回答

3

您可以使用以下方法: Create a custom button with SWT

爲出發點。在PaintListener內,您可以按照您希望的方式繪製按鈕。

這裏是一個小例子我只是想:

import org.eclipse.swt.events.MouseAdapter; 
import org.eclipse.swt.events.MouseAdapter; 
import org.eclipse.swt.events.MouseEvent; 
import org.eclipse.swt.events.PaintEvent; 
import org.eclipse.swt.events.PaintListener; 
import org.eclipse.swt.widgets.Canvas; 
import org.eclipse.swt.widgets.Composite; 

public class ImageButton extends Canvas { 
    private boolean checked = false; 
    private final ImageButton button = this; 

    public ImageButton(Composite parent, int style) { 
     super(parent, style); 

     this.addPaintListener(new PaintListener() { 
      @Override 
      public void paintControl(PaintEvent e) { 
       if(checked) 
       { 
        e.gc.drawImage(Icons.ON, 0, 0); 
       } 
       else 
       { 
        e.gc.drawImage(Icons.OFF, 0, 0); 
       } 
       button.setSize(WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE); 
      } 
     }); 
     this.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseUp(MouseEvent e) { 
       checked = !checked; 
       redraw(); 
      } 
     }); 
    } 
} 

其中Icons.ONIcons.OFF是兩個圖像和WIDTH_OF_IMAGE和HEIGHT_OF_IMAGE是使用圖像的寬度和高度。

+0

謝謝你,我來試試:) – 2012-07-12 17:19:37

+0

工作相當well..I'll保持在尋找一種方式,使其「綁定」到JFace現在:d – 2012-07-12 18:00:59

+0

不客氣。 – Baz 2012-07-12 18:01:25

相關問題