GWT Widget.addHandler

发布于 2024-11-17 19:44:17 字数 1864 浏览 0 评论 0原文

我正在尝试利用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哑剧 2024-11-24 19:44:17

GwtEvent.Type 用于根据事件特定且唯一的对象分派事件(对象相等 - == - 用于匹配事件类型)。将 CustomMouseMoveHandler 作为 Type 传递给 addHandler 表示除用于 MouseMoveEvent 的事件类型之外的事件类型(事实上,在这种情况下,每个 CustomMouseMoveHandler 将被分配给不同的事件 Type,因为每个对象都是不同的)。

您需要从 MouseMoveEvent 本身获取事件 Type(使用静态 getType() 方法)。

不要在 CustomMouseMoveHandler 中扩展 GwtEvent.Type

public class CustomMouseMoveHandler
  implements MouseMoveHandler
{
  ...
}

并添加处理程序:

cp.addDomHandler(handler, MouseMoveEvent.getType());

GwtEvent.Type is used to dispatch events based on an event specific and unique object (object equality - == - is used to match event types). Passing your CustomMouseMoveHandler as the Type to addHandler indicates an event type other than that used for MouseMoveEvents (Indeed in this case every CustomMouseMoveHandler would be assigned to a different event Type since each object is different).

Instead of extending GwtEvent.Type<MouseMoveHandler> in your handler you need to get the event Type from MouseMoveEvent itself (using the static getType() method).

Don't extend GwtEvent.Type in your CustomMouseMoveHandler:

public class CustomMouseMoveHandler
  implements MouseMoveHandler
{
  ...
}

And to add the handler:

cp.addDomHandler(handler, MouseMoveEvent.getType());
不必在意 2024-11-24 19:44:17

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.

你不是我要的菜∠ 2024-11-24 19:44:17

这是我解决问题的方法。我想将处理程序添加到 NumberLabel。这就是有效的方法:

    final NumberLabel<Long> label = new NumberLabel<Long>();
    label.setValue(2000l);
    label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    MouseOverHandler handler = new MouseOverHandler() {

        public void onMouseOver(MouseOverEvent event) {
            System.out.println("mouse over");
        }
    };
    Widget widget = label.asWidget();
    widget.addDomHandler(handler, MouseOverEvent.getType());

将 is 视为 Widget 就可以了。

顺便说一句,System.out.println 工作了。

Here's how I solved my problem. I wanted to add handlers to a NumberLabel. This is what worked:

    final NumberLabel<Long> label = new NumberLabel<Long>();
    label.setValue(2000l);
    label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
    MouseOverHandler handler = new MouseOverHandler() {

        public void onMouseOver(MouseOverEvent event) {
            System.out.println("mouse over");
        }
    };
    Widget widget = label.asWidget();
    widget.addDomHandler(handler, MouseOverEvent.getType());

Treating is as a Widget did the trick.

By the way, System.out.println worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文