下面列出了javax.swing.JComponent#addMouseListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* <pre>
* 初始化边框焦点监听器
*
* Initializes the border focus listener
* <pre>
*
* @param c
*/
protected void installFocusListener(JComponent c)
{
handle = new LuckComboboxFocusHandle();
isFocusBorder = UIManager.getBoolean(LuckComboBoxUIBundle.ISFOCUSBORDER);
if (isFocusBorder)
{
contentShape = new RoundRectangle2D.Float(0, 0, 0, 0, 8, 8);
borderShape = new RoundRectangle2D.Float(0, 0, 0, 0, 8, 8);
c.addMouseListener(handle);
c.addFocusListener(handle);
}
}
/**
* 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) {
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);
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.addPropertyChangeListener(JPalette.PROPERTY_COLORS,
propertyLayoutListener);
c.addPropertyChangeListener(PaletteUI.PROPERTY_HIGHLIGHT,
propertyRepaintListener);
c.setLayout(new PaletteLayoutManager());
c.setRequestFocusEnabled(true);
c.addMouseListener(mouseListener);
c.addFocusListener(focusListener);
c.addKeyListener(keyListener);
c.setFocusable(true);
Fields fields = getFields((JPalette) c, true);
fields.install();
c.setBorder(new CompoundBorder(new LineBorder(new Color(0xB0B0B0)),
new FocusedBorder(getDefaultBorder())));
relayoutCells((JPalette) c);
}
/**
* <pre>
* 初始化边框焦点监听器
*
* Initializes the border focus listener
* <pre>
*
* @param c
*/
protected void installFocusListener(JComponent c)
{
handle = createFocusHandle();
borderShape = new RoundRectangle2D.Float(0, 0, 0, 0, 8, 8);
c.addMouseListener(handle);
c.addFocusListener(handle);
}
@Override
public void addMarginProvider(MarginProvider marginProvider) {
JComponent component = marginProvider.getComponent();
// just in case we get repeated calls
component.removeMouseListener(focusingMouseListener);
component.addMouseListener(focusingMouseListener);
connectedProvider.getListingPanel().addMarginProvider(marginProvider);
}
protected void setMouseListener(JComponent c) {
c.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (selectedItem != null)
selectedItem.deselect();
selectedItem = CheckListItemPanel.this;
CheckListItemPanel.this.select();
}
});
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.addMouseListener(mouseListener);
c.addPropertyChangeListener(JBreadCrumb.PATH_KEY, refreshUIListener);
c.addPropertyChangeListener(JBreadCrumb.FORMATTER_KEY,
refreshUIListener);
c.addPropertyChangeListener(PROPERTY_SEPARATOR_ICON, refreshUIListener);
refreshUI((JBreadCrumb<?>) c);
}
/**
* @param pf Portfolio which is dynamically evaluated at mouse-even-time for
* any contained private companies
*/
public static void addMouseListener(JComponent c,ORUIManager orUIManager,PortfolioModel pf) {
if (isEnabled(false)) {
HexHighlightMouseListener l = new HexHighlightMouseListener(orUIManager);
l.portfolio = pf;
c.addMouseListener(l);
}
}
/**
* @param tileId ID of the tile the occurrences of which should be highlighted on
* the map
* @param enableIrrespectiveOfHighlightConfig If true, the mouse listener is
* enabled irrespective of the base configuration. Needed since some highlighting
* should not be disabled by configuration.
*/
public static void addMouseListener(JComponent c,ORUIManager orUIManager, Tile tile,boolean enableIrrespectiveOfHighlightConfig) {
if (isEnabled(enableIrrespectiveOfHighlightConfig)) {
HexHighlightMouseListener l = new HexHighlightMouseListener(orUIManager);
l.tile = tile;
c.addMouseListener(l);
}
}
/**
* @param p Public company the hexes associated to it (home, destination, ...) are
* to be highlighted (in case of events)
* @param enableIrrespectiveOfHighlightConfig If true, the mouse listener is
* enabled irrespective of the base configuration. Needed since some highlighting
* should not be disabled by configuration.
*/
public static void addMouseListener(JComponent c,ORUIManager orUIManager,PublicCompany p,boolean enableIrrespectiveOfHighlightConfig) {
if (isEnabled(enableIrrespectiveOfHighlightConfig)) {
HexHighlightMouseListener l = new HexHighlightMouseListener(orUIManager);
l.addToHexListDistinct(p.getHomeHexes());
l.addToHexListDistinct(p.getDestinationHex());
c.addMouseListener(l);
}
}
public ComponentTitledBorder(Component comp, JComponent container,
Border border) {
this.comp = comp;
this.container = container;
this.border = border;
container.addMouseListener(this);
}
@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();
}
/**
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;
}
/**
* <pre>
* 初始化边框焦点监听器
*
* Initializes the border focus listener
* <pre>
*
* @param c
*/
protected void installFocusListener(JComponent c)
{
handle = createFocusHandle();
borderShape = new RoundRectangle2D.Float(0, 0, 0, 0, 8, 8);
c.addMouseListener(handle);
c.addFocusListener(handle);
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.addKeyListener(keyListener);
c.addMouseListener(mouseListener);
layout.addPropertyChangeListener(layoutListener);
}
@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();
}
private void addMouseListener(MouseListener ml) {
for (JComponent l : layers) {
l.addMouseListener(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;
}
/**
* 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);
}