OPTION
1: Implementing a Listner Interface
One option is to create a class, e.g. myMouseListner
(or use an existing class you have previously written),
and have it implement the appropriate Listner Interface(e.g.
MouseListner) to handle the events you are interested
in. Then this class's (e.g. myMouseListener)
methods which you must implement are used to handle the
events of interest.
- You must implement all of the methods in the interface,
even as dummy methods (that do nothing).
Example Inside your Applet class having
it implement a Listner Interface. Here we make
the applet itself a listener!
import java.awt.event.*; //need this package
import java.applet.*;
//Can only have one parent...thus must implement the
//Event interfaces you are wanting to handle.
//Thus you must create methods (even if they do nothing)
// for all of the defined methods in the interface.
class MyApplet extends Applet implements MouseListener {
public void mousePressed(MouseEvent e) {
//handle it here
}
//implement ALL the other mouse*() methods below
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
|
|
OPTION 2: Extending
a Listner Adapter
Alternatively, you can create a class (or use an existing
class) and have it extend the appropriate adaptor class
(e.g. MouseAdaptor).
- This way you do not have to implement any methods
you do not care about...you simply inherit the default
ones from the Adapter class.
Example You Own Listener Class extending
an Adapter Class.
import java.awt.event.*; //need this package
//will ONLY handle the mouse down event and
// do nothing for others
class MyMouseListner extends MouseAdapter {
public void mousePressed(MouseEvent e) {
//handle it here
}
}
|
|