0
我正在創建一個簡單的突破遊戲。但是,KeyAdapter沒有收到輸入。代碼對我來說看起來很好,但也許我錯過了更基本的東西?KeyAdapter沒有響應〜Java
public DatGamePanel(BustOut bo, long framerate) {
setBackground(Color.black);
setPreferredSize(new Dimension(GAME_WIDTH,GAME_HEIGHT));
setFocusable(true);
font = new Font("Sans Serif", Font.BOLD, 24);
fm = this.getFontMetrics(font);
this.bo = bo;
period = 1000/framerate;
bat = new Bat("bat.png",GAME_WIDTH,GAME_HEIGHT-32,2);
//Get keyboard input :D
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {
handleInputPressed(ke);
}
public void keyReleased(KeyEvent ke) {
handleInputReleased(ke);
}
});
}
public void handleInputPressed(KeyEvent ke) {
int a = ke.getKeyCode();
switch(a) {
case KeyEvent.VK_LEFT:
bat.keyHandle(0);
test = 1;
break;
case KeyEvent.VK_RIGHT:
bat.keyHandle(2);
break;
}
}
public void handleInputReleased(KeyEvent ke) {
System.out.println("Key Pressed");
int a = ke.getKeyCode();
switch(a) {
case KeyEvent.VK_LEFT:
bat.keyHandle(1);
test = 0;
break;
case KeyEvent.VK_RIGHT:
bat.keyHandle(3);
break;
}
}
這些都是基本的輸入句柄。當我按下左箭頭時,測試變量不會改變。這裏有什麼錯...
「確保組件的isFocusable()方法返回true。」 http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html – trashgod 2011-02-19 03:35:18