我编写一个小程序,小程序窗口显示 "Java程序设计 "想当键盘按下方向键时,显示字符串想应的动.但是我写的小程序编译成功,却不会响应.以下是我的代码:
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.applet.*;
public class Respond extends Applet implements KeyListener{
int x,y;
public void init(){
x=50;
y=50;
addKeyListener(this);
}
public void keyTyped(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_DOWN: y+= 10; break;
case KeyEvent.VK_UP: y-= 10; break;
case KeyEvent.VK_LEFT: x-= 10; break;
case KeyEvent.VK_RIGHT: x+= 10; break;
}
repaint();
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void paint(Graphics g){
g.drawString( "Java程序设计 "+ "( "+x+ ", "+y+ ") ",x,y);
}
}
请问哪里出错了
------解决方案--------------------
方向键是个动作键不调用keyTyped.用 keyPressed 或keyReleased就可以了吧.