import java.awt.event.*;
import javax.swing.JFrame;
public class TestKey {
public static void main(String[]args){
new TestKey().launchFrame();
}
void launchFrame(){
addKeyListener(new Monitor());
setBounds(300,300,300,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class Monitor extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keycode=e.getKeyCode();
if(keycode==KeyEvent.VK_0 ){
System.out.println("nihao");
}
}
}
}
------最佳解决方案--------------------------------------------------------
没有extends JFrame
public class TestKey extends JFrame
------其他解决方案--------------------------------------------------------
楼上正解。。。。。