[post-views]
To handle mouse events, you must implement the MouseListener and the MouseMotionListener interfaces.
MouseEvent
is a class that encapsulates information about mouse events, such as mouse clicks, movements, and button actions.
Method | Description |
---|---|
int getX() |
Returns the x-coordinate of the mouse event relative to the source component. |
int getY() |
Returns the y-coordinate of the mouse event relative to the source component. |
Point getPoint() |
Returns the Point object representing the x and y coordinates of the mouse event. |
int getButton() |
Returns which, if any, of the mouse buttons has changed state. |
int getClickCount() |
Returns the number of mouse clicks associated with the event. |
MouseListener
is an interface that listens for mouse events. It has five methods that must be implemented.
Method | Description |
---|---|
void mouseClicked(MouseEvent e) |
Invoked when the mouse button has been clicked (pressed and released) on a component. |
void mousePressed(MouseEvent e) |
Invoked when a mouse button has been pressed on a component. |
void mouseReleased(MouseEvent e) |
Invoked when a mouse button has been released on a component. |
void mouseEntered(MouseEvent e) |
Invoked when the mouse enters a component. |
void mouseExited(MouseEvent e) |
Invoked when the mouse exits a component. |
package mouseeventhandling;
import javax.swing.*;
import java.awt.event.*;
public class MouseListenerExample extends JFrame implements MouseListener {
private JLabel label;
public MouseListenerExample() {
// Set up the frame
setTitle("MouseListener Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
// Set up the label
label = new JLabel("Mouse events will be displayed here");
label.setBounds(50, 100, 300, 50);
add(label);
// Add MouseListener to the frame
addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
label.setText("Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")");
}
@Override
public void mousePressed(MouseEvent e) {
label.setText("Mouse Pressed at (" + e.getX() + ", " + e.getY() + ")");
}
@Override
public void mouseReleased(MouseEvent e) {
label.setText("Mouse Released at (" + e.getX() + ", " + e.getY() + ")");
}
@Override
public void mouseEntered(MouseEvent e) {
label.setText("Mouse Entered at (" + e.getX() + ", " + e.getY() + ")");
}
@Override
public void mouseExited(MouseEvent e) {
label.setText("Mouse Exited at (" + e.getX() + ", " + e.getY() + ")");
}
public static void main(String[] args) {
MouseListenerExample example = new MouseListenerExample();
example.setVisible(true);
}
}
javax.swing.*
: For Swing components like JFrame and JLabel.java.awt.event.*
: For event handling, including MouseListener
and MouseEvent
.MouseListenerExample
class extends JFrame
and implements MouseListener
.addMouseListener(this)
.
Method | Description |
---|---|
void mouseDragged(MouseEvent e) |
Called when a mouse button is pressed and the mouse is dragged. |
void mouseMoved(MouseEvent e) |
Called when the mouse is moved with no buttons pressed. |
package mouseeventhandling;
import javax.swing.*;
import java.awt.event.*;
public class MouseMotionListenerExample extends JFrame
implements MouseMotionListener {
private JLabel label;
public MouseMotionListenerExample() {
// Set up the frame
setTitle("MouseMotionListener Example");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
// Set up the label
label = new JLabel("Move the mouse within the frame");
label.setBounds(50, 100, 300, 50);
add(label);
// Add MouseMotionListener to the frame
addMouseMotionListener(this);
}
@Override
public void mouseDragged(MouseEvent e) {
label.setText("Mouse Dragged to (" + e.getX() + ", " + e.getY() + ")");
}
@Override
public void mouseMoved(MouseEvent e) {
label.setText("Mouse Moved to (" + e.getX() + ", " + e.getY() + ")");
}
public static void main(String[] args) {
MouseMotionListenerExample example = new MouseMotionListenerExample();
example.setVisible(true);
}
}
Output
You must be logged in to submit a review.