MouseListener
上的JButton
在应用程序启动期间注册,然后在启动后任何鼠标点击按钮,将调用相应的mouseClicked(MouseEvent e)方法。
问题是什么/谁负责用所有元数据创建MouseEvent
对象并调用mouseClicked
方法。
发布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());
}
});
}
}
答案 0 :(得分:2)
查看Component
课程的来源。您可以通过右键单击JButton
类并转到" show declaration"来从eclipse执行此操作,或者您可以查看JDK附带的src.zip。您也可以在grepcode上找到它。
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;
}
}
}
这是您的问题的简短答案。此函数调用mousePressed()
函数。但我们可以更深入......
从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;
}
}
}
从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)
至于创建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
您可以继续深入挖掘,但在某些时候,您可以获得操作系统将基础事件发送到Java的本机代码。
但大部分时间你都不必关心这些事情。