MouseEvent MouseListener Example Applet

MouseEvent MouseListener Example Applet

Note: Before going into this tutorial, it is advised to have better knowledge of Applet.

What mouse events and key events possible with frame are also possible with applet because frame and applet are containers (that can hold AWT components) derived from the same java.awt.Container class. Following is their hierarchy.

MouseListener Example

As usual every applet file should be accompanied by a HTML file.

Following two programs of MouseListener Example illustrate.

HTML file to run the applet

Java applet File Name: AppletMouseListener.java

No main() method and no constructor, it is being an applet.

public class AppletMouseListener extends Applet implements MouseListener

Observe, the applet implements MouseListener.

addMouseListener(this);

Applet window is added with MouseListener in init() method.

The paint() method should be called with repaint() only. Calling paint() directly raises compilation error. Why?

g.drawString(str, 75, 150);

The string str is drawn in applet window with Graphics class drawString() method at x coordinate of 75 and y coordinate of 150.


MouseListener Example
Output screen when mouse is clicked on applet window of MouseListener Example.

Leave a Reply

Your email address will not be published. Required fields are marked *