import java.awt.event.*;
import javax.swing.*;
Mouse()
{
}
// main class
{
// create a frame
// set the size of the frame
f.setSize(600, 100);
// close the frame when close button is pressed
// create anew panel
// set the layout of the panel
// initialize the labels
// create an object of mouse class
Mouse m = new Mouse();
// add mouseListener to the frame
f.addMouseListener(m);
// add labels to the panel
p.add(label1);
p.add(label2);
p.add(label3);
// add panel to the frame
f.add(p);
f.show();
}
// this function is invoked when the mouse is pressed
{
// show the point where the user pressed the mouse
label1.setText("mouse pressed at point:"
e.getX() " " e.getY());
}
// this function is invoked when the mouse is released
{
// show the point where the user released the mouse click
label1.setText("mouse released at point:"
e.getX() " " e.getY());
}
// this function is invoked when the mouse exits the component
{
// show the point through which the mouse exited the frame
label2.setText("mouse exited through point:"
e.getX() " " e.getY());
}
// this function is invoked when the mouse enters the component
{
// show the point through which the mouse entered the frame
label2.setText("mouse entered at point:"
e.getX() " " e.getY());
}
// this function is invoked when the mouse is pressed or released
{
label3.setText("mouse clicked at point:"
e.getX() " "
e.getY() "mouse clicked :" e.getClickCount());
}
}
Java program to implement mouse events.
Reviewed by
on
September 05, 2019
Rating:
No comments: