java 键盘监听器不能捕获事件_求助。。。为什么键盘监听器没法使用啊

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

下面是有关监听器部分的代码,一个吃豆子的APPLET程序,但是按键盘就是没有反应。。。

有好心的大神能帮我看一下吗

3546c8f94fbd2f518df6f1624b444f7f.png,这算是一个作业,弄了好久了按键盘就是没有用快烦死了

public void init(){

player = new Player();

health = 100;

//初始化敌人

Red = new Enemy(210, 189, 1, false, 0);

Cyan = new Enemy(210, 231, 2, true, 0);

Pink = new Enemy(220, 231, 3, true, 66);

Orange = new Enemy(200, 231, 4, true, 132);

//添加键盘事件侦听

addKeyListener(this);

requestFocus();

t = new Ticker(30);

t.addActionListener(this);

gold = golds();

wall = walls();

WALL = new Image[47];

for(int i = 0; i < 47; i++)

WALL[i] = getImage(getDocumentBase(), "Image\\" + "Wall" + (i + 1) + ".jpg");

SOUND = new AudioClip[8];

for(int i = 0; i < 8; i++)

SOUND[i] = getAudioClip(getDocumentBase(), "Sound\\" + (i + 1) + ".au");

}

public void keyPressed(KeyEvent e){

//点击“空格”,游戏开始

if(e.getKeyCode() == KeyEvent.VK_SPACE){

SOUND[6].play();

gameStarted = true;

}

//点击方向键,执行相应的操作

if(e.getKeyCode() == KeyEvent.VK_UP){

player.ChangeDirection(0);

UP_TYPED = true;

DOWN_TYPED = false;

LEFT_TYPED = false;

RIGHT_TYPED = false;

}

if(e.getKeyCode() == KeyEvent.VK_DOWN){

player.ChangeDirection(1);

UP_TYPED = false;

DOWN_TYPED = true;

LEFT_TYPED = false;

RIGHT_TYPED = false;

}

if(e.getKeyCode() == KeyEvent.VK_LEFT ){

player.ChangeDirection(2);

UP_TYPED = false;

DOWN_TYPED = false;

LEFT_TYPED = true;

RIGHT_TYPED = false;

}

if(e.getKeyCode() == KeyEvent.VK_RIGHT){

player.ChangeDirection(3);

UP_TYPED = false;

DOWN_TYPED = false;

LEFT_TYPED = false;

RIGHT_TYPED = true;

}

}

public void keyReleased(KeyEvent e){}

public void keyTyped(KeyEvent e){}

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页