下面列出了怎么用java.awt.event.MouseEvent的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void mousePressed(MouseEvent e) {
int mx = e.getX();
int my = e.getY();
int col = (e.getX() - ICON_SEP) / (ICON_WIDTH + ICON_SEP);
int row = (e.getY() - ICON_SEP) / (ICON_HEIGHT + ICON_SEP);
int x0 = ICON_SEP + col * (ICON_SEP + ICON_WIDTH);
int y0 = ICON_SEP + row * (ICON_SEP + ICON_HEIGHT);
if (mx >= x0 && mx < x0 + ICON_WIDTH && my >= y0 && my < y0 + ICON_HEIGHT && col >= 0 && col < tools.length
&& row >= 0 && row < tools[col].length) {
toolPressed = tools[col][row];
inTool = true;
toolX = x0;
toolY = y0;
repaint();
} else {
toolPressed = null;
inTool = false;
}
}
/** Creates a window */
public LizziePane(LizzieMain owner) {
// super(owner);
// initCompotents();
// input = owner.input;
// installInputListeners();
setOpaque(false);
addMouseListener(
new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Lizzie.frame.getFocus();
}
});
}
public FileSaver(JProgressBar bar, JLabel label) {
this.bar = bar;
this.label = label;
final JPopupMenu menu = new JPopupMenu("Cancel");
final JMenuItem item = new JMenuItem("Cancel");
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setCancel(true);
}
});
menu.add(item);
this.label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent ev) {
if (SwingUtilities.isRightMouseButton(ev) && isExtracting())
menu.show(ev.getComponent(), ev.getX(), ev.getY());
}
});
}
protected EditorGlassPane() {
super();
setOpaque(false);
setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
@Override public boolean accept(Component c) {
return Objects.equals(c, getEditorTextField());
}
});
addMouseListener(new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
if (!getEditorTextField().getBounds().contains(e.getPoint())) {
renameTitle.actionPerformed(new ActionEvent(e.getComponent(), ActionEvent.ACTION_PERFORMED, ""));
}
}
});
}
/**
* 鼠标放开
*/
public void mouseReleased(MouseEvent e) {
if (isLock || isClose || !isLoad) {
return;
}
int type = ACTION_UP;
int button = e.getButton();
touch.action = type;
touch.type = button;
touch.pointer = 1;
touch.x = e.getX() - tx;
touch.y = e.getY() - ty;
this.touchX = (int) touch.x;
this.touchY = (int) touch.y;
this.isDraging = false;
try {
touchType[type] = false;
touchButtonReleased = button;
touchButtonPressed = LInput.NO_BUTTON;
onTouchUp(touch);
} catch (Exception ex) {
touchButtonPressed = LInput.NO_BUTTON;
touchButtonReleased = LInput.NO_BUTTON;
ex.printStackTrace();
}
}
public ShapesPanel() {
this.shapes = new ArrayList<MyShape>();
this.topColor = COLOR_BLUE;
this.bottomColor = COLOR_GREEN;
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
addShape(e.getPoint());
}
});
// animate the gradient endpoint colors in an infinite timeline
SwingRepaintTimeline.repaintBuilder(this)
.addPropertyToInterpolate("topColor", COLOR_BLUE, COLOR_GREEN)
.addPropertyToInterpolate("bottomColor", COLOR_GREEN, COLOR_BLUE)
.setDuration(1000)
.playLoop(RepeatBehavior.REVERSE);
}
private void maybeEditCustomExtKeyUsage(MouseEvent evt) {
if (evt.getClickCount() > 1) {
Point point = new Point(evt.getX(), evt.getY());
int row = jtCustomExtKeyUsages.rowAtPoint(point);
if (row != -1) {
try {
CursorUtil.setCursorBusy(JCustomExtendedKeyUsage.this);
jtCustomExtKeyUsages.setRowSelectionInterval(row, row);
editSelectedCustomExtKeyUsage();
} finally {
CursorUtil.setCursorFree(JCustomExtendedKeyUsage.this);
}
}
}
}
public void onMousePressed(MouseEvent e) {
_mouseDownPos.x = e.getX();
_mouseDownPos.y = e.getY();
MousePos mPos = new MousePos();
mPos.curTop = 0;
mPos.inItem = false;
_dragNode = getNodeByPosition(e.getX(), e.getY(), mPos);
}
private JLabel visualizeMediaItem(MediaItem mediaItem) throws MalformedURLException {
URL imgSource = new URL(getResizedImageSource(mediaItem.getBaseUrl()));
ImageIcon fetchedImage = new ImageIcon(imgSource);
JLabel label = new JLabel("", fetchedImage, JLabel.CENTER);
final PhotoListView self = this;
label.addMouseListener(
new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
onItemClicked.accept(self, mediaItem);
}
});
return label;
}
public ResultSetTableCellEditor(final JTextField textField) {
super(textField);
delegate = new EditorDelegate() {
@Override
public void setValue(Object value) {
val = value;
textField.setText((value != null) ? value.toString() : "");
}
@Override
public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
return ((MouseEvent) evt).getClickCount() >= 2;
}
return true;
}
@Override
public Object getCellEditorValue() {
String txtVal = textField.getText();
if (val == null && txtVal.equals("")) {
return null;
} else {
return txtVal;
}
}
};
textField.addActionListener(delegate);
// #204176 - workarround for MacOS L&F
textField.setCaret(new DefaultCaret());
}
public SingleClickTimer(JColorWell well, int doubleClickThreshold,
Long timeStamp, ActionListener actionListener,
MouseEvent trigger) {
super(doubleClickThreshold, timerListener);
this.well = well;
this.timeStamp = timeStamp;
this.actionListener = actionListener;
this.trigger = trigger;
setRepeats(false);
}
@Override
public void mouseDragged(MouseEvent e) {
this.stateTransitionTracker.turnOffModelChangeTracking();
try {
super.mouseDragged(e);
} finally {
this.stateTransitionTracker.onModelStateChanged();
}
}
protected void processMouseEvent(MouseEvent e) {
if (isSelected()) {
e.consume();
action = false;
} else {
action = true;
}
super.processMouseEvent(e);
}
/**
*
* @param e
*/
@Override
public void mouseReleased(MouseEvent e) {
nextX = lastX;//e.getX();
if (mouseInsideValueModelSliderBox) {
// recalculate fraction - fire property
setValueProperty(currentValue);
// june 2017 workaround for broken java 1.8
// mouseInsideValueModelSliderBox = false;
}
}
public boolean mouseMovedAction(MouseEvent event) {
if (overHandler != null) {
context.actionFactory.handleAction(overHandler, null, this, context);
return true;
}
return false;
}
/**
* mouse-over event on a visual item (node item or edge item)
*/
@Override
public void itemEntered(VisualItem item, MouseEvent e) {
// if ctrl is pressed, user zooms -> ignore itemEntered
if (e.getModifiers() == InputEvent.CTRL_MASK) {
ctrlZoom(e);
return;
}
// only mark items as highlighted if the layout process is active
RunLayoutControl rlc = new RunLayoutControl(viewManagerID);
if (rlc.isLayouting()) {
if (item instanceof NodeItem) {
/* set highlight attribute to true, NodeRenderer will change the color */
item.set(ColumnNames.IS_HIGHLIGHTED, true);
}
if (item instanceof EdgeItem) {
/* set highlight attribute to true, EdgeRenderer will change the color */
item.set(ColumnNames.IS_HIGHLIGHTED, true);
}
if (item instanceof TableDecoratorItem) {
/* set highlight attribute to true, EdgeRenderer will change the color */
item.set(ColumnNames.IS_HIGHLIGHTED, true);
}
}
}
public void mouseReleased( MouseEvent e ) {
if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
if ( nowZooming )
zoomWindow.hide();
nowZooming = false;
}
this.setCursor( Cursor.getDefaultCursor() );
}
private static void test(final Frame frame, final MouseEvent me) {
MouseEvent newme = SwingUtilities.convertMouseEvent(frame, me, frame);
if (me.getModifiersEx() != newme.getModifiersEx()
|| me.getModifiers() != newme.getModifiers()) {
fail(me, newme);
}
}
CommentsPanel(HttpRequestResponse requestResponse, SharedValues sharedValues) {
this.sharedValues = sharedValues;
commentsList = new JList<>();
commentsList.setCellRenderer(new JPanelListCellRenderer());
commentsList.setModel(new JPanelListModel());
commentsList.addMouseListener( new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
/*
Checks to see if it is a right click and if the click point is
within the bounds of a Comments borders
*/
if ( SwingUtilities.isRightMouseButton(e) && commentsList.getCellBounds(commentsList.locationToIndex(e.getPoint()),commentsList.locationToIndex(e.getPoint())).contains(e.getPoint())) {
int selectedIndex =
commentsList.locationToIndex(e.getPoint());
RequestComment selectedComment =
commentsList.getModel().getElementAt(selectedIndex);
if (sharedValues.getClient().getUsername().equals(selectedComment.getUserWhoCommented())) {
JPopupMenu menu = new JPopupMenu();
JMenuItem itemRemove = new JMenuItem("Delete");
itemRemove.addActionListener(e1 -> {
sharedValues.getCallbacks().printOutput(
"Deleting comment " + selectedComment);
((JPanelListModel) commentsList.getModel()).removeComment(selectedIndex);
sharedValues.getRequestCommentModel().removeCommentFromNewOrExistingReqResp(selectedComment, requestResponse);
});
menu.add(itemRemove);
menu.show(commentsList, e.getPoint().x, e.getPoint().y);
}
}
}
});
setPreferredSize(new Dimension(400, 700));
setViewportView(commentsList);
}
DatasetTreeView() {
// the catalog tree
tree = new JTree() {
public JToolTip createToolTip() {
return new MultilineTooltip();
}
};
tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(null, false)));
tree.setCellRenderer(new MyTreeCellRenderer());
tree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int selRow = tree.getRowForLocation(e.getX(), e.getY());
if (selRow != -1) {
TreeNode node = (TreeNode) tree.getLastSelectedPathComponent();
if (node instanceof VariableNode) {
Variable v = ((VariableNode) node).var;
firePropertyChangeEvent(new PropertyChangeEvent(this, "Selection", null, v));
}
}
}
});
tree.putClientProperty("JTree.lineStyle", "Angled");
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setToggleClickCount(1);
ToolTipManager.sharedInstance().registerComponent(tree);
// layout
setLayout(new BorderLayout());
add(new JScrollPane(tree), BorderLayout.CENTER);
}
public void mouseEntered(final MouseEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
inChart = true;
mouseX = e.getX();
mouseY = e.getY();
}
});
}
/**
* Creates a new state event object when the edge is clicked with the right mouse button.
*
* @param e The edge which is clicked.
* @param event The mouse event that caused the state change.
*
* @return The state object that describes the mouse state.
*/
public IMouseState createEdgeClickedRightState(final Edge e, final MouseEvent event) {
final CEdgeClickedRightState<NodeType, EdgeType> state =
new CEdgeClickedRightState<NodeType, EdgeType>(this, m_graph, e);
m_factory.createEdgeClickedRightAction().execute(state, event);
return state;
}
private void processMouseEvent(MouseEvent evt, boolean over) {
Object src = evt.getSource();
if (src instanceof JButton) {
JButton button = (JButton)src;
button.setContentAreaFilled(over);
button.setBorderPainted(over);
}
}
@Override
public void eventDispatched(final AWTEvent event) {
if (pickerWindow == null || !pickerWindow.isVisible()) return;
switch (event.getID()) {
case MouseEvent.MOUSE_PRESSED :
((MouseEvent) event).consume();
pickAndClose();
break;
case MouseEvent.MOUSE_CLICKED :
((MouseEvent) event).consume();
break;
case KeyEvent.KEY_PRESSED :
downKeyCode = ((KeyEvent) event).getKeyCode();
switch (downKeyCode) {
case KeyEvent.VK_ESCAPE :
cancelPipette();
break;
case KeyEvent.VK_ENTER :
pickAndClose();
break;
default :
break;
}
if (!keyDown) {
keyDown = true;
updatePipette(true);
}
break;
case KeyEvent.KEY_RELEASED :
keyDown = false;
Window picker = getPickerWindow();
if (picker != null) {
picker.repaint();
}
break;
default :
break;
}
}
@Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
ERDesignerGraph theGraph = (ERDesignerGraph) graph;
theGraph.setDragging(true);
}
@Override
public void mousePressed(MouseEvent e) {
table.requestFocus();
int row = table.getRow(e);
int col = table.getColumn(e);
setCursor(row, col, (e.getModifiersEx() & InputEvent.SHIFT_DOWN_MASK) != 0);
}
@Override
public void mouseMoved(Canvas canvas, Graphics g, MouseEvent e) {
updateLocation(canvas, e);
select.mouseMoved(canvas, g, e);
}
private static boolean oldIsRightMouseButton(MouseEvent e) {
return ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK);
}
@Override
public void mouseEntered(MouseEvent e) {
}
private void jButtonDownloadDefinitionsMouseExited(MouseEvent evt) {//GEN-FIRST:event_jButtonDownloadDefinitionsMouseExited
jLabelDescP0.setText("");
}