Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class mouse_event extends JPanel implements MouseMotionListener, MouseListener {
- private String msg;
- private int x, y;
- public mouse_event() {
- addMouseMotionListener(this);
- addMouseListener(this);
- }
- public void mouseClicked(MouseEvent m) {
- x = 0;
- y = 40;
- msg = "Mouse Clicked";
- repaint();
- }
- public void mouseEntered(MouseEvent m) {
- x = 0;
- y = 40;
- msg = "Mouse Entered";
- repaint();
- }
- public void mouseDragged(MouseEvent m) {
- x = m.getX();
- y = m.getY();
- msg = "#";
- repaint();
- }
- public void mouseExited(MouseEvent m) {
- x = 0;
- y = 20;
- msg = "Mouse Exited";
- repaint();
- }
- public void mouseReleased(MouseEvent m) {
- x = 0;
- y = 20;
- msg = "Mouse Released";
- repaint();
- }
- public void mousePressed(MouseEvent m) {
- x = 0;
- y = 40;
- msg = "Mouse Pressed";
- repaint();
- }
- public void mouseMoved(MouseEvent m) {
- x = 0;
- y = 40;
- msg = "Mouse Moved";
- repaint();
- }
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawString(msg, x, y);
- }
- public static void main(String[] args) {
- JFrame frame = new JFrame("Mouse Event Example");
- mouse_event panel = new mouse_event();
- frame.add(panel);
- frame.setSize(400, 400);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement