|
Here is an example of capturing mouse events! It uses a Canvas and a TextArea. The Canvas provides a general drawing area. The TextArea provides for multi-line text editing and uses scrollbars if the text exceeds the visible area. import java.awt.*; import java.awt.event.MouseListener;
public class Example3 extends
java.applet.Applet public void init() { setLayout(new BorderLayout() ); Canvas
c = new Canvas(); textArea
= new TextArea(); //Register
for mouse events on the canvas. } public void mousePressed(MouseEvent
e) { public void mouseReleased(MouseEvent
e) { public void mouseEntered(MouseEvent
e) { public void mouseExited(MouseEvent
e) { public void mouseClicked(MouseEvent
e) { void saySomething(String
eventDescription, Note: the MouseListener interface has 5 methods that all must be overriden in the implementing class. When one needs only one of the methods, the others must still must be encoded, albeit with empty code bodies. This is rather inelegant. So the Java folk created for each of those listener interfaces with more than one method a corresponding adapter class that implements empty methods for each of the interface methods. A subclass of an adapter class then only needs to override the required method. We will discuss adapter classes later in the context of inner classes.
|
||||||||||||
|
|||||||||||||