获取所有鼠标事件类型

时间:2015-07-17 17:17:20

标签: gwt mouseevent

如果我为鼠标悬停事件设置处理程序,它将看起来像这样。

boxPanel.addDomHandler(new MouseOverHandler() {
            @Override
            public void onMouseOver(MouseOverEvent e) {
               //do something
            }
        }, MouseOverEvent.getType());

请注意最后的MouseOverEvent.getType(),因为它与我的问题有关。

如果我想为所有鼠标事件添加处理程序,如此

spotlightPanel.addDomHandler(new HandlesAllMouseEvents() {
        @Override
        public void onMouseOver(MouseOverEvent event) {
            //do something
        }
        @Override
        public void onMouseOut(MouseOutEvent event) {
            //do something
        }
        @Override
        public void onMouseDown(MouseDownEvent event) {
            //do something
        }
        @Override
        public void onMouseUp(MouseUpEvent event) {
            //do something
        }
        @Override
        public void onMouseMove(MouseMoveEvent event) { 
            //do something
        }
        @Override
        public void onMouseWheel(MouseWheelEvent event) {
            //do something
        }
    }, ???);

什么会???是?我尝试了MouseEvent.getType()之类的东西,但这种方法并不存在。我尝试输入多个事件,如(MouseOverEvent.getType(), MouseOutEvent.getType()),但语法都错了。引用HandlesAllMouseEvents()的所有鼠标事件的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

由于HandlesAllMouseEvents是多个事件类型的接口,因此您必须将每个事件类型添加到窗口小部件,但重用相同的处理程序。

    Widget spotlightPanel = [...]

    HandlesAllMouseEvents mouseHandler = new HandlesAllMouseEvents(){
        public void onMouseDown(MouseDownEvent event) {}
        public void onMouseUp(MouseUpEvent event) {}
        public void onMouseMove(MouseMoveEvent event) {}
        public void onMouseOut(MouseOutEvent event) {}
        public void onMouseOver(MouseOverEvent event) {}
        public void onMouseWheel(MouseWheelEvent event) {}
    };

    spotlightPanel.addDomHandler(mouseHandler, MouseDownEvent.getType());
    spotlightPanel.addDomHandler(mouseHandler, MouseUpEvent.getType());
    spotlightPanel.addDomHandler(mouseHandler, MouseMoveEvent.getType());
    spotlightPanel.addDomHandler(mouseHandler, MouseOverEvent.getType());
    spotlightPanel.addDomHandler(mouseHandler, MouseWheelEvent.getType());