自定义的SWT控件

来源:互联网 发布:模拟买基金软件 编辑:程序博客网 时间:2024/04/25 03:09
效果图:
自定义的SWT控件
为了在SWT中实现像Windows桌面图标那样的效果,自定义了一个控件,该控件继承自org.eclipse.swt.widgets.Composite,由一个缩略图标(CLabel)和一个图标名称(Label)构成。
缩略图是由图片文件提供,并缩放到固定尺寸。
创建该控件需要向其构造函数传递一个Image和String,分别是缩略图的原图和即将在图标下方显示的名称。
控件的代码如下,很简单:
Java代码
隐藏行号 复制代码 Source
  1. import java.io.File;    
  2. import org.eclipse.jface.resource.ImageDescriptor;    
  3. import org.eclipse.swt.SWT;    
  4. import org.eclipse.swt.custom.CLabel;    
  5. import org.eclipse.swt.events.MouseListener;    
  6. import org.eclipse.swt.events.PaintEvent;    
  7. import org.eclipse.swt.events.PaintListener;    
  8. import org.eclipse.swt.graphics.Image;    
  9. import org.eclipse.swt.graphics.Point;    
  10. import org.eclipse.swt.graphics.Rectangle;    
  11. import org.eclipse.swt.layout.FillLayout;    
  12. import org.eclipse.swt.layout.GridData;    
  13. import org.eclipse.swt.layout.GridLayout;    
  14. import org.eclipse.swt.widgets.Composite;    
  15. import org.eclipse.swt.widgets.Display;    
  16. import org.eclipse.swt.widgets.Label;    
  17. import org.eclipse.swt.widgets.Shell;    
  18.    
  19. public class Thumbnails extends Composite {    
  20.     private CLabel cLabelImage;    
  21.     private Image image;    
  22.     private Label label;    
  23.     private String labelText;    
  24.    
  25.     private final int IMAGE_SIZE = 164;//缩放后的图片最长的边长为定值。    
  26.    
  27.        
  28.     public Thumbnails(Composite parent, int style, Image image, String labelText) {    
  29.         super(parent, style);    
  30.         this.image = image;    
  31.         this.labelText = labelText;    
  32.         initGUI();    
  33.     }    
  34.    
  35.     private void initGUI() {    
  36.         GridData data = new GridData ();    
  37.         data.widthHint = 170;    
  38.         data.heightHint = 180;    
  39.         this.setLayoutData(data);    
  40.         try {    
  41.             GridLayout thisLayout = new GridLayout();    
  42.             this.setLayout(thisLayout);    
  43.    
  44.             {    
  45.                 GridData cLabelImageGridData = new GridData();    
  46.                 cLabelImageGridData.horizontalAlignment = GridData.FILL;    
  47.                 cLabelImageGridData.verticalAlignment = GridData.FILL;    
  48.                 cLabelImageGridData.grabExcessVerticalSpace = true;    
  49.                 cLabelImageGridData.grabExcessHorizontalSpace = true;    
  50.                 cLabelImage = new CLabel(this, SWT.NONE);    
  51.                 cLabelImage.setLayoutData(cLabelImageGridData);    
  52.    
  53. //              cLabelImage.setImage(this.image);    
  54.                 cLabelImage.addPaintListener(new PaintListener() {    
  55.                     public void paintControl(PaintEvent evt) {    
  56.                         cLabelImagePaintControl(evt);    
  57.                     }    
  58.                 });    
  59.             }    
  60.             {    
  61.                 label = new Label(this, SWT.NONE);    
  62.                 GridData labelGridData = new GridData();    
  63.                 labelGridData.horizontalAlignment = GridData.CENTER;    
  64.                 label.setLayoutData(labelGridData);    
  65.                 label.setText(this.labelText);    
  66.             }    
  67.             this.layout();    
  68.         } catch (Exception e) {    
  69.             e.printStackTrace();    
  70.         }    
  71.     }    
  72.    
  73.        
  74.     private void cLabelImagePaintControl(PaintEvent evt) {    
  75.         Rectangle bounds = image.getBounds();    
  76.         int imageWidth = bounds.width;    
  77.         int imageHeight = bounds.height;    
  78.    
  79.         int thumbWidth = IMAGE_SIZE;// TODO: 缩放后的图片最大宽度    
  80.         int thumbHeight = IMAGE_SIZE;// TODO: 缩放后的图片最大高度    
  81.    
  82.         double ratio = (double)imageWidth/(double)imageHeight;    
  83.    
  84.         //若宽大于高    
  85.         if(ratio>(double)1){    
  86.             thumbHeight = (int)(thumbWidth/ratio);    
  87.         }else {    
  88.             thumbWidth = (int)(thumbHeight*ratio);    
  89.         }    
  90.    
  91.         // 如果图片小于所略图大小, 不作处理    
  92.         if (imageWidth < IMAGE_SIZE && imageHeight < IMAGE_SIZE) {    
  93.             thumbWidth = imageWidth;    
  94.             thumbHeight = imageHeight;    
  95.         }    
  96.    
  97.         evt.gc.drawImage(image, 0, 0, bounds.width, bounds.height, 0, 0,    
  98.                 thumbWidth, thumbHeight);    
  99.         //   evt.gc.drawImage(image, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight)    
  100.     }    
  101.    
  102.    
  103.     @Override   
  104.     public void addMouseListener(MouseListener listener) {    
  105.         super.addMouseListener(listener);    
  106.         //这样在点击图标或标题的时候,整个控件才能被选中。    
  107.         cLabelImage.addMouseListener(listener);    
  108.         label.addMouseListener(listener);    
  109.     }    
  110.    
  111.     @Override   
  112.     public void dispose() {    
  113.         if(null !=image && !image.isDisposed()){    
  114.             image.dispose();    
  115.         }    
  116.         if(null !=cLabelImage && !cLabelImage.isDisposed()){    
  117.             cLabelImage.dispose();    
  118.         }    
  119.         if(null !=label && !label.isDisposed()){    
  120.             label.dispose();    
  121.         }    
  122.    
  123.         super.dispose();    
  124.     }    
  125.        
  126.        
  127.     public static void main(String[] args) {    
  128.         showGUI();    
  129.     }    
  130.    
  131.        
  132.     public static void showGUI() {    
  133.         Display display = Display.getDefault();    
  134.         Shell shell = new Shell(display);    
  135.    
  136.         File imageFile = new File("D://sb.jpg");    
  137.         Image localImage = null;    
  138.         if (imageFile.exists()) {    
  139.             ImageDescriptor imageDescriptor = ImageDescriptor.createFromFile(    
  140.                     null, imageFile.getAbsolutePath());    
  141.             localImage = imageDescriptor.createImage();    
  142.         }    
  143.    
  144.         Thumbnails inst = new Thumbnails(shell, SWT.NULL, localImage, "hello");    
  145.    
  146.         Point size = inst.getSize();    
  147.         shell.setLayout(new GridLayout());    
  148.         shell.layout();    
  149.         if (size.x == 0 && size.y == 0) {    
  150.             inst.pack();    
  151.             shell.pack();    
  152.         } else {    
  153.             Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);    
  154.             shell.setSize(shellBounds.width, shellBounds.height);    
  155.         }    
  156.         shell.open();    
  157.         while (!shell.isDisposed()) {    
  158.             if (!display.readAndDispatch())    
  159.                 display.sleep();    
  160.         }    
  161.     }    
  162. }   
.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}
原创粉丝点击