public void mouseClicked(MouseEvent e) { int clicks = e.getClickCount(); if (clicks == 1) { System.out.println("Single Click"); } else if (clicks == 2) { System.out.println("Double Click"); } }
public void mousePressed(MouseEvent e) { int button = e.getButton(); if (button == MouseEvent.BUTTON1) { System.out.println("Left Button Pressed"); } else if (button == MouseEvent.BUTTON2) { System.out.println("Middle Button Pressed"); } else if (button == MouseEvent.BUTTON3) { System.out.println("Right Button Pressed"); } }In this example, the mousePressed() method is called every time the user presses a button on the mouse. The getButton() method is used to retrieve the button pressed. If the left button is pressed, the output will be "Left Button Pressed", if the middle button is pressed, the output will be "Middle Button Pressed", and if the right button is pressed, the output will be "Right Button Pressed". The java.awt.event.MouseEvent is part of the Java AWT (Abstract Window Toolkit) package library.