问题
How can I generate event MouseWheelEvent
for some Object
?
回答1:
MouseWheelEvent mwe = new MouseWheelEvent(...);
component.dispatchEvent( mwe );
回答2:
You can use Robot class.Take a look at Mouse Wheel method
回答3:
Well, I don't know about generating a scroll Event
from an object, but you should take a look at the Robot class. It allows you to actually cause the mouse wheel to scroll. That may be helpful.
Specifically, look at the mouseWheel(int) method.
回答4:
Do you want to actually fire a MouseWheelEvent, or do you want to do something when a mouse wheel is scrolled?
If it is the former you can create a new instance using the constructor:
new MouseWheelEvent(Component source, int id, long when, int modifiers, int x,
int y, int clickCount, boolean popupTrigger, int scrollType,
int scrollAmount, int wheelRotation)
and pass the instance to the component's processMouseWheelEvent method to fire it.
If it's the latter, you can either implement a MouseWheelListener and add it to your component using the addMouseWheelListener() method of Component.
来源:https://stackoverflow.com/questions/2900400/generate-event-swing