30.12.2013 Views

Chapter 6: Inheritance and Abstract Classes Chapter Topics 6.1 ...

Chapter 6: Inheritance and Abstract Classes Chapter Topics 6.1 ...

Chapter 6: Inheritance and Abstract Classes Chapter Topics 6.1 ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CS151 Object-Oriented Design<br />

Dr. Kim<br />

}<br />

public void mousePressed(MouseEvent event) {}<br />

public void mouseReleased(MouseEvent event) {}<br />

public void mouseEntered(MouseEvent event) {}<br />

public void mouseExited(MouseEvent event) {}<br />

• To define a listener, extend these adapter classes <strong>and</strong> override only methods you want.<br />

Without an adapter class, you need to supply all five methods inside a listener class.<br />

• Use an anonymous class to define the mouse listener. The following anonymous listener<br />

class extends MouseAdapter rather than implementing MouseListener.<br />

addMouseListener(new<br />

MouseAdapter()<br />

{<br />

public void mousePressed(MouseEvent event)<br />

{<br />

mouse action goes here<br />

}<br />

});<br />

The above code is equivalent to<br />

class X extends MouseAdapter<br />

{ public void mousePressed(MouseEvent event)<br />

{<br />

mouse action goes here<br />

}<br />

}<br />

addMouseListener(new X());<br />

Example: Car Mover Program<br />

11

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!