Difference between Mouse Listener and Action Listener?

前端 未结 3 1296
无人共我
无人共我 2021-02-10 02:05

Whats the difference? When would you use a mouse listener? or a action listener? Please and Thank You!

3条回答
  •  广开言路
    2021-02-10 02:52

    ActionListener Doc's

    The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.

    MouseListener Doc's

    The listener interface for receiving "interesting" mouse events (press, release, click, enter, and exit) on a component. (To track mouse moves and mouse drags, use the MouseMotionListener.)

    From the docs you can see that the usage of this interfaces is completly different. While you can use the MouseListener only in combination with gui elements, the ActionListener is also used when there is no gui, for example in combination with a timer.

提交回复
热议问题