大夢想家

          5年開發工程師,2年實施經理,X年售前顧問,......
          數據加載中……
          打造仿淘寶注冊的Text(二)
          上次貼了幾張圖片出來顯擺,這次徹底公布代碼~大家看看原理就好,有興趣的朋友可以和我聯系,把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下載

          客戶虐我千百遍,我待客戶如初戀!

          posted on 2008-01-08 20:20 阿南 閱讀(1849) 評論(3)  編輯  收藏 所屬分類: Eclipse-RCPEclipse-SWT個人原創

          評論

          # re: 打造仿淘寶注冊的Text(二) 2008-01-08 22:00 xidudui

          很有用多謝誒
            回復  更多評論    

          # re: 打造仿淘寶注冊的Text(二)[未登錄] 2008-01-09 17:14 fisher

          不錯,期待作者能將它封裝一下。
            回復  更多評論    

          # re: 打造仿淘寶注冊的Text(二) 2008-01-10 15:30 xidudui

          受教了
            回復  更多評論    
          主站蜘蛛池模板: 雷州市| 江津市| 永泰县| 香河县| 洪泽县| 柳州市| 会泽县| 郴州市| 永城市| 正宁县| 仁寿县| 射阳县| 子长县| 揭阳市| 邢台市| 龙州县| 五常市| 昆明市| 周宁县| 曲靖市| 葫芦岛市| 手游| 鹰潭市| 宾阳县| 广西| 云霄县| 孟州市| 津市市| 张掖市| 永平县| 凤阳县| 毕节市| 托克逊县| 绥德县| 黄龙县| 布拖县| 潢川县| 莱阳市| 靖宇县| 井陉县| 定西市|