Handling a mouse event : MenuListener « Swing Event « Java Tutorial






Handling a mouse event
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);
  }
}








15.27.MenuListener
15.27.1.Using MenuListener to listen to: menu canceled, selected and deselected eventsUsing MenuListener to listen to: menu canceled, selected and deselected events
15.27.2.Handling a mouse eventHandling a mouse event