下面列出了javax.swing.JComponent#addMouseWheelListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Actually register the rubber as the mouse listener for the provided component.
*
* @param component the related component
*/
public final void connectComponent (JComponent component)
{
// Clean up if needed
disconnectComponent(this.component);
// Remember the related component (to get visible rect, etc ...)
this.component = component;
if (component != null) {
// To be notified of mouse clicks
component.removeMouseListener(this); // No multiple notifications
component.addMouseListener(this);
// To be notified of mouse mouvements
component.removeMouseMotionListener(this); // No multiple notifs
component.addMouseMotionListener(this);
// To be notified of mouse wheel mouvements
component.removeMouseWheelListener(this); // No multiple notifs
component.addMouseWheelListener(this);
}
}
/**
* Actually register the rubber as the mouse listener for the provided
* component.
*
* @param component the related component
*/
public void connectComponent (JComponent component)
{
// Clean up if needed
disconnectComponent(this.component);
// Remember the related component (to get visible rect, etc ...)
this.component = component;
// To be notified of mouse clicks
component.removeMouseListener(this); // No multiple notifications
component.addMouseListener(this);
// To be notified of mouse mouvements
component.removeMouseMotionListener(this); // No multiple notifs
component.addMouseMotionListener(this);
// To be notified of mouse wheel mouvements
component.removeMouseWheelListener(this); // No multiple notifs
component.addMouseWheelListener(this);
}
/**
Construct a PanZoomListener that listens for events on a JComponent.
@param component The component to listen for mouse events on.
*/
public PanZoomListener(JComponent component) {
this.component = component;
component.addMouseListener(this);
component.addMouseMotionListener(this);
component.addMouseWheelListener(this);
panTriggerModifiers = InputEvent.BUTTON1_DOWN_MASK;
zoomTriggerModifiers = InputEvent.BUTTON2_DOWN_MASK;
zoomThreshold = DEFAULT_MOUSE_ZOOM_THRESHOLD;
enabled = true;
}
@Override
public void addNotify() {
super.addNotify();
JComponent comp = getComponent();
if (comp != null) {
comp.addMouseWheelListener(this);
}
}
public void register(JComponent node) {
node.addMouseMotionListener(mouseMotionEventHandler);
node.addMouseListener(mouseEventHandler);
node.addMouseWheelListener(mouseWheelEventHandler);
}