下面列出了javax.swing.JComponent#addMouseMotionListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Registers a component for tooltip management.
* <p>
* This will register key bindings to show and hide the tooltip text
* only if <code>component</code> has focus bindings. This is done
* so that components that are not normally focus traversable, such
* as <code>JLabel</code>, are not made focus traversable as a result
* of invoking this method.
*
* @param component a <code>JComponent</code> object to add
* @see JComponent#isFocusTraversable
*/
protected void registerComponent(JComponent component) {
component.removeMouseListener(this);
component.addMouseListener(this);
component.removeMouseMotionListener(moveBeforeEnterListener);
component.addMouseMotionListener(moveBeforeEnterListener);
if (shouldRegisterBindings(component)) {
// register our accessibility keybindings for this component
// this will apply globally across L&F
// Post Tip: Ctrl+F1
// Unpost Tip: Esc and Ctrl+F1
InputMap inputMap = component.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap actionMap = component.getActionMap();
if (inputMap != null && actionMap != null) {
//XXX remove
}
}
}
/**
* 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);
}
}
@Override
public void installUI(JComponent c) {
c.addFocusListener(repaintFocusListener);
c.addMouseListener(mouseListener);
c.addMouseMotionListener(mouseListener);
c.addKeyListener(keyListener);
JColorWell well = (JColorWell) c;
ColorChangeListener ccl = new ColorChangeListener(c);
listenerMap.put(well, ccl);
well.getColorSelectionModel().addChangeListener(ccl);
ShowColorPaletteActionListener colorPaletteActionListener = new ShowColorPaletteActionListener();
c.putClientProperty(DOUBLE_CLICK_ACTION_PROPERTY,
colorPickerActionListener);
c.putClientProperty(SPACE_KEY_ACTION_PROPERTY,
colorPickerActionListener);
c.putClientProperty(SINGLE_CLICK_ACTION_PROPERTY,
colorPaletteActionListener);
c.putClientProperty(DOWN_KEY_ACTION_PROPERTY,
colorPaletteActionListener);
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
spinner.addChangeListener(valueListener);
label = createLabel();
maybeAdd(label, "Label");
UpdateLabelListener changeListener = new UpdateLabelListener(
(JSpinner) c, label);
changeListener.refreshLabel();
c.putClientProperty(PROPERTY_LABEL_CHANGE_LISTENER, changeListener);
((JSpinner) c).addChangeListener(changeListener);
((JSpinner) c).addPropertyChangeListener(changeListener);
c.addMouseListener(dragListener);
c.addMouseMotionListener(dragListener);
c.setBorder(null);
}
/**
* 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);
}
/**
*
* @param event
*/
private void initiateToolTip(MouseEvent event) {
JComponent component = (JComponent) event.getSource();
String newToolTipText = component.getToolTipText(event);
newToolTipComponent = ((ComponentToolTipProvider) component).getCustomToolTipComponent(event);
if (newToolTipComponent == null)
return;
component.removeMouseMotionListener(moveBeforeEnterListener);
Point location = event.getPoint();
// ensure tooltip shows only in proper place
if (location.x < 0 || location.x >= component.getWidth() || location.y < 0
|| location.y >= component.getHeight()) {
return;
}
component.removeMouseMotionListener(this);
component.addMouseMotionListener(this);
mouseEvent = event;
toolTipText = newToolTipText;
preferredLocation = component.getToolTipLocation(event);
insideComponent = component;
showTipWindow();
}
/** Creates a new instance of SplashDnDSupport */
DragManager(JComponent component) {
this.component = component;
dSource = new DragSource();
dRecognizer = dSource.createDefaultDragGestureRecognizer(this.component,DnDConstants.ACTION_MOVE,this);
dTarget = new DropTarget(this.component,DnDConstants.ACTION_MOVE,this);
component.addMouseMotionListener(this);
oCursor = component.getCursor();
}
/**
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 installUI(JComponent slider) {
slider.addMouseListener(this);
slider.addMouseMotionListener(this);
slider.addFocusListener(focusListener);
slider.addKeyListener(keyListener);
slider.addComponentListener(compListener);
slider.addPropertyChangeListener(propertyListener);
slider.addPropertyChangeListener(THUMB_SHAPE_PROPERTY,
thumbShapeListener);
calculateGeometry();
}
/**
*
* @param event
*/
private void initiateToolTip(MouseEvent event) {
JComponent component = (JComponent) event.getSource();
String newToolTipText = component.getToolTipText(event);
newToolTipComponent = ((ComponentToolTipProvider) component).getCustomToolTipComponent(event);
if (newToolTipComponent == null)
return;
component.removeMouseMotionListener(moveBeforeEnterListener);
Point location = event.getPoint();
// ensure tooltip shows only in proper place
if (location.x < 0 || location.x >= component.getWidth() || location.y < 0
|| location.y >= component.getHeight()) {
return;
}
component.removeMouseMotionListener(this);
component.addMouseMotionListener(this);
mouseEvent = event;
toolTipText = newToolTipText;
preferredLocation = component.getToolTipLocation(event);
insideComponent = component;
showTipWindow();
}
@Override
public void installUI(JComponent slider) {
slider.addMouseListener(this);
slider.addMouseMotionListener(this);
slider.addFocusListener(focusListener);
slider.addKeyListener(keyListener);
slider.addComponentListener(compListener);
slider.addPropertyChangeListener(propertyListener);
slider.addPropertyChangeListener(THUMB_SHAPE_PROPERTY, thumbShapeListener);
calculateGeometry();
}
@Override
public void setProperty( String sProperty, Object oValue )
{
// InteractiveRenderer(iv) is only for Swing
if ( sProperty.equals( IDeviceRenderer.UPDATE_NOTIFIER ) && iv != null )
{
_iun = (IUpdateNotifier) oValue;
iv.setUpdateNotifier( _iun );
_lhmAllTriggers.clear( );
Object obj = _iun.peerInstance( );
if ( obj instanceof JComponent )
{
JComponent jc = (JComponent) obj;
if ( _eh != null )
{
// We can't promise to remove all the old swtEventHandler
// due to SWT limitation here, so be sure to just attach the
// update_notifier only to one renderer.
jc.removeMouseListener( _eh );
jc.removeMouseMotionListener( _eh );
jc.removeKeyListener( _eh );
jc.removeFocusListener( _eh );
}
_eh = new SwingEventHandler( iv,
_lhmAllTriggers,
_iun,
getULocale( ) );
jc.addMouseListener( _eh );
jc.addMouseMotionListener( _eh );
jc.addKeyListener( _eh );
jc.addFocusListener( _eh );
}
}
super.setProperty( sProperty, oValue );
}
/** Creates a new instance of Resizer */
public Resizer(JComponent jc, Rectangle r, boolean editable, ResizerListener rl) {
this.jc = jc;
setRect(r);
this.editable = editable;
if (editable)
defaultCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
this.rl = rl;
jc.addMouseListener(this);
jc.addMouseMotionListener(this);
minSize = new Dimension(Constants.MIN_CELL_SIZE, Constants.MIN_CELL_SIZE);
maxSize = new Dimension(jc.getPreferredSize());
setEnabled(true);
}
private void initiateToolTip(MouseEvent event) {
if (event.getSource() == window) {
return;
}
JComponent component = (JComponent)event.getSource();
component.removeMouseMotionListener(moveBeforeEnterListener);
exitTimer.stop();
Point location = event.getPoint();
// ensure tooltip shows only in proper place
if (location.x < 0 ||
location.x >=component.getWidth() ||
location.y < 0 ||
location.y >= component.getHeight()) {
return;
}
if (insideComponent != null) {
enterTimer.stop();
}
// A component in an unactive internal frame is sent two
// mouseEntered events, make sure we don't end up adding
// ourselves an extra time.
component.removeMouseMotionListener(this);
component.addMouseMotionListener(this);
boolean sameComponent = (insideComponent == component);
insideComponent = component;
if (tipWindow != null){
mouseEvent = event;
if (showImmediately) {
Rectangle rect = provider.getToolTipSourceBounds( event.getPoint() );
if( null != rect ) {
String newToolTipText = startToolTipCalculation( rect, event.getPoint() );
if (!sameComponent || !toolTipText.equals(newToolTipText) /*||
!sameLoc*/) {
toolTipText = newToolTipText;
showTipWindow();
}
}
} else {
enterTimer.start();
}
}
}
private void addMouseMotionListener(MouseMotionListener ml) {
for (JComponent l : layers) {
l.addMouseMotionListener(ml);
}
}
/**
* Adds a component on this Canvas inside a frame.
*
* @param comp The component to add to the canvas.
* @param toolBox Should be set to true if the resulting frame is
* used as a toolbox (that is: it should not be counted as a
* frame).
* @param popupPosition A preferred {@code PopupPosition}.
* @param resizable Whether this component can be resized.
* @return The {@code JInternalFrame} that was created and added.
*/
private JInternalFrame addAsFrame(JComponent comp, boolean toolBox,
PopupPosition popupPosition,
boolean resizable) {
final int FRAME_EMPTY_SPACE = 60;
final JInternalFrame f = (toolBox) ? new ToolBoxFrame()
: new JInternalFrame();
Container con = f.getContentPane();
if (con instanceof JComponent) {
JComponent c = (JComponent)con;
c.setOpaque(false);
c.setBorder(null);
}
if (comp.getBorder() != null) {
if (comp.getBorder() instanceof EmptyBorder) {
f.setBorder(Utility.blankBorder(10, 10, 10, 10));
} else {
f.setBorder(comp.getBorder());
comp.setBorder(Utility.blankBorder(5, 5, 5, 5));
}
} else {
f.setBorder(null);
}
final FrameMotionListener fml = new FrameMotionListener(f);
comp.addMouseMotionListener(fml);
comp.addMouseListener(fml);
if (f.getUI() instanceof BasicInternalFrameUI) {
BasicInternalFrameUI biu = (BasicInternalFrameUI) f.getUI();
biu.setNorthPane(null);
biu.setSouthPane(null);
biu.setWestPane(null);
biu.setEastPane(null);
}
f.getContentPane().add(comp);
f.setOpaque(false);
f.pack();
int width = f.getWidth();
int height = f.getHeight();
if (width > getWidth() - FRAME_EMPTY_SPACE) {
width = Math.min(width, getWidth());
}
if (height > getHeight() - FRAME_EMPTY_SPACE) {
height = Math.min(height, getHeight());
}
f.setSize(width, height);
Point p = chooseLocation(comp, width, height, popupPosition);
f.setLocation(p);
this.addToCanvas(f, MODAL_LAYER);
f.setName(comp.getClass().getSimpleName());
f.setFrameIcon(null);
f.setVisible(true);
f.setResizable(resizable);
try {
f.setSelected(true);
} catch (java.beans.PropertyVetoException e) {}
return f;
}
public void register(JComponent node) {
node.addMouseMotionListener(mouseMotionEventHandler);
node.addMouseListener(mouseEventHandler);
node.addMouseWheelListener(mouseWheelEventHandler);
}
/**
* Registers a component for tooltip management.
* <p>
* This will register key bindings to show and hide the tooltip text only if
* <code>component</code> has focus bindings. This is done so that components that are not
* normally focus traversable, such as <code>JLabel</code>, are not made focus traversable as a
* result of invoking this method.
*
* @param component a <code>JComponent</code> object to add
* @see JComponent#isFocusTraversable
*/
public void registerComponent(JComponent component) {
if (!(component instanceof ComponentToolTipProvider))
return;
component.addMouseListener(this);
component.addMouseMotionListener(moveBeforeEnterListener);
ToolTipManager.sharedInstance().unregisterComponent(component);
}
/**
* Registers a component for tooltip management.
* <p>
* This will register key bindings to show and hide the tooltip text only if
* <code>component</code> has focus bindings. This is done so that components that are not
* normally focus traversable, such as <code>JLabel</code>, are not made focus traversable as a
* result of invoking this method.
*
* @param component a <code>JComponent</code> object to add
* @see JComponent#isFocusTraversable
*/
public void registerComponent(JComponent component) {
if (!(component instanceof ComponentToolTipProvider))
return;
component.addMouseListener(this);
component.addMouseMotionListener(moveBeforeEnterListener);
ToolTipManager.sharedInstance().unregisterComponent(component);
}