2012-07-12 84 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

相关问题