Java Convert MouseEvent to ActionEvent
is it possible to conve开发者_开发百科rt a MouseEvent to an 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.
精彩评论