77 public class mymouseadapter extends mouseadapter

This preview shows page 77 - 87 out of 105 pages.

77public class MyMouseAdapter extends MouseAdapter{public void mousePressed(MouseEvent event) {System.out.println("User pressed mouse button!");}}...// using the MyMouseAdapterMyPanel panel = new MyPanel();panel.addMouseListener(new MyMouseAdapter());MouseAdapter usage
78InputEventpublic static int BUTTON1_MASK, BUTTON2_MASK, BUTTON3_MASK,CTRL_MASK, ALT_MASK, SHIFT_MASKMouseEventpublic int getClickCount()public Point getPoint()public int getX(), getY()public Object getSource()public int getModifiers()(use the above button masks with this)SwingUtilitiesisLeftMouseButton(MouseEvent event)isRightMouseButton(MouseEvent event)MouseEvent objects
79MouseEvent: usagepublic class MyMouseAdapter extends MouseAdapter{public void mousePressed(MouseEvent event) {Point p = event.getPoint();Object source = event.getSource();if (source == this.panel && p.getX() < 10) {JOptionPane.showMessageDialog(null, "You clicked the left side of myPanel!");}}}
80package java.awt.event;public interface MouseMotionListener{public void mouseDragged(MouseEvent event);public void mouseMoved(MouseEvent event);}abstract MouseMotionAdapter class provides empty implementations of both methods if you just want to override oneMouse movement: MouseMotionListener
81public class MyMouseMotionAdapter extendsMouseMotionAdapter {public void mouseMoved(MouseEvent event) {Point p = event.getPoint();double x = event.getX();double y = event.getY();System.out.println("Mouse is at " + p);System.out.println("x is " + x);System.out.println("y is " + y);}}// using the listenermyPanel.addMouseMotionListener(new MyMouseMotionAdapter());MouseMotionAdapter example
82MouseInputListenerpackage javax.swing.event;public interface MouseInputListenerextends MouseListener, MouseMotionListener{}more importantly: MouseInputAdapter class includes empty implementations for ALL methods from both mouse input interfaces, allowing same listener to listen to mouse clicks and movement
83public class MyMouseInputAdapter extendsMouseInputAdapter {public void mousePressed(MouseEvent event) {System.out.println("Mouse was pressed");}public void mouseMoved(MouseEvent event) {Point p = event.getPoint();System.out.println("Mouse is at " + p);}}...// using the listenerMyMouseInputAdapter adapter = new MyMouseInputAdapter();myPanel.addMouseListener(adapter); myPanel.addMouseMotionListener(adapter);MouseInputAdapter: Example
84Keyboard EventsUsage of keyboard events:listen to keyboard activity within a GUI component (usually a panel)respond to keyboard activity with appropriate actionscontrol onscreen drawn characters and simulate text input
85Key Presses: KeyListenerpackage java.awt.event;public interface KeyListener{public void keyPressed(KeyEventevent);public void keyReleased(KeyEvent event);public void keyTyped(KeyEvent event);}abstract class KeyAdapter implements all KeyListener methods
86InputEventpublic static int CTRL_MASK,ALT_MASK, SHIFT_MASKKeyEventpublic static int VK_A .. VK_Z,VK_0 .. VK_9,VK_F1 .. VK_F10,VK_UP, VK_LEFT, ..,VK_TAB, VK_SPACE, VK_ENTER, ...

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture