在Java中使用keyAdapter和keyEvent时出错 [英] Error using keyAdapter and keyEvent in java
问题描述
我是编程的初学者,我一直在从事一个名为Tetris的著名小游戏,而我遇到了这个小问题,我希望您能为我提供解决方案.我导入了import java.awt.event.KeyAdapter
和
import java.awt.event.KeyEvent
可以使用键盘玩游戏,但是当我扩展为使用键而创建的类时,它向我显示错误!
这是代码:
addKeyListener(new TAdapter());
错误在这里说:
The method addKeyListener(keyListener) in the type Component is not applicable for the arguments(Board.TAdapter)
class TAdapter扩展keyAdapter {//第二个发生在这里:keyAdapter无法//解析为一个类型 public void keyPressed(keyEvent e){//第三次发生在这里:keyEvent//无法解析为类型
if (!isStarted || curPiece.getShape() == Tetrominoes.NoShape) {
return;
}
int keycode = e.getKeyCode();
if (keycode == 'p' || keycode == 'P') {
pause();
return;
}
if (isPaused)
{return;}
switch (keycode) {
case KeyEvent.VK_LEFT:
tryMove(curPiece, curX - 1, curY);
break;
case KeyEvent.VK_RIGHT:
tryMove(curPiece, curX + 1, curY);
break;
case KeyEvent.VK_DOWN:
tryMove(curPiece.rotateRight(), curX, curY);
break;
case KeyEvent.VK_UP:
tryMove(curPiece.rotateLeft(), curX, curY);
break;
case KeyEvent.VK_SPACE:
dropDown();
break;
case 'd':
oneLineDown();
break;
case 'D':
oneLineDown();
break;
}
}
您应该避免使用KeyListener
,它们有很多与重点相关的问题,它们还会使您的代码膨胀,并使管理更加困难. >
您应该改为使用密钥绑定API ,它提供了更可重用的API,并提供了确定组件接收关键事件所需的焦点级别的方法
I'm a beginner in programming and I have been working on a small project, the well known game called Tetris and I came upon this little problem and I would like you to help me with a solution. I imported : import java.awt.event.KeyAdapter
and
import java.awt.event.KeyEvent
to be able to use my keyboard to play the game, but when I'm extending the class that I created to use the keys, it showing me an error!!
Here is the code:
addKeyListener(new TAdapter());
The error happens here saying this:
The method addKeyListener(keyListener) in the type Component is not applicable for the arguments(Board.TAdapter)
class TAdapter extends keyAdapter { // The second happens here: keyAdapter cannot be //resolved to a type public void keyPressed(keyEvent e) { // The third happens here: keyEvent //cannot be resolved to a type
if (!isStarted || curPiece.getShape() == Tetrominoes.NoShape) {
return;
}
int keycode = e.getKeyCode();
if (keycode == 'p' || keycode == 'P') {
pause();
return;
}
if (isPaused)
{return;}
switch (keycode) {
case KeyEvent.VK_LEFT:
tryMove(curPiece, curX - 1, curY);
break;
case KeyEvent.VK_RIGHT:
tryMove(curPiece, curX + 1, curY);
break;
case KeyEvent.VK_DOWN:
tryMove(curPiece.rotateRight(), curX, curY);
break;
case KeyEvent.VK_UP:
tryMove(curPiece.rotateLeft(), curX, curY);
break;
case KeyEvent.VK_SPACE:
dropDown();
break;
case 'd':
oneLineDown();
break;
case 'D':
oneLineDown();
break;
}
}
You should avoid KeyListener
s, they have a number of issues related to focus, they can also bloat your code and make the management more difficult.
You should instead, take advantage of the Key Bindings API, which provide a more reusable API and provide the means to determine the level of focus a component needs in order to recieve key events
这篇关于在Java中使用keyAdapter和keyEvent时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!