Mar 12 Tue. Class Notes
Objectives
KeyListener
MouseListener
MouseMotionListener
Review Questions
Practice with KeyListener
---------------------------------------------------------
Structure of KeyEvent and MouseEvent
Object
EventObject
AWTEvent
ComponentEvent
InputEvent
KeyEvent MouseEvent
---------------------------------------------------------
KeyListener interface:
public void keyPressed(KeyEvent e );
public void keyTyped(KeyEvent e);
public void keyReleased(KeyEvent e);
---------------------------------------------------------
KeyAdapter class
e.g.
addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
//deal with sth
}
});
---------------------------------------------------------
public void keyTyped(KeyEvent e) {}
* Called just after the user types a Unicode character into the
listened-to component.
* Platform independent
* Any printable characters.
---------------------------------------------------------
public void keyPressed(KeyEvent e) {}
* Called just after the user presses a key while the listened-to
component has the focus.
* Platform dependent
* Any key, printable or unprintable
---------------------------------------------------------
public void keyReleased(KeyEvent e) {}
* Called just after the user releases a key while the listened-to
component has the focus.
* Platform dependent
* Any key, printable or nonprintable
----------------------------------------------------------
KeyEvent
char getKeyChar() //the return type is char p529
void setKeyChar(char c)
Get or set the Unicode character associated with this event.
int getKeyCode()//virtual key codes
void setKeyCode(int)
Get or set the key code associated with this event.
Virtual key codes are used to report which keyboard key has been
pressed, rather than a character generated by the combination of
one or more keystrokes (such as "A", which comes from shift and "a").
The KeyEvent class defines many key code constants for
commonly seen keys.
VK_A ==> A,
VK_ESCAPE ==> ESCAPE
etc.
void setModifiers(int)
Sets the state of the modifier keys for this event.
InputEvent getModifiers()
Gets the state of the modifier keys
String getKeyText()
String getKeyModifiersText()
Return text descriptions of the event's key code and modifier keys
-----------------------------------------------------------
MouseListener interface
public void mousePressed(MouseEvent e);
public void mouseReleased(MouseEvent e);
public void mouseClicked(MouseEvent e);
public void mouseEntered(MouseEvent e);
public void mouseExited(MouseEvent e);
-----------------------------------------------------------
MouseAdapter class
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
//code
}
//another methods
});
-----------------------------------------------------------
public void mouseClicked(MouseEvent e) {}
Invoked when the mouse button has been clicked
(pressed and released) on a component.
public void mousePressed(MouseEvent e) {}
Invoked when a mouse button has been pressed on a component.
public void mouseReleased(MouseEvent e) {}
Invoked when a mouse button has been released on a component.
public void mouseEntered(MouseEvent e) {}
Invoked when the mouse enters a component.
public void mouseExited(MouseEvent e) {}
Invoked when the mouse exits a component.
-----------------------------------------------------------
MouseMotionListener interface
public void mouseDragged(MouseEvent e);
public void mouseMoved(MouseEvent e);
MouseMotionAdapter class is available.
addMouseMotionListener(MouseMotionListener ml);
-----------------------------------------------------------
public void mouseDragged(MouseEvent e){}
Invoked when a mouse button is pressed on a component and
then dragged.
Platform-dependent
public void mouseMoved(MouseEvent e) {}
Invoked when the mouse button has been moved on a component
(with no buttons down).
--------------------------------------------------------
Questions on p537
1. c 5. b 8. a 11. b 14. d
2. a 6. c 9. b 12. d 15. a
3. d 7. d 10. b 13. a 16. d
4. a
--------------------------------------------------------
Practice with KeyListener
KeyEventDemo.java
Lab goals inside the code.
---------------------------------------------------------