public interface KeyListener extends EventListener {
/**
* Invoked when a key has been typed.
* See the class description for {@link KeyEvent} for a definition of
* a key typed event.
*/
public void keyTyped(KeyEvent e);
/**
* Invoked when a key has been pressed.
* See the class description for {@link KeyEvent} for a definition of
* a key pressed event.
*/
public void keyPressed(KeyEvent e);
/**
* Invoked when a key has been released.
* See the class description for {@link KeyEvent} for a definition of
* a key released event.
*/
public void keyReleased(KeyEvent e);
}
public class KeyBoardListen extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private String s = null;
private JLabel label;
KeyBoardListen() {
setLayout(new FlowLayout());
label = new JLabel();
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
s = "按下的键是Type:" + e.getKeyChar();
label.setText(s);
System.out.println(s);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
s = "释放的键是:" + e.getKeyChar();
label.setText(s);
System.out.println(s);
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
s = "按下的键是Press:" + e.getKeyChar();
label.setText(s);
System.out.println(s);
}
});
add(label);
setBounds(350, 100, 600, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new KeyBoardListen();
}
}