[英]The mouse listener in Java
The MouseListener
on a JButton
is registered during the application startup then after startup any mouse clicks on the button the corresponding mouseClicked(MouseEvent e) method will be called. 在应用程序启动期间,将在JButton
上注册MouseListener
,然后在启动任何鼠标单击该按钮后,将调用相应的mouseClicked(MouseEvent e)方法。
The question is what/who is responsible to create the MouseEvent
object with all the metadata and invoke the mouseClicked
method. 问题是由谁/谁负责创建具有所有元数据的MouseEvent
对象并调用mouseClicked
方法。
Posting a SSCCE: 发布SSCCE:
public class TestMouseListener {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
createAndShowGui();
});
}
private void createAndShowGui(){
JFrame frame = new JFrame();
JButton button = new JButton();
frame.add(button);
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("The button was clicked");
System.out.println("The clicked time is":e.getWhen());
}
});
}
}
Take a look at the source for the Component
class. 看一下Component
类的源代码。 You can do this from eclipse by right-clicking your JButton
class and going to "show declaration", or you can view the src.zip that comes with your JDK. 您可以通过右键单击JButton
类并转到“显示声明”来从eclipse中进行操作,或者可以查看JDK随附的src.zip。 You might also be able to find it on grepcode . 您也许还可以在grepcode上找到它。
The Component
class has this processMouseEvent()
function: Component
类具有以下processMouseEvent()
函数:
protected void processMouseEvent(MouseEvent e) {
MouseListener listener = mouseListener;
if (listener != null) {
int id = e.getID();
switch(id) {
case MouseEvent.MOUSE_PRESSED:
listener.mousePressed(e);
break;
case MouseEvent.MOUSE_RELEASED:
listener.mouseReleased(e);
break;
case MouseEvent.MOUSE_CLICKED:
listener.mouseClicked(e);
break;
case MouseEvent.MOUSE_EXITED:
listener.mouseExited(e);
break;
case MouseEvent.MOUSE_ENTERED:
listener.mouseEntered(e);
break;
}
}
}
This is the short answer to your question. 这是对您问题的简短回答。 This function is what calls the mousePressed()
function. 此函数称为mousePressed()
函数。 But we can go deeper... 但是我们可以更深入...
The processMouseEvent()
function is called from the processEvent()
function: 从processEvent()
函数调用processMouseEvent()
processEvent()
函数:
protected void processEvent(AWTEvent e) {
if (e instanceof FocusEvent) {
processFocusEvent((FocusEvent)e);
} else if (e instanceof MouseEvent) {
switch(e.getID()) {
case MouseEvent.MOUSE_PRESSED:
case MouseEvent.MOUSE_RELEASED:
case MouseEvent.MOUSE_CLICKED:
case MouseEvent.MOUSE_ENTERED:
case MouseEvent.MOUSE_EXITED:
processMouseEvent((MouseEvent)e);
break;
case MouseEvent.MOUSE_MOVED:
case MouseEvent.MOUSE_DRAGGED:
processMouseMotionEvent((MouseEvent)e);
break;
case MouseEvent.MOUSE_WHEEL:
processMouseWheelEvent((MouseWheelEvent)e);
break;
}
} else if (e instanceof KeyEvent) {
processKeyEvent((KeyEvent)e);
} else if (e instanceof ComponentEvent) {
processComponentEvent((ComponentEvent)e);
} else if (e instanceof InputMethodEvent) {
processInputMethodEvent((InputMethodEvent)e);
} else if (e instanceof HierarchyEvent) {
switch (e.getID()) {
case HierarchyEvent.HIERARCHY_CHANGED:
processHierarchyEvent((HierarchyEvent)e);
break;
case HierarchyEvent.ANCESTOR_MOVED:
case HierarchyEvent.ANCESTOR_RESIZED:
processHierarchyBoundsEvent((HierarchyEvent)e);
break;
}
}
}
Which is called from the dispatchEventImpl()
function, which is called from the dispatchEvent()
function, which is called from a bunch of functions: 从dispatchEventImpl()
函数调用,从dispatchEvent()
函数调用,从函数调用:
dispatchEvent(AWTEvent) : void - java.awt.Component
addDelicately(Component, Container, int) : void - java.awt.Container
addImpl(Component, Object, int) : void - java.awt.Container
addNotify() : void - java.awt.Component
close() : void - javax.swing.plaf.metal.MetalTitlePane
createHierarchyEvents(int, Component, Container, long, boolean) : int - java.awt.Component (2 matches)
dispatchAndCatchException(Throwable, Component, FocusEvent) : Throwable - java.awt.KeyboardFocusManager
dispatchEventImpl(AWTEvent, Object) : void - java.awt.EventQueue
forwardEventToParent(MouseEvent) : void - com.sun.java.swing.plaf.motif.MotifDesktopIconUI.IconButton
forwardEventToParent(MouseEvent) : void - com.sun.java.swing.plaf.motif.MotifDesktopIconUI.IconLabel
forwardEventToParent(MouseEvent) : void - com.sun.java.swing.plaf.motif.MotifInternalFrameTitlePane.Title
mouseClicked(MouseEvent) : void - javax.swing.plaf.basic.BasicTreeUI.MouseInputHandler
mouseDragged(MouseEvent) : void - javax.swing.plaf.basic.BasicTreeUI.MouseInputHandler
MouseInputHandler(BasicTreeUI, Component, Component, MouseEvent, Component) - javax.swing.plaf.basic.BasicTreeUI.MouseInputHandler
mouseReleased(MouseEvent) : void - javax.swing.plaf.basic.BasicTreeUI.MouseInputHandler
postClosingEvent(JInternalFrame) : void - javax.swing.plaf.basic.BasicInternalFrameTitlePane
redispatchEvent(Component, AWTEvent) : void - java.awt.KeyboardFocusManager
remove(int) : void - java.awt.Container
removeAll() : void - java.awt.Container
removeDelicately(Component, Container, int) : boolean - java.awt.Container
removeNotify() : void - java.awt.Component
repostEvent(MouseEvent) : boolean - javax.swing.plaf.basic.BasicTableUI.Handler
retargetMouseEvent(Component, int, MouseEvent) : void - java.awt.LightweightDispatcher (2 matches)
As for what creates the MouseEvent
, here are some of the places that call new MouseEvent()
: 至于创建MouseEvent
,以下是一些调用new MouseEvent()
的地方:
MouseEvent(Component, int, long, int, int, int, int, int, int, boolean, int) - java.awt.event.MouseEvent
actionPerformed(ActionEvent) : void - javax.swing.Autoscroller
convertMouseEvent(Component, MouseEvent, Component) : MouseEvent - javax.swing.SwingUtilities
convertMouseEvent(MouseEvent) : MouseEvent - javax.swing.plaf.basic.BasicComboPopup
eventDispatched(AWTEvent) : void - java.awt.LightweightDispatcher
forwardEventToParent(MouseEvent) : void - com.sun.java.swing.plaf.motif.MotifDesktopIconUI.IconButton
forwardEventToParent(MouseEvent) : void - com.sun.java.swing.plaf.motif.MotifDesktopIconUI.IconLabel
forwardEventToParent(MouseEvent) : void - com.sun.java.swing.plaf.motif.MotifInternalFrameTitlePane.Title
getToolTipText(MouseEvent) : String - javax.swing.JList
getToolTipText(MouseEvent) : String - javax.swing.JTable
getToolTipText(MouseEvent) : String - javax.swing.JTree
getToolTipText(MouseEvent) : String - javax.swing.table.JTableHeader
MenuDragMouseEvent(Component, int, long, int, int, int, int, int, int, boolean, MenuElement[], MenuSelectionManager) - javax.swing.event.MenuDragMouseEvent
MouseEvent(Component, int, long, int, int, int, int, boolean, int) - java.awt.event.MouseEvent
MouseWheelEvent(Component, int, long, int, int, int, int, int, int, boolean, int, int, int, double) - java.awt.event.MouseWheelEvent
processMouseEvent(MouseEvent) : void - javax.swing.MenuSelectionManager (3 matches)
processMouseEvent(MouseEvent) : void - javax.swing.plaf.basic.new JList() {...}
retargetMouseEvent(Component, int, MouseEvent) : void - java.awt.LightweightDispatcher
start(JComponent, MouseEvent) : void - javax.swing.Autoscroller
You can keep digging deeper, but at some point you reach native code where the OS sends the underlying events to Java. 您可以继续进行更深入的研究,但是在某些时候,您会到达本机代码,操作系统会将底层事件发送给Java。
But mostly you don't ever have to care about any of this stuff. 但是,大多数情况下,您无需关心任何这些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.