[e.getButton] 这个方法中,这个对象 e 是从哪里来的?

huangapple go评论69阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年4月11日 15:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定