JAVA键盘事件不响应

publicclassGamePanelextendsJPanel{Rectangleboxbox1=newRectanglebox(0,0,40,40,0.5,3,1)... public class GamePanel extends JPanel{
Rectanglebox box1 = new Rectanglebox(0,0,40,40,0.5,3,1);
Rectanglebox box2 = new Rectanglebox(0,70,20,10,0.5,1,1);
Ball ball_1 = new Ball(200,200,5);
JButton jbtPlay = new JButton("Play");
JButton jbtReplay = new JButton("Replay");
Timer timer = new Timer(10,new TimerListener());
public GamePanel(){
this.setSize(500, 500);
this.setPreferredSize(getSize());

jbtPlay.setBounds(200, 200, 20, 20);
jbtReplay.setBounds(200, 200, 20, 20);
add(jbtPlay);
add(jbtReplay);

jbtReplay.setVisible(false);

addKeyListener(new KeyAdapter(){
public void KeyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_DOWN: ball_1.moveDown();repaint();break;
case KeyEvent.VK_UP: ball_1.moveUp();repaint();break;
case KeyEvent.VK_LEFT: ball_1.moveLeft();repaint();break;
case KeyEvent.VK_RIGHT: ball_1.moveRight();repaint();break;
default: break;
}

}
});

jbtPlay.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
timer.start();
jbtPlay.setVisible(false);
}
});

jbtReplay.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
timer.start();
jbtReplay.setVisible(false);
}
});
}

class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e){
box1.move();
if(crash(box1,ball_1)||crash(box2,ball_1)){
timer.stop();
jbtReplay.setVisible(true);
}
repaint();
}
}

protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.blue);
g.fillRect((int)box1.getX(), (int)box1.getY(), (int)box1.getWidth(), (int)box1.getLength());
g.fillOval((int)(ball_1.getX()-ball_1.getRadius()), (int)(ball_1.getY()-ball_1.getRadius()),(int)(2*ball_1.getRadius()), (int)(2*ball_1.getRadius()));
}

public static boolean crash(Rectanglebox box,Ball ball){
double box_x=box.getX()+0.5*box.getLength();
double box_y=box.getY()+0.5*box.getWidth();
double distance_x =Math.abs(box_x-ball.getX());
double distance_y =Math.abs(box_y-ball.getY());
if(distance_x>ball.getRadius()+0.5*box.getLength())return false;
if(distance_y>ball.getRadius()+0.5*box.getWidth())return false;
if(distance_x<ball.getRadius()+0.5*box.getLength())return true;
if(distance_y<ball.getRadius()+0.5*box.getWidth())return true;
double a =(box_x-ball.getX())*(box_x-ball.getX())+(box_y-ball.getY())*(box_y-ball.getY());
return (a<=ball.getRadius()*ball.getRadius());

}

}
加了 ,还是没用
展开
 我来答
liyi830813
2012-02-10 · TA获得超过952个赞
知道小有建树答主
回答量:1034
采纳率:0%
帮助的人:963万
展开全部
请加一句
this.setFocusable(true);
设定本焦点在窗口
====================================以下程序仅供参考,当然运行也可以
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class C extends JApplet{

public C(){
this.setFocusable(true);
this.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int keycode = e.getKeyCode();
switch (keycode) {
case KeyEvent.VK_LEFT:
System.out.println(1);
break;
case KeyEvent.VK_RIGHT:
break;
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_DOWN:
break;
default:
break;
}
}
});
}
}
====================================================
@Override public void keyPressed(KeyEvent e)
貌似你写成KeyPressed了,注意大小写
追问
加了,还是没用
追答
@Override public void keyPressed(KeyEvent e) 
貌似你写成KeyPressed了,注意大小写
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式