import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class TestKey extends JFrame {
public TestKey() {
MyPanel myPanel=new MyPanel();
this.getContentPane().add(myPanel);
setSize(300, 200);
setLocation(100, 100);
setVisible( true );
}
class TestKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent event) {
System.out.println("===========================" + event.getKeyCode());
}
public void keyReleased(KeyEvent event){
System.out.println("W释放" + event.getKeyCode());
}
public void keyTyped(KeyEvent event){
if (event.getKeyCode() == KeyEvent.VK_K) {
System.out.println("按下一次 pressed and released");
}
}
}
class MyPanel extends JPanel{
public MyPanel(){
addKeyListener(new TestKeyListener());
}
}
public static void main(String args[]) {
TestKey application = new TestKey();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}