Advertisement
Method | Description |
---|---|
public int getX() | Returns the x-coordinate of the MouseEvent. |
public int getY() | Returns the y-coordinate of the MouseEvent. |
Method | Description |
---|---|
public void mouseEntered(MouseEvent me) | This method is called when a mouse cursor enters a window listening for MouseEvent |
public void mousePressed(MouseEvent me) | This method is called when a mouse button is being pressed. |
public void mouseClicked(MouseEvent me) | This method is called when a mouse button was clicked. |
public void mouseReleased(MouseEvent me) | This method is called when a mouse button is released. |
public void mouseExited(MouseEvent me) | This method is called when a mouse cursor exists the window. |
Method | Description |
---|---|
public void addMouseListener(ItemListener object) | where object is an object of the class that wants to listen and respond to MouseEvent and has implemented MouseListener interface. This registers the class to listen & respond to MouseEvent. |
Advertisement
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseEx1 implements MouseListener
{
Label label1, label2;
Frame frame;
String str;
MouseEx1()
{
frame = new Frame("Window");
label1= new Label("Handling mouse events in the Frame window", Label.CENTER);
label2= new Label();
frame.setLayout(new FlowLayout());
frame.add(label1);
frame.add(label2);
//Registering class MouseEx1 to catch and respond to mouse events
frame.addMouseListener(this);
frame.setSize(340,200);
frame.setVisible(true);
}
public void mouseClicked(MouseEvent we)
{
str+=" and Mouse button was clicked";
label2.setText(str);
frame.setVisible(true);
}
public void mouseEntered(MouseEvent we)
{
label2.setText("Mouse has entered the window area");
frame.setVisible(true);
}
public void mouseExited(MouseEvent we)
{
label2.setText("Mouse has exited the window area");
frame.setVisible(true);
}
public void mousePressed(MouseEvent we)
{
label2.setText("Mouse button is being pressed");
frame.setVisible(true);
}
public void mouseReleased(MouseEvent we)
{
str="Mouse button is released";
label2.setText(str);
frame.setVisible(true);
}
public static void main(String... ar)
{
new MouseEx1();
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement