Listeners
are created by using one or more of the interfaces defined by the java.awt.event package.
Example:
import
java.awt.event.*;
import
java.applet.*;
import
java.awt.*;
public
class MyEventApplet extends Applet implements MouseListener,
MouseMotionListener
{
String msg="";
int x,y;
public void init()
{
x=y=0;
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
g.drawString(msg,x,y);
}
public void mouseClicked(MouseEvent e)
{
x=e.getX();
y=e.getY();
msg="Mouse Clicked";
}
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
msg="Mouse Pressed";
}
public void mouseReleased(MouseEvent e)
{
x=e.getX();
y=e.getY();
msg="Mouse Released";
}
public void mouseMoved(MouseEvent e)
{
x=e.getX();
y=e.getY();
showStatus("Mouse Moved at
"+x+" , "+y);
repaint();
}
public void mouseDragged(MouseEvent e)
{
x=e.getX();
y=e.getY();
showStatus("Mouse Dragged at
"+x+" , "+y);
repaint();
}
public void mouseExited(MouseEvent e)
{
x=0;
y=10;
msg="Mouse exited";
repaint();
}
public void mouseEntered(MouseEvent e)
{
x=0;
y=10;
msg="Mouse exited";
repaint();
}
}
OUTPUT:
In
the above example, Listener are created for various mouse related events that
handles the appropriate event generated by the event.
No comments:
Write comments