<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    大夢想家

    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 阿南 閱讀(1847) 評論(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

    受教了
      回復  更多評論    
    主站蜘蛛池模板: 亚洲黄片手机免费观看| 国产传媒在线观看视频免费观看| 国产成人亚洲精品青草天美| 国产无遮挡色视频免费观看性色| 亚洲一区二区久久| 成年女人毛片免费视频| 亚洲最大福利视频| 免费A级毛片无码A| 成年私人影院免费视频网站| 日韩免费高清大片在线| 亚洲av午夜电影在线观看 | 久久久久久国产a免费观看不卡 | 免费在线视频一区| 在线精品免费视频无码的| 99精品在线免费观看| 一个人免费视频在线观看www| 一本色道久久综合亚洲精品| 久爱免费观看在线网站| 中文字幕亚洲综合小综合在线| 亚洲一区二区三区夜色| 最好免费观看韩国+日本| 一级毛片免费观看不收费| 亚洲AV无码精品色午夜在线观看| 一个人看的www在线观看免费| 久久久久久国产精品免费无码| 中文字字幕在线高清免费电影| 亚洲日本人成中文字幕| 亚洲精品视频久久| 亚洲美免无码中文字幕在线| 亚洲福利在线播放| 日韩亚洲精品福利| 99久久99久久精品免费观看 | 亚洲国产精品成人网址天堂| 国产乱子影视频上线免费观看| 国产又大又黑又粗免费视频 | 激情内射亚洲一区二区三区| 91亚洲自偷手机在线观看| 亚洲精品欧洲精品| 亚洲成aⅴ人在线观看| 深夜国产福利99亚洲视频| 亚洲国产午夜福利在线播放|