我目前正在尝试在我的程序中实现一个按键监听器,以便当我按下箭头键时它会执行一个操作,我的程序中的对象要么向左要么向右移动。
这是我程序中的移动方法
public void moveDirection(KeyEvent e)
{
int move = 0;
int r = K.getRow();
int c = K.getCol();
if (e.getKeyCode() == 39) move = 1; //KeyEvent.VK_RIGHT
if (e.getKeyCode() == 37) move = 2; //KeyEvent.VK_LEFT
//if (e.getKeyCode() == KeyEvent.VK_DOWN) move = 3;
switch (move)
{
case 1: if (inBound(r, c+1))
K.setLocation(r ,c+1);
if (inBound(r, c-1) && frame2[r][c-1] == K)
frame2[K.getRow()][K.getCol()-1] = null;
break; //move right 39
case 2: K.setLocation(K.getRow(), K.getCol()-1); break; //move left 37
//case 3: b.setLocation(b.getRow()+1, b.getCol()); break; //move down
default: return;
}
processBlockList();
}
我想知道程序应该如何读取 (KeyEvent) e。我真的不能输入箭头键....
请帮忙!
编辑:我还需要知道我需要添加到我的代码中,以便我的程序等待大约 700 毫秒的键输入,然后再转到另一种方法