`
soleegn
  • 浏览: 143420 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论
阅读更多
上次贴了几张图片出来显摆,这次彻底公布代码~大家看看原理就好,有兴趣的朋友可以和我联系,把SWT里面的控件都封装一下,做一套验证框架出来~
  1package com.glnpu.dmp.controls;
  2
  3import org.eclipse.swt.SWT;
  4import org.eclipse.swt.events.ControlEvent;
  5import org.eclipse.swt.events.ControlListener;
  6import org.eclipse.swt.events.ModifyEvent;
  7import org.eclipse.swt.events.ModifyListener;
  8import org.eclipse.swt.events.PaintEvent;
  9import org.eclipse.swt.events.PaintListener;
 10import org.eclipse.swt.graphics.GC;
 11import org.eclipse.swt.graphics.Image;
 12import org.eclipse.swt.widgets.Composite;
 13import org.eclipse.swt.widgets.Display;
 14import org.eclipse.swt.widgets.Text;
 15
 16public class SuperText extends Composite implements PaintListener, ControlListener{
 17
 18    private Text text;
 19    
 20    private Image tempCorner;
 21    
 22    private Image tempMsg;
 23    
 24    private Image warning_corner;
 25    
 26    private Image ok_corner;
 27    
 28    private Image error_corner;
 29    
 30    private Image msg_warning;
 31    
 32    private Image msg_ok;
 33    
 34    private Image msg_error;
 35    
 36    /** *//**
 37     * Create the composite
 38     * @param parent
 39     * @param style
 40     */

 41    public SuperText(Composite parent, int style) {
 42        super(parent, SWT.NONE);
 43        initResource();
 44        initControls(style);
 45        this.addPaintListener(this);
 46        this.addControlListener(this);
 47    }

 48
 49    private void initResource() {
 50        warning_corner = new Image(Display.getDefault(), "icons/input_warning_corner.gif");
 51        ok_corner = new Image(Display.getDefault(), "icons/input_ok_corner.gif");
 52        error_corner = new Image(Display.getDefault(), "icons/input_error_corner.gif");
 53        
 54        msg_warning = new Image(Display.getDefault(), "icons/standard_msg_warning.gif");
 55        msg_ok = new Image(Display.getDefault(), "icons/standard_msg_ok.gif");
 56        msg_error = new Image(Display.getDefault(), "icons/standard_msg_error.gif");
 57        
 58        tempCorner = warning_corner;
 59        tempMsg = msg_warning;
 60    }

 61
 62    private void initControls(int style) {
 63        text = new Text(this, SWT.FLAT|style);
 64        text.addModifyListener(new ModifyListener(){
 65            public void modifyText(ModifyEvent e) {
 66                if(SuperText.this.text.getText()!=null && !SuperText.this.text.getText().equals("")) {
 67                    if(SuperText.this.text.getText().length()>10{
 68                        tempMsg = msg_ok;
 69                        tempCorner = ok_corner;
 70                        SuperText.this.redraw();
 71                    }
else {
 72                        tempCorner = error_corner;
 73                        tempMsg = msg_error;
 74                        SuperText.this.redraw();
 75                    }

 76
 77                }
else {
 78                    System.out.println("do here");
 79                    tempCorner = warning_corner;
 80                    tempMsg = msg_warning;
 81                    SuperText.this.redraw();
 82                }

 83            }

 84        }
);
 85    }

 86
 87    @Override
 88    public void dispose() {
 89        super.dispose();
 90    }

 91
 92    @Override
 93    protected void checkSubclass() {}
 94
 95    public void paintControl(PaintEvent e) {
 96        System.out.println("paintControl");
 97        GC gc = e.gc;
 98        gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
 99        gc.fillGradientRectangle(11this.getSize().x-11-tempMsg.getBounds().width, this.getSize().y-2false);
100        gc.drawRectangle(11this.getSize().x-11-tempMsg.getBounds().width, this.getSize().y-2);
101        
102        //set warning corner
103        gc.drawImage(tempCorner, this.getSize().x-11-tempMsg.getBounds().width-tempCorner.getBounds().width, this.getSize().y-2-tempCorner.getBounds().height);
104        
105        //set msg warning
106        gc.drawImage(tempMsg, this.getSize().x-tempMsg.getBounds().width-51);
107        gc.dispose();
108    }

109
110    public void controlMoved(ControlEvent e) {}
111
112    public void controlResized(ControlEvent e) {
113        text.setBounds(22this.getSize().x-12-tempMsg.getBounds().width-tempCorner.getBounds().width, this.getSize().y-3);
114    }

115
116}

117
实现原理其实就是绘制!所谓自定义控件其实就是两种,一种就是用C写,然后JAVA调用,用SWT封装;第二种就是绘制;第一种的做法其实并不好,会让SWT的控件失去跨平台性~一般的做法都是第二种。
大家可以看到整个类中没有布局的出现,其实真的是“无布局才是最好的布局!”,没有布局反倒更加的灵活了,方便谈不上,但是灵活性大大提高;
Demo下载
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics