简体   繁体   中英

Java Convert MouseEvent to ActionEvent

是否可以将MouseEvent转换为ActionEvent?

Not without losing some information. The MouseEvent contains information about the mouse location ( x, y ) and which buttons that are pressed (if any).


I would do the conversion like this:

MouseEvent me = ...;
ActionEvent ae = new ActionEvent(me.getSource(), me.getID(), me.paramString());

Sure, that's what a Button does (to my understanding). It processes a MouseEvent and creates (sends) an ActionEvent .

Action events are semantic events - like a signal, that a certain button (widget!) has been "pressed". The trigger for this action event may have been a mouse event ("left button has been pressed and released while the mouse pointer was in the rectangle defined by a AWT Button widget") or a keyboard event ("Space bar has been pressed and released while the focus was at AWT Button widget").

I guess you're not looking at a technical conversion. Practiacally, you'll have to listen to mouse events and fire new action events to your action listeners.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM