将MouseOverHandler添加到元素? [英] Adding a MouseOverHandler to an Element?

查看:299
本文介绍了将MouseOverHandler添加到元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想倾听GWT 1.6中的鼠标悬停事件。由于GWT 1.6引入了处理程序和不赞成使用的监听器,因此我不确定如何使用少量信息来完成此操作。

注意:我有一个Element对象。这就是我需要添加鼠标处理程序。我很抱歉我缺乏清晰度。



谢谢!

解决方案

我希望在我需要自己解决这个问题之前,我们会看到一个答案。他发布的示例代码中存在一些错误,但在此主题中由Mark Renouf发布包含我们需要的大部分内容。



假设您想要侦听鼠标悬停和鼠标悬停事件在自定义小部件上。在您的小部件中,添加两个方法:

$ p $ public HandlerRegistration addMouseOverHandler(MouseOverHandler handler){
return addDomHandler(handler,MouseOverEvent .getType());

$ b $ public HandlerRegistration addMouseOutHandler(MouseOutHandler handler){
return addDomHandler(handler,MouseOutEvent.getType());
}

然后创建一个处理程序类:

  public class MyMouseEventHandler implements MouseOverHandler,MouseOutHandler {
public void onMouseOver(final MouseOverEvent moe){
Widget widget =(Widget)moe.getSource() ;
widget.addStyleName(my-mouse-over);


public void onMouseOut(final MouseOutEvent moe){
Widget widget =(Widget)moe.getSource();
widget.removeStyleName(my-mouse-over);






$ p最后,将处理程序添加到窗口小部件中:

  myWidget.addMouseOverHandler(new MyMouseEventHandler()); 
myWidget.addMouseOutHandler(new MyMouseEventHandler());

如果您只收听鼠标悬停事件,则可以跳过鼠标悬停处理。如果你没有制作自定义小部件,那么这个小部件我已经有了一个添加处理程序的方法。

最后,根据线程的警告,请记住鼠标事件的 addDomHandler ,而不是 addHandler


I would like to listen for the mouse over event in GWT 1.6. Since GWT 1.6 has introduced handlers and deprecated listeners I'm unsure as to how I can accomplish this with what little information exists.

Note: I have an Element object. That's what I need to add the mouse handler to. I apologize for my lack of clarity.

Thanks!

解决方案

I was hoping we'd see an answer before I needed to tackle this myself. There are some errors in the example code he posted, but the post by Mark Renouf in this thread has most of what we need.

Let's say you want to listen for mouse over and mouse out events on a custom widget. In your widget, add two methods:

public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
  return addDomHandler(handler, MouseOverEvent.getType());
}

public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
  return addDomHandler(handler, MouseOutEvent.getType());
}

Then create a handler class:

public class MyMouseEventHandler implements MouseOverHandler, MouseOutHandler {
  public void onMouseOver(final MouseOverEvent moe) {
    Widget widget = (Widget) moe.getSource();
    widget.addStyleName("my-mouse-over");
  }

  public void onMouseOut(final MouseOutEvent moe) {
    Widget widget = (Widget) moe.getSource();
    widget.removeStyleName("my-mouse-over");
  }
}

Finally, add the handler to the widget:

myWidget.addMouseOverHandler(new MyMouseEventHandler());
myWidget.addMouseOutHandler(new MyMouseEventHandler());

If you are only listening to the mouse over event, you can skip the mouse out handling. And if you aren't making a custom widget, the widget my already have a method to add the handler.

Finally, per the warning from the thread, remember to addDomHandler for the mouse events, not addHandler.

这篇关于将MouseOverHandler添加到元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆