如何在GWT 1.4.61中添加MouseListener,KeyboardListener和ClickListener

时间:2019-04-25 19:08:21

标签: java gwt

如何在FooPanel或MyPanel上实现这些侦听器MouseListener,KeyboardListener和ClickListener?

当用户单击屏幕时,我应该能够捕获这些事件并执行适当的逻辑。最初,我通过将myPanel包装在其中来尝试了focuspanel,但这没有帮助。

我的代码:

public abstract class FooPanel extends Composite implements  SourcesClickEvents, SourcesMouseEvents, SourcesMouseWheelEvents{
private ClickListenerCollection clickListeners;
private KeyboardListenerCollection keyboardListeners;
private MouseListenerCollection mouseListeners;
private MouseWheelListenerCollection mouseWheelListeners;
    public FooPanel() {
       sinkEvents(Event.FOCUSEVENTS | Event.KEYEVENTS | Event.ONCLICK
          | Event.MOUSEEVENTS | Event.ONMOUSEWHEEL);
   }

/********************** Listeners ***********************************/
public void addClickListener(ClickListener listener) {
    if (clickListeners == null) {
        clickListeners = new ClickListenerCollection();
    }
    clickListeners.add(listener);
}

public void removeClickListener(ClickListener listener) {
    if (clickListeners != null) {
        clickListeners.remove(listener);
    }
}

public void addMouseListener(MouseListener listener) {
    if (mouseListeners == null) {
        mouseListeners = new MouseListenerCollection();
    }
    mouseListeners.add(listener);
}

public void removeMouseListener(MouseListener listener) {
    if (mouseListeners != null) {
        mouseListeners.remove(listener);
    }
}

public void addMouseWheelListener(MouseWheelListener listener) {
    if (mouseWheelListeners == null) {
        mouseWheelListeners = new MouseWheelListenerCollection();
    }
    mouseWheelListeners.add(listener);
}

public void removeMouseWheelListener(MouseWheelListener listener) {
    if (mouseWheelListeners != null) {
        mouseWheelListeners.remove(listener);
    }
}

public void addKeyboardListener(KeyboardListener listener) {
    if (keyboardListeners == null) {
        keyboardListeners = new KeyboardListenerCollection();
    }
    keyboardListeners.add(listener);
}

public void removeKeyboardListener(KeyboardListener listener) {
    if (keyboardListeners != null) {
        keyboardListeners.remove(listener);
    }
}
    public void onBrowserEvent(Event event) {
    switch (DOM.eventGetType(event)) {
    case Event.ONCLICK:
        if (clickListeners != null) {
            clickListeners.fireClick(this);
        }
        break;

    case Event.ONMOUSEDOWN:
    case Event.ONMOUSEUP:
    case Event.ONMOUSEMOVE:
    case Event.ONMOUSEOVER:
    case Event.ONMOUSEOUT:
        if (mouseListeners != null) {
            mouseListeners.fireMouseEvent(this, event);
        }
        break;

    case Event.ONMOUSEWHEEL:
        if (mouseWheelListeners != null) {
            mouseWheelListeners.fireMouseWheelEvent(this, event);
        }
        break;

    case Event.ONKEYDOWN:
    case Event.ONKEYUP:
    case Event.ONKEYPRESS:
        if (keyboardListeners != null) {
            keyboardListeners.fireKeyboardEvent(this, event);
        }
        break;
    }
}

}

public class MyPanel extends FooPanel {
    // logic
}

public class ClientPanel implements EntryPoint {
  Panel rootPanel= new Panel();
   public void onModuleLoad() {
    MyPanel p = new MyPanel();
     rootPanel.add(p);
    new ViewPort(rootPanel);
 }
}

0 个答案:

没有答案