MouseClickListener.java Source code

Java tutorial

Introduction

Here is the source code for MouseClickListener.java

Source

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

class MouseClickListener extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e)) {
            System.out.print("The mouse left button was clicked");
        } else if (SwingUtilities.isRightMouseButton(e)) {
            System.out.print("The mouse right button was clicked");
        } else if (SwingUtilities.isMiddleMouseButton(e)) {
            System.out.print("The mouse middle button was clicked");
        }
        System.out.print(e.getClickCount() + "time(s)");
        int x = e.getX();
        int y = e.getY();
        System.out.println(" at (" + x + "," + y + ")");
    }
}

public class MouseListenerTest1 {

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("MouseListener Test 1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addMouseListener(new MouseClickListener());
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
}