Presentation is loading. Please wait.

Presentation is loading. Please wait.

MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )

Similar presentations


Presentation on theme: "MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )"— Presentation transcript:

1 MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )

2 Mouse There are two kinds of mouse related events: – you press a mouse button – you move the mouse The former is handled by a MouseListener, the latter by a MouseMotionListener. Both are interfaces.

3 Mouse MouseListener has five methods: – mouseClicked – mouseEntered – mouseExited – mousePressed – mouseReleased

4 Mouse So if you want to implement a MouseListener, you have to provide the implementation for 5 methods even if you are only interested in one method. So there a class called MouseAdapter which implements MouseListener but only provides empty methods. So if you just want to override one method, then you may use a MouseAdapter instead of MouseListener.

5 MouseAdapter, MouseListener JPanel panel=new JPanel();.... panel.addMouseListener( new MouseListener() { public void mouseClicked( MouseEvent e) { System.out.println("hello"); } public void mouseEntered(...) {} public void mouseExited(...) {} public void mousePressed(...) {} public void mouseReleased(...) {} } ); JPanel panel=new JPanel();... panel.addMouseListener( new MouseAdapter() { public void mouseClicked( MouseEvent e) { System.out.println("hello"); } );

6 Mouse Each of the 5 methods of MouseListener accepts a parameter of type MouseEvent. The followings are some of the useful methods of MouseEvent: – getX(): get the x coordinate – getY(): get the y coordinate

7 MouseMotionListener It has two methods: – mouseDragged – mouseMoved

8 MouseMotionAdaptor This is a class the implements MouseMotionListener. So if you only want to override one method of a MouseMotionListener, then you'd better used a MouseMotionAdaptor.


Download ppt "MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )"

Similar presentations


Ads by Google