In this Java AWT Tutorial, we will handle the AWT MouseEvents using MouseListener and MouseMotionListener. Here, we use a Panel as the originator of these Mouse Events and display a status text in a Label for the handled events.
First we handle mouse Enter, Exit, button press, button release and mouse click from MouseListener. The we handle mouse move and mouse drag.
FrameWin.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class FrameWin extends Frame implements WindowListener { public FrameWin(String FrameTitle){ super(FrameTitle); setSize(400, 300); setLocation(100,100); addWindowListener(this); //Sample 03: Create Mouse Test Panel Label StatusLabel = new Label(); MouseTestPanel panel = new MouseTestPanel(StatusLabel); panel.setBackground(Color.gray); panel.addMouseListener(panel); add(panel); add(BorderLayout.SOUTH, StatusLabel); //Sample 08: Register with Mouse Move panel.addMouseMotionListener(panel); } public void windowOpened(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} } |
MouseTestPanel.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import java.awt.Dimension; import java.awt.Label; import java.awt.Panel; import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; //Sample 01: Implement MouseListeners //Sample 06: Implement MouseMotionListener public class MouseTestPanel extends Panel implements MouseListener, MouseMotionListener { Label Status; public MouseTestPanel(Label status) { super(); this.Status = status; setPreferredSize(new Dimension(300,200)); } @Override public void mouseClicked(MouseEvent e) { //Sample 04: Check which Mouse Button Clicked String out = ""; int ButtonClickMask = e.getModifiers(); if ((ButtonClickMask & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) out += "Left Mouse Clicked "; if ((ButtonClickMask & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) out += "Middle Mouse Clicked "; if ((ButtonClickMask & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) out += "Right Mouse Clicked "; //Sample 05: Get the Location out += "@ " + e.getX() + "," + e.getY(); Status.setText(out); } @Override public void mouseEntered(MouseEvent e) { Status.setText("Mouse Entered"); } @Override public void mouseExited(MouseEvent e) { Status.setText("Mouse Left"); } @Override public void mousePressed(MouseEvent e) { Status.setText("Mouse Button Pressed"); } @Override public void mouseReleased(MouseEvent e) { Status.setText("Mouse Button Released"); } //Sample 07: Track Mouse Movement/Drag @Override public void mouseDragged(MouseEvent arg0) { Status.setText( "Mouse Draggged. (" + arg0.getX() + "," + arg0.getY() + ")"); } @Override public void mouseMoved(MouseEvent arg0) { Status.setText( "Mouse Moved. (" + arg0.getX() + "," + arg0.getY() + ")"); } } |
Categories: AWT-Tube