英文:
In this method [e.getButton] where this object e came from?
问题
I know that in order to use objects or instances you have to initialize it first, e.g. A a = new A ()
, then why does that object e
in the line e.getButton()
work without initializing it?
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1)
System.out.println("Left button clicked!");
else if (e.getButton() == MouseEvent.BUTTON3)
System.println("Right button clicked!");
}
I know e
is a parameter of the mouseClicked
method, but to use it must initialize it first, and in the code above, it is not initialized. Why?
英文:
I know that in order to use objects or instances you have to initialize it first, e.g. A a = new A ()
, then why does that object e
in the line e.getButton()
work without initializing it?
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1)
System.out.println("Left button clicked!");
else if (e.getButton() == MouseEvent.BUTTON3)
System.out.println("Right button clicked!");
}
I know e
is parameter of mouseClicked
method, but to use it must initialize it first, and in the code above is not initialized. Why?
答案1
得分: 1
MouseEvent
e
是该方法的参数。 调用者为该参数提供参数,因此您不负责创建实例。
您似乎正在实现一个 MouseListener
。 该监听器将向某个组件注册。 当程序执行并且用户操纵鼠标时,将触发 MouseEvent
。 框架将找到已注册以处理该事件的监听器,并将事件传递给该方法。
英文:
The MouseEvent
e
is a parameter of the method. The caller provides the argument to this parameter so you are NOT responsible for creating the instance.
You appear to be implementing a MouseListener
. That listener will be registered with some component. When the program is executed and the user manipulates their mouse, a MouseEvent
will be fired. The framework will find that your listener is registered to handle that event and will pass the event to the method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论