GWT Widget.addHandler
我正在尝试利用 Widget.addHandler()
。然而,处理程序永远不会被调用。下面是我的示例代码。我需要改变什么来解决这个问题?
我的处理程序实现:
public class CustomMouseMoveHandler
extends GwtEvent.Type<MouseMoveHandler>
implements MouseMoveHandler
{
@Override
public void onMouseMove(MouseMoveEvent event) {
System.out.println("----> onMouseMove.");
}
}
我的 EntryPoint.OnModuleLoad()
:
ContentPanel cp = new ContentPanel();
cp.setHeaderVisible(false);
cp.setHeight(com.google.gwt.user.client.Window.getClientHeight());
CustomMouseMoveHandler handler = new CustomMouseMoveHandler();
cp.addHandler(handler, handler);
RootPanel.get().add(cp);
///// 于 2011 年 7 月 1 日添加。
以下完整的 GWT 简单代码也不起作用(应用 Jason 的提示)。请帮帮我。谢谢
package tut.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class GwtHandler implements EntryPoint, MouseMoveHandler {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
TextArea comp = new TextArea();
comp.setSize("200px", "200px");
comp.setText("Testing Text");
comp.addHandler(this, MouseMoveEvent.getType());
RootPanel.get().add(comp);
}
@Override
public void onMouseMove(MouseMoveEvent event) {
com.google.gwt.user.client.Window.alert("onMouseMove");
}
}
I am trying to utilize Widget.addHandler()
. However, the handler never gets called. Below is my sample code. What do I need to change to fix this?
My Handler Implementation:
public class CustomMouseMoveHandler
extends GwtEvent.Type<MouseMoveHandler>
implements MouseMoveHandler
{
@Override
public void onMouseMove(MouseMoveEvent event) {
System.out.println("----> onMouseMove.");
}
}
My EntryPoint.OnModuleLoad()
:
ContentPanel cp = new ContentPanel();
cp.setHeaderVisible(false);
cp.setHeight(com.google.gwt.user.client.Window.getClientHeight());
CustomMouseMoveHandler handler = new CustomMouseMoveHandler();
cp.addHandler(handler, handler);
RootPanel.get().add(cp);
/////
Added on 7/1/2011.
The following complete GWT simple code does not work either (with Jason's hint applied). Please help me out. Thanks
package tut.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class GwtHandler implements EntryPoint, MouseMoveHandler {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
TextArea comp = new TextArea();
comp.setSize("200px", "200px");
comp.setText("Testing Text");
comp.addHandler(this, MouseMoveEvent.getType());
RootPanel.get().add(comp);
}
@Override
public void onMouseMove(MouseMoveEvent event) {
com.google.gwt.user.client.Window.alert("onMouseMove");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GwtEvent.Type
用于根据事件特定且唯一的对象分派事件(对象相等 -==
- 用于匹配事件类型)。将CustomMouseMoveHandler
作为Type
传递给addHandler
表示除用于MouseMoveEvent
的事件类型之外的事件类型(事实上,在这种情况下,每个CustomMouseMoveHandler
将被分配给不同的事件Type
,因为每个对象都是不同的)。您需要从
MouseMoveEvent
本身获取事件Type
(使用静态getType()
方法)。不要在
CustomMouseMoveHandler
中扩展GwtEvent.Type
:并添加处理程序:
GwtEvent.Type
is used to dispatch events based on an event specific and unique object (object equality -==
- is used to match event types). Passing yourCustomMouseMoveHandler
as theType
toaddHandler
indicates an event type other than that used forMouseMoveEvent
s (Indeed in this case everyCustomMouseMoveHandler
would be assigned to a different eventType
since each object is different).Instead of extending
GwtEvent.Type<MouseMoveHandler>
in your handler you need to get the eventType
fromMouseMoveEvent
itself (using the staticgetType()
method).Don't extend
GwtEvent.Type
in yourCustomMouseMoveHandler
:And to add the handler:
DomEvents 必须使用 addDomHandler 进行注册,或者您必须为其事件类型接收 SinkEvents。 addDomHandler 是sinkEvents+addHandler 的快捷方式。
DomEvents have to be registered using addDomHandler, or you have to sinkEvents for their event type. addDomHandler is a shortcut for sinkEvents+addHandler.
这是我解决问题的方法。我想将处理程序添加到 NumberLabel。这就是有效的方法:
将 is 视为 Widget 就可以了。
顺便说一句,
System.out.println
工作了。Here's how I solved my problem. I wanted to add handlers to a NumberLabel. This is what worked:
Treating is as a Widget did the trick.
By the way,
System.out.println
worked.