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

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

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

    好好生活,努力工作,天天向上!

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      46 Posts :: 1 Stories :: 178 Comments :: 0 Trackbacks

       最近在實(shí)現(xiàn)一個(gè)圖片壓縮的功能,想在Eclipse.org上看有沒有辦法能夠通過SWT的API能夠改變圖片的分辨率,Eclipse.org上面提供了好些SWT的例子,發(fā)現(xiàn)了Display的post方法挺有趣的,以前沒有注意到,現(xiàn)在趕快把它記錄下來,post方法的參數(shù)為Event,通過制定這個(gè)Events的屬性,可以控制系統(tǒng)的鍵盤事件,比如保持shift鍵一直按著。下面是代碼:

     1 import org.eclipse.swt.*;
     2 import org.eclipse.swt.widgets.*;
     3 
     4 /**
     5  * 觸發(fā)系統(tǒng)的鍵盤事件  。 
     6  * @author vwpolo
     7  * <p>2009-6-1</p>
     8  */
     9 public class Snippet146 {
    10 
    11 public static void main(String[] args) {
    12     final Display display = new Display();
    13     final Shell shell = new Shell(display);
    14     final Text text = new Text(shell, SWT.BORDER);
    15     text.setSize(text.computeSize(150, SWT.DEFAULT));
    16     shell.pack();
    17     shell.open();
    18     new Thread(){
    19         public void run(){
    20             String string = "Love the method.";
    21             for (int i = 0; i < string.length(); i++) {
    22                 char ch = string.charAt(i);
    23                 boolean shift = Character.isUpperCase(ch);
    24                 ch = Character.toLowerCase(ch);
    25                 if (shift) {
    26                     Event event = new Event();
    27                     event.type = SWT.KeyDown;
    28                     event.keyCode = SWT.SHIFT;
    29                     display.post(event);    
    30                 }
    31                 Event event = new Event();
    32                 event.type = SWT.KeyDown;
    33                 event.character = ch;
    34                 display.post(event);
    35                 try {
    36                     Thread.sleep(10);
    37                 } catch (InterruptedException e) {}
    38                 event.type = SWT.KeyUp;
    39                 display.post(event);
    40                 try {
    41                     Thread.sleep(100);
    42                 } catch (InterruptedException e) {}
    43                 if (shift) {
    44                     event = new Event();
    45                     event.type = SWT.KeyUp;
    46                     event.keyCode = SWT.SHIFT;
    47                     display.post(event);    
    48                 }
    49             }
    50         }    
    51     }.start();
    52     while (!shell.isDisposed()) {
    53         if (!display.readAndDispatch()) display.sleep();
    54     }
    55     display.dispose();
    56 }
    57 }

      上面的例子功能是演示在一個(gè)文本框中模擬用戶輸入一段字符串,字符串的內(nèi)容是"Love the method.",還可以通過這個(gè)來移動(dòng)鼠標(biāo)的箭頭,像下面這樣:
     1 public static void main(String[] args) {
     2     final Display display = new Display();
     3     final Shell shell = new Shell(display);
     4     final Button button = new Button(shell,SWT.NONE);
     5     button.setSize(100,100);
     6     button.setText("Click");
     7     shell.pack();
     8     shell.open();
     9     button.addListener(SWT.MouseDown, new Listener() {
    10         public void handleEvent(Event e){
    11             System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")");
    12         }
    13     });
    14     final Point pt = display.map(shell, null, 5050);
    15     new Thread(){
    16         Event event;
    17         public void run(){
    18             try {
    19                 Thread.sleep(300);
    20             } catch (InterruptedException e) {}
    21             event = new Event();
    22             event.type = SWT.MouseMove;
    23             event.x = pt.x;
    24             event.y = pt.y;
    25             display.post(event);
    26             try {
    27                 Thread.sleep(300);
    28             } catch (InterruptedException e) {}
    29             event.type = SWT.MouseDown;
    30             event.button = 1;
    31             display.post(event);
    32             try {
    33                 Thread.sleep(300);
    34             } catch (InterruptedException e) {}
    35             event.type = SWT.MouseUp;
    36             display.post(event);
    37         }    
    38     }.start();
    39     while (!shell.isDisposed()) {
    40         if (!display.readAndDispatch()) display.sleep();
    41     }
    42     display.dispose();
    43 }

        首先創(chuàng)建一個(gè)100*100大小的按鈕,然后通過display.map(shell, null, 50, 50);這段代碼獲得指定shell上的相對坐標(biāo),這里是指shell的相對坐標(biāo)上的x=50,y=50,然后再換算成顯示屏幕系統(tǒng)的絕對坐標(biāo),設(shè)置事件類型為鼠標(biāo)移動(dòng),移動(dòng)的目標(biāo)坐標(biāo)位置是剛才我們?nèi)〉玫南到y(tǒng)坐標(biāo)

       event = new Event();
       event.type 
    = SWT.MouseMove;
       event.x 
    = pt.x;
       event.y 
    = pt.y;
       display.post(event);

     

      在觸發(fā)這個(gè)事件后,讓它休眠0.3

    try {
        Thread.sleep(
    300);
        } 
    catch (InterruptedException e) {}

     

      接著再將事件的類型設(shè)置為鼠標(biāo)按下事件:

        event.type = SWT.MouseDown;
        event.button 
    = 1;
        display.post(event);
      
      這樣就基本上模擬出了系統(tǒng)鼠標(biāo)的動(dòng)作了。大家有興趣可以研究一下,比如用它來做點(diǎn)壞事情,呵呵
    posted on 2009-06-01 11:15 VWPOLO 閱讀(2125) 評論(3)  編輯  收藏 所屬分類: SWT JFace

    Feedback

    # re: 使用Display的Post方法控制鍵盤 2009-06-01 13:57 無量字幕
    呵呵,有意思  回復(fù)  更多評論
      

    # re: 使用Display的Post方法控制鍵盤 2009-06-01 22:55 俊星
    第二段代碼有點(diǎn)問題:
    14 final Point pt = display.map(shell, null, 100, 50);
    換成
    14 final Point pt = display.map(shell, null, 50, 50);
    才能看到控制臺輸出的鼠標(biāo)響應(yīng)事件。
      回復(fù)  更多評論
      

    # re: 使用Display的Post方法控制鍵盤 2009-06-02 09:08 VWPOLO
    @俊星
    謝謝提醒,已經(jīng)修正了  回復(fù)  更多評論
      

    主站蜘蛛池模板: 99久久人妻精品免费二区| 亚洲成AV人片一区二区| 99视频免费播放| 国产免费福利体检区久久| 亚洲大成色www永久网址| 亚洲精品乱码久久久久久下载| 一二三四视频在线观看中文版免费 | 国产成人精品男人免费| 老汉精品免费AV在线播放| 一区二区免费国产在线观看| 亚洲乱码一区二区三区国产精品| 亚洲人成依人成综合网| 亚洲精品乱码久久久久久久久久久久 | 在线播放免费人成毛片乱码| 色妞www精品视频免费看| 国产精品亚洲综合五月天| 亚洲国产精品自在在线观看| 亚洲精品卡2卡3卡4卡5卡区| 亚洲欧洲精品成人久久奇米网 | 亚洲日韩中文字幕天堂不卡| 亚洲啪啪AV无码片| 久久精品国产精品亚洲下载| 亚洲国产免费综合| 免费一级特黄特色大片在线观看 | 亚洲日韩精品国产3区| 精品丝袜国产自在线拍亚洲| 亚洲免费在线视频观看| 亚洲视频国产视频| 亚洲白嫩在线观看| 亚洲国产日韩在线| 亚洲一级高清在线中文字幕| 亚洲图片中文字幕| 亚洲人成综合在线播放| 亚洲1区1区3区4区产品乱码芒果| 亚洲精品美女久久久久9999| 亚洲人成免费网站| 亚洲人片在线观看天堂无码| 精品国产亚洲一区二区三区在线观看| 亚洲狠狠色丁香婷婷综合| 黄色三级三级三级免费看| 一级做a免费视频观看网站|