21) Design a screen in Java to handle the mouse events and display the positions of the mouse click in a text box.// Demonstrate the mouse event handlers.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code=”MouseEvents1? width=300 height=100>
</applet>
*/
public class MouseEvents1 extends Applet implements MouseListener, MouseMotionListener
{
String msg = “”;
TextField text=new TextField(20);
public void init()
{
setLayout(new FlowLayout(FlowLayout.RIGHT));
add(text);
addMouseListener(this);
addMouseMotionListener(this);
}
// Handle mouse clicked.
public void mouseClicked(MouseEvent me)
{
text.setText(“Mouse Click at ” + me.getX() + “, ” +me.getY());
repaint();
}
// Handle mouse entered.
public void mouseEntered(MouseEvent me)
{
msg = “Mouse entered.”;
repaint();
}
// Handle mouse exited.
public void mouseExited(MouseEvent me) { }
// Handle button pressed.
public void mousePressed(MouseEvent me) { }
// Handle button released.
public void mouseReleased(MouseEvent me) { }
// Handle mouse dragged.
public void mouseDragged(MouseEvent me) { }
// Handle mouse moved.
public void mouseMoved(MouseEvent me)
{
msg=”Moving mouse at ” + me.getX() + “, ” +me.getY();
repaint();
}
// Display msg in applet window
public void paint(Graphics g)
{
g.drawString(msg,10,10);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s