2011年8月8日 星期一

GWT Note 2

Event and Event handler

有四種方式來實作UI介面與事件觸發處理的關係
1.handler獨立成一個class,這樣會多很多class file不好review code和維護。
public class GwtEvents implements EntryPoint {
   ...
   public void onModuleLoad() {
     textfield = new TextBox();
     textfield.addKeyUpHandler(new MyHandler(this));
     ...
   }//end onModuleLoad
}//end class

public class MyHandler implements KeyUpHandler {
   ...
   public void onKeyUp(KeyUpEvent event) {
   ...
   }
}//end class

2.直接實作handler
public class GwtEvents implements EntryPoint, KeyUpHandler {
   ...
   public void onModuleLoad() {
     textfield = new TextBox();
     textfield.addKeyUpHandler(this);
     ...
    }

   public void onKeyUp(KeyUpEvent event) {
   ...
   }

}//end class


3. (Named)Inner class
public class GwtEvents implements EntryPoint { 
   public void onModuleLoad() { 
      textfield = new TextBox(); 
      textfield.addKeyUpHandler(new MyHandler()); 

   private class MyHandler implements KeyUpHandler { 
     public void onKeyUp(KeyUpEvent event) { 
     ... 
    } 

   }//end onModuleLoad 
}//end class 
4.Anonymous inner classes
public class GwtEvents implements EntryPoint { 
   ... 
   public void onModuleLoad() { 
     textfield = new TextBox(); 
     textfield.addKeyUpHandler(new KeyUpHandler() { 
     @Override 
     public void onKeyUp(KeyUpEvent event) { 
      ... 
     } 
    }); 
   }//end onModuleLoad 
}//end class 
請依情況選擇2、3的作法,避免用方法4。

沒有留言:

張貼留言

COVID-19 確診經歷紀實

原本以為真的是天選之人,就算先前家裡兩個小孩都確診都逃過了(可能有中獎但無症狀吧),不過就在2023年六月18日破解自認為天選之人的"心態",為什麼可以確認就是這天中獎的呢?因為在前都是居家上班,到人多的室內場所都會戴口罩,就剛好這天傍晚原本只想說要去附近的國...