目的:在GWT项目中注册一个UI控件到一个register类中,并以一个关键字作为查找条件,以方便被其他地方的类获取。
设计类Register,里面的方法全部是静态,将数据用HashMap来存储
在EntryPoint实现注册方法
显示结果:
[img]http://dl.iteye.com/upload/attachment/190147/c6534e3d-2317-3f31-ac52-8c77dae361ed.bmp[/img]
设计类Register,里面的方法全部是静态,将数据用HashMap来存储
import com.google.gwt.user.client.rpc.IsSerializable;
import com.google.gwt.user.client.ui.UIObject;
public class Register implements IsSerializable {
private static HashMap objects=new HashMap();
public static UIObject get(String name){
return objects.get(name);
}
public static void regist(String name,UIObject object){
if(objects.containsKey(name)){
unregist(name);
}
objects.put(name, object);
}
public static void unregist(String name){
objects.remove(name);
}
}
在EntryPoint实现注册方法
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
public class Gap implements EntryPoint {
public void onModuleLoad() {
Button btn=new Button("ABC");
Register.regist("btnok", btn);
Button bs=(Button) Register.get("btnok");
GWT.log(bs.getText(), null);
RootPanel.get().add(btn);
}
}
显示结果:
[img]http://dl.iteye.com/upload/attachment/190147/c6534e3d-2317-3f31-ac52-8c77dae361ed.bmp[/img]