0
Iam創建一個自己的Button Widget,並且想要在這個Widget上註冊HasClickHandlers和HasMouseoverHandlers。 問題是,當我使用這個小部件,並添加一個ClickHandler,沒有反應。如何將HasEventHandlers註冊到自創的GWT-Widget?
MyButton extends Widget implements HasClickHandlers, HasMouseOverHandlers{
Element elem;
Element child1;
Element child2;
public MyButton(String txt1, String txt2){
elem = Document.get().createDivElement();
child1 = Document.get().createDivElement();
child2 = Document.get().createDivElement();
child1.setInnerText(txt1);
child2.setInnerText(txt2);
elem.appendChild(child1);
elem.appendChild(child2);
setStyleName(elem, "elem");
setStyleName(child1, "child1");
setStyleName(child1, "child2");
setElement(elem);
}
@Override
public HandlerRegistration addClickHandler(ClickHandler handler) {
return this.addHandler(handler, ClickEvent.getType());
}
@Override
public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
return this.addDomHandler(handler, MouseOverEvent.getType());
}
}
現在我使用這個小部件,應該是一個按鈕。
MyButton b1 = new MyButton("Hello", "People");
b1.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Window.alert("Hi");
}
});
對此點擊沒有反應。
b1.addMouseOverHandler(new MouseOverHandler() {
public void onMouseOver(MouseOverEvent event) {
anotherWidget.getElement().getStyle().setBackgroundColor("yellow");
}
});
mouseOverHandler有效,但不是clickHandler。 有什麼不對?