Monday 26 March 2012

Handling Mouse and Keyboard Events

Handling Mouse Events:

•To handle mouse events, we must implement one of the appropriate interfaces as follows:
1) MouseListener
2) MouseMotionListener
3) MouseWheelListener
•If any one of the above interfaces is implemented, we must provide implementations for all the methods available in that interface.
•Methods available in “MouseListener” interface are:
1) mouseClicked()
2) mousePressed()
3) mouseReleased()
4) mouseEntered()
5) mouseExited()

•Methods available in “MouseMotionListener” interface are:
1) mouseMoved()
2) mouseDragged()
•Methods available in “MouseWheelListener” are:
1) mouseWheelMoved()
•We can get the x-coordinate and y-coordinate where the mouse is clicked by using two methods available in “MouseEvent” class. Those methods are:
1) int getX() – To get the x-coordinate
2) int getY() – To get the y-coordinate


Handling Keyboard Events:

•To handle keyboard events, we must implement the “KeyListener” interface.
•When the “KeyListener” interface is implemented, we must provide implementations for three methods available in that interface. They are:
1) keyPressed()
2) keyReleased()
3) keyTyped()
•To get the character, when the keyTyped() event occurs, we use the method:
char getKeyChar()

No comments:

Post a Comment