本文整理了Java中java.awt.event.MouseEvent
类的一些代码示例,展示了MouseEvent
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MouseEvent
类的具体详情如下:
包路径:java.awt.event.MouseEvent
类名称:MouseEvent
[英]An event which indicates that a mouse action occurred in a component. A mouse action is considered to occur in a particular component if and only if the mouse cursor is over the unobscured part of the component's bounds when the action happens. Component bounds can be obscurred by the visible component's children or by a menu or by a top-level window. This event is used both for mouse events (click, enter, exit) and mouse motion events (moves and drags).
This low-level event is generated by a component object for:
Mouse Events
a mouse button is pressed
Mouse Motion Events
the mouse is moved
A MouseEvent
object is passed to every MouseListener
or MouseAdapter
object which is registered to receive the "interesting" mouse events using the component's addMouseListener
method. (MouseAdapter
objects implement the MouseListener
interface.) Each such listener object gets a MouseEvent
containing the mouse event.
A MouseEvent
object is also passed to every MouseMotionListener
or MouseMotionAdapter
object which is registered to receive mouse motion events using the component's addMouseMotionListener
method. (MouseMotionAdapter
objects implement the MouseMotionListener
interface.) Each such listener object gets a MouseEvent
containing the mouse motion event.
When a mouse button is clicked, events are generated and sent to the registered MouseListener
s. The state of modal keys can be retrieved using InputEvent#getModifiersand InputEvent#getModifiersEx. The button mask returned by InputEvent#getModifiers reflects only the button that changed state, not the current state of all buttons. (Note: Due to overlap in the values of ALT_MASK/BUTTON2_MASK and META_MASK/BUTTON3_MASK, this is not always true for mouse events involving modifier keys). To get the state of all buttons and modifier keys, use InputEvent#getModifiersEx. The button which has changed state is returned by MouseEvent#getButton
For example, if the first mouse button is pressed, events are sent in the following order:
id modifiers button
MOUSE_PRESSED: BUTTON1_MASK BUTTON1
MOUSE_RELEASED: BUTTON1_MASK BUTTON1
MOUSE_CLICKED: BUTTON1_MASK BUTTON1
When multiple mouse buttons are pressed, each press, release, and click results in a separate event.
For example, if the user presses button 1 followed by button 2, and then releases them in the same order, the following sequence of events is generated:
id modifiers button
MOUSE_PRESSED: BUTTON1_MASK BUTTON1
MOUSE_PRESSED: BUTTON2_MASK BUTTON2
MOUSE_RELEASED: BUTTON1_MASK BUTTON1
MOUSE_CLICKED: BUTTON1_MASK BUTTON1
MOUSE_RELEASED: BUTTON2_MASK BUTTON2
MOUSE_CLICKED: BUTTON2_MASK BUTTON2
If button 2 is released first, the MOUSE_RELEASED
/MOUSE_CLICKED
pair for BUTTON2_MASK
arrives first, followed by the pair for BUTTON1_MASK
.
MOUSE_DRAGGED
events are delivered to the Component
in which the mouse button was pressed until the mouse button is released (regardless of whether the mouse position is within the bounds of the Component
). Due to platform-dependent Drag&Drop implementations, MOUSE_DRAGGED
events may not be delivered during a native Drag&Drop operation. In a multi-screen environment mouse drag events are delivered to the Component
even if the mouse position is outside the bounds of the GraphicsConfiguration
associated with that Component
. However, the reported position for mouse drag events in this case may differ from the actual mouse position:
GraphicsConfiguration
associated with the Component
.Component
.[*Implementations of this Profile exhibit certain restrictions with respect to mouse events, specifically:
Regardless of this restriction, applications may still create MouseEvent objects and post them to the system event queue.
For more information, see*](https://www.tabnine.com/code/java/classes/java.awt.event.MouseEvent#)*Profile-specific properties.
Note: These restrictions and their corresponding properties apply only to the MouseEvent
class, and not to its subclass, MouseWheelEvent.*
[中]指示组件中发生鼠标操作的事件。当且仅当鼠标光标位于特定组件边界的未遮挡部分时,鼠标动作才会被视为发生在该组件中。组件边界可以被可见组件的子级、菜单或顶级窗口遮挡。此事件用于鼠标事件(单击、输入、退出)和鼠标运动事件(移动和拖动)。
此低级事件由以下组件对象生成:
*鼠标事件
*按下鼠标按钮
*松开鼠标按钮
*单击鼠标按钮(按下并释放)
*鼠标光标进入零部件几何体的未遮挡部分
*鼠标光标将退出零部件几何体中未被遮挡的部分
*鼠标运动事件
*鼠标被移动了
*老鼠被拖走了MouseEvent
对象被传递给每个MouseListener
或MouseAdapter
对象,这些对象被注册以使用组件的addMouseListener
方法接收“有趣的”鼠标事件。(MouseAdapter
对象实现MouseListener
接口。)每个这样的侦听器对象都会获得一个包含鼠标事件的MouseEvent
。MouseEvent
对象也会传递给每个MouseMotionListener
或MouseMotionAdapter
对象,这些对象已注册以使用组件的addMouseMotionListener
方法接收鼠标运动事件。(MouseMotionAdapter
对象实现MouseMotionListener
接口。)每个这样的侦听器对象都会获得一个MouseEvent
,其中包含鼠标运动事件。
单击鼠标按钮时,会生成事件并将其发送到注册的MouseListener
s。可以使用InputEvent#GetModifiers和InputEvent#getModifiersEx检索模式键的状态。InputEvent#getModifiers返回的按钮掩码仅反映更改状态的按钮,而不是所有按钮的当前状态。(注意:由于ALT_MASK/BUTTON2_MASK和META_MASK/BUTTON3_MASK的值重叠,涉及修改键的鼠标事件并不总是如此)。要获取所有按钮和修改器键的状态,请使用InputEvent#getModifiersEx。已更改状态的按钮由MouseeEvent#getButton返回
例如,如果按下第一个鼠标按钮,事件将按以下顺序发送:
id modifiers button
MOUSE_PRESSED: BUTTON1_MASK BUTTON1
MOUSE_RELEASED: BUTTON1_MASK BUTTON1
MOUSE_CLICKED: BUTTON1_MASK BUTTON1
当按下多个鼠标按钮时,每次按下、释放和单击都会产生一个单独的事件。
例如,如果用户先按按钮1,再按按钮2,然后按相同的顺序释放,则会生成以下事件序列:
id modifiers button
MOUSE_PRESSED: BUTTON1_MASK BUTTON1
MOUSE_PRESSED: BUTTON2_MASK BUTTON2
MOUSE_RELEASED: BUTTON1_MASK BUTTON1
MOUSE_CLICKED: BUTTON1_MASK BUTTON1
MOUSE_RELEASED: BUTTON2_MASK BUTTON2
MOUSE_CLICKED: BUTTON2_MASK BUTTON2
如果首先释放按钮2,则BUTTON2_MASK
的MOUSE_RELEASED
/MOUSE_CLICKED
对首先到达,然后是BUTTON1_MASK
对。MOUSE_DRAGGED
事件被传递到Component
,在Component
中,鼠标按钮被按下,直到松开鼠标按钮为止(无论鼠标位置是否在Component
的范围内)。由于依赖于平台的拖放实现,MOUSE_DRAGGED
事件可能不会在本机拖放操作期间传递。在多屏幕环境中,即使鼠标位置超出与Component
关联的GraphicsConfiguration
的边界,鼠标拖动事件也会传递到Component
。但是,在这种情况下,鼠标拖动事件的报告位置可能与实际鼠标位置不同:
*在没有虚拟设备的多屏幕环境中:
报告的鼠标拖动事件坐标将被剪裁,以适合与Component
关联的GraphicsConfiguration
的边界。
*在具有虚拟设备的多屏幕环境中:
鼠标拖动事件的报告坐标将被剪裁,以适合与Component
关联的虚拟设备的边界。
[*Implementations of this Profile exhibit certain restrictions with respect to mouse events, specifically:
Regardless of this restriction, applications may still create MouseEvent objects and post them to the system event queue.
For more information, see*](https://www.tabnine.com/code/java/classes/java.awt.event.MouseEvent#)Profile-specific properties.
注意:这些限制及其相应属性仅适用于MouseEvent
类,而不适用于其子类MouseWheelEvent
代码示例来源:origin: stanfordnlp/CoreNLP
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger())
treePopup.show(e.getComponent(), e.getX(), e.getY());
}
代码示例来源:origin: runelite/runelite
private MouseEvent translateEvent(MouseEvent e)
{
Dimension stretchedDimensions = client.getStretchedDimensions();
Dimension realDimensions = client.getRealDimensions();
int newX = (int) (e.getX() / (stretchedDimensions.width / realDimensions.getWidth()));
int newY = (int) (e.getY() / (stretchedDimensions.height / realDimensions.getHeight()));
return new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), e.getModifiersEx(),
newX, newY, e.getClickCount(), e.isPopupTrigger(), e.getButton());
}
}
代码示例来源:origin: opentripplanner/OpenTripPlanner
@Override
public void mouseClicked(MouseEvent e) {
if( SwingUtilities.isRightMouseButton(e) ){
@SuppressWarnings("unchecked")
JList<PathPrinter> list = (JList<PathPrinter>)e.getSource();
int row = list.locationToIndex(e.getPoint());
list.setSelectedIndex(row);
popup.show(list, e.getX(), e.getY());
}
}
代码示例来源:origin: skylot/jadx
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == 3 && e.getSource() == textComponent) {
process(e);
}
}
});
代码示例来源:origin: libgdx/libgdx
public void mouseMoved (MouseEvent event) {
int mouseX = event.getX();
int mouseY = event.getY();
}
});
代码示例来源:origin: libgdx/libgdx
public void mousePressed (MouseEvent event) {
movingIndex = overIndex;
moveAll = event.isControlDown();
if (moveAll) {
moveAllProportionally = event.isShiftDown();
moveAllPrevY = event.getY();
}
}
代码示例来源:origin: stackoverflow.com
JFrame f = new JFrame("AnimationTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
timer.start();
this.setOpaque(false);
this.setPreferredSize(new Dimension(WIDE, HIGH));
this.addMouseListener(new MouseHandler());
this.addComponentListener(new ComponentHandler());
this.repaint();
JTextField field = new JTextField("test");
Dimension d = field.getPreferredSize();
field.setBounds(e.getX(), e.getY(), d.width, d.height);
add(field);
代码示例来源:origin: stackoverflow.com
@Override
public void mouseMoved(MouseEvent e) {
Point point = e.getPoint();
int width = getWidth();
int height = getHeight();
if (e.getX() >= xOffset && e.getY() >= yOffset) {
int column = (e.getX() - xOffset) / cellWidth;
int row = (e.getY() - yOffset) / cellHeight;
repaint();
addMouseMotionListener(mouseHandler);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int width = getWidth();
int height = getHeight();
g2d.setColor(Color.BLUE);
g2d.fill(cell);
代码示例来源:origin: stackoverflow.com
addMouseListener(mouseAdapter);
addMouseMotionListener(mouseAdapter);
Floor2 mainPanel = new Floor2(w, h, gridDiv);
g2.setColor(LINE_COLOR);
for (List<Ellipse2D> ellipses : ellipseList) {
Point2D p2d1 = new Point2D.Double(ellipses.get(0).getCenterX(), ellipses.get(0).getCenterY());
g.drawImage(bufImage, 0, 0, this);
g2.setStroke(initStroke);
for (int i = 1; i < grid; i++) {
int x = i * divisionSize;
g2.drawLine(x, 0, x, getSize().height);
if (e.getButton() != MouseEvent.BUTTON1) {
return;
p1 = e.getPoint();
return;
drawingLine = new Line2D.Double(p1, e.getPoint());
代码示例来源:origin: stackoverflow.com
setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
setPreferredSize(new Dimension(500, 500));
final JFrame f = new JFrame("Test");
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
drag = true;
dragLocation = e.getPoint();
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (drag) {
if (dragLocation.getX()> getWidth()-10 && dragLocation.getY()>getHeight()-10) {
System.err.println("in");
setSize((int)(getWidth()+(e.getPoint().getX()-dragLocation.getX())),
(int)(getHeight()+(e.getPoint().getY()-dragLocation.getY())));
dragLocation = e.getPoint();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(this,BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
g.setColor(Color.red);
g.fillRect(0, 0, getWidth(), getHeight());
代码示例来源:origin: stackoverflow.com
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
int tabNumber = getUI().tabForCoordinate(DraggableTabbedPane.this, e.getX(), e.getY());
Image totalImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics totalGraphics = totalImage.getGraphics();
totalGraphics.setClip(bounds);
setDoubleBuffered(false);
paintComponent(totalGraphics);
graphics.drawImage(totalImage, 0, 0, bounds.width, bounds.height, bounds.x, bounds.y, bounds.x + bounds.width, bounds.y+bounds.height, DraggableTabbedPane.this);
repaint();
currentMouseLocation = e.getPoint();
repaint();
int tabNumber = getUI().tabForCoordinate(DraggableTabbedPane.this, e.getX(), 10);
g.drawImage(tabImage, currentMouseLocation.x, currentMouseLocation.y, this);
tabs.addTab("Four", new JButton("Four"));
test.add(tabs);
test.setVisible(true);
代码示例来源:origin: stackoverflow.com
Graphics2D g2d = bImage.createGraphics();
g2d.setBackground(Color.white);
g2d.clearRect(0, 0, BI_WIDTH, BI_HEIGHT);
g2d.dispose();
imageLabel.addMouseListener(myMouseAdapter);
imageLabel.addMouseMotionListener(myMouseAdapter);
imageLabel.setBorder(BorderFactory.createEtchedBorder());
Graphics2D g2 = bImage.createGraphics();
g2.setBackground(Color.white);
g2.clearRect(0, 0, BI_WIDTH, BI_HEIGHT);
g2.dispose();
imageLabel.repaint();
btnPanel.add(saveImageBtn);
btnPanel.add(clearImageBtn);
setLayout(new BorderLayout());
add(imageLabel, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
pointList.add(e.getPoint());
imageLabel.repaint();
pointList.add(e.getPoint());
imageLabel.repaint();
代码示例来源:origin: stackoverflow.com
private JFrame frame = new JFrame();
setBackground(Color.gray);
label.setOpaque(true);
label.setBackground(Color.RED);
label.setForeground(Color.WHITE);
setBackground(Color.black);
setLayout(new FlowLayout(FlowLayout.RIGHT));
add(label);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
pX = me.getX();
pY = me.getY();
frame.setLocation(frame.getLocation().x + me.getX() - pX,
frame.getLocation().y + me.getY() - pY);
public void mouseDragged(MouseEvent me) {
frame.setLocation(frame.getLocation().x + me.getX() - pX,
frame.getLocation().y + me.getY() - pY);
代码示例来源:origin: stackoverflow.com
redLabel.setOpaque(true);
redLabel.setBackground(Color.red.brighter().brighter());
redLabel.setPreferredSize(LABEL_SIZE);
panelGrid[4][3].add(redLabel);
panelGrid[1][1].add(blueLabel);
addMouseListener(myMouseAdapter);
clickedPanel = (JPanel) backingPanel.getComponentAt(me.getPoint());
int x = me.getPoint().x - dragLabelWidthDiv2;
int y = me.getPoint().y - dragLabelHeightDiv2;
return;
int x = me.getPoint().x - dragLabelWidthDiv2;
int y = me.getPoint().y - dragLabelHeightDiv2;
JPanel droppedPanel = (JPanel) backingPanel.getComponentAt(me.getPoint());
if (droppedPanel == null) {
JFrame frame = new JFrame("DragLabelOnLayeredPane");
frame.getContentPane().add(new DragLabelOnLayeredPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
代码示例来源:origin: stackoverflow.com
this.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 32));
this.addMouseListener(new MouseAdapter() {
mousePt = e.getPoint();
repaint();
this.addMouseMotionListener(new MouseMotionAdapter() {
int dx = e.getX() - mousePt.x;
int dy = e.getY() - mousePt.y;
textPt.setLocation(textPt.x + dx, textPt.y + dy);
mousePt = e.getPoint();
repaint();
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w2 = g.getFontMetrics().stringWidth(TITLE) / 2;
g.drawString(TITLE, textPt.x - w2, textPt.y);
JFrame f = new JFrame(TITLE);
f.add(new MouseDragTest());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
代码示例来源:origin: stackoverflow.com
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
int x2 = p2.x;
int y2 = p2.y;
g.drawLine(x1, y1, x2, y2);
p1 = p2;
g.setColor(CURRENT_POINTS_COLOR);
if (currentPointList != null && currentPointList.size() > 1) {
Point p1 = currentPointList.get(0);
public void mousePressed(MouseEvent mEvt) {
currentPointList = new ArrayList<Point>();
currentPointList.add(mEvt.getPoint());
repaint();
currentPointList.add(mEvt.getPoint());
repaint();
currentPointList.add(mEvt.getPoint());
pointsList.add(currentPointList);
currentPointList = null;
代码示例来源:origin: stackoverflow.com
final JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(600, 400));
final JToolBar toolBar = new JToolBar();
button.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
popup.show(e.getComponent(), e.getX(), e.getY());
toolBar.add(button);
frame.getContentPane().add(toolBar, BorderLayout.NORTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
代码示例来源:origin: stackoverflow.com
table.addMouseListener( new MouseAdapter()
if (e.isPopupTrigger())
int row = source.rowAtPoint( e.getPoint() );
int column = source.columnAtPoint( e.getPoint() );
popup.show(e.getComponent(), e.getX(), e.getY());
table.setPreferredScrollableViewportSize(table.getPreferredSize());
getContentPane().add( new JScrollPane(table) );
JPopupMenu popup = (JPopupMenu)c.getParent();
JTable table = (JTable)popup.getInvoker();
System.out.println(table.getSelectedRow() + " : " + table.getSelectedColumn());
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
代码示例来源:origin: stackoverflow.com
this.setPreferredSize(new Dimension(640, 480));
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
g2d.setStroke(new BasicStroke(8,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
g.drawLine(p1.x, p1.y, p2.x, p2.y);
public void mousePressed(MouseEvent e) {
drawing = true;
p1 = e.getPoint();
p2 = p1;
repaint();
public void mouseReleased(MouseEvent e) {
drawing = false;
p2 = e.getPoint();
public void mouseDragged(MouseEvent e) {
if (drawing) {
p2 = e.getPoint();
this.add(new MoveButton("\u2190", KeyEvent.VK_LEFT, -DELTA, 0));
this.add(new MoveButton("\u2191", KeyEvent.VK_UP, 0, -DELTA));
this.add(new MoveButton("\u2192", KeyEvent.VK_RIGHT, DELTA, 0));
JFrame f = new JFrame("LinePanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
代码示例来源:origin: stackoverflow.com
private JFrame f = new JFrame("FullScreenTest");
private Action exit = new AbstractAction(EXIT) {
f.dispatchEvent(new WindowEvent(
f, WindowEvent.WINDOW_CLOSING));
this.add(b);
f.getRootPane().setDefaultButton(b);
this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), EXIT);
this.getActionMap().put(EXIT, exit);
this.addMouseMotionListener(new MouseAdapter() {
FullScreenTest.this.setToolTipText(
"("+ e.getX() + "," + e.getY() + ")");
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice dev = env.getDefaultScreenDevice();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBackground(Color.darkGray);
f.setResizable(false);
f.setUndecorated(true);
f.add(this);
f.pack();
dev.setFullScreenWindow(f);
内容来源于网络,如有侵权,请联系作者删除!