我希望能够注册触发布尔变量的按键事件。我已经在我的主要课程中完成了,但现在我正试图把它放到课堂上,而且似乎没有用。
这是英雄级:
public class Hero extends Main {
private boolean downPressed;
private boolean leftPressed;
private boolean rightPressed;
public void init() {
}
public void paint(Graphics g, int x_pos, int y_pos) {
if (isDownPressed() && isLeftPressed()) {
this.sprite = hero225;
} else if (isDownPressed() && isRightPressed()) {
this.sprite = hero135;
} else if (isUpPressed() && isRightPressed()) {
this.sprite = hero45;
} else if (isUpPressed() && isLeftPressed()) {
this.sprite = hero315;
} else if (isLeftPressed() == true) {
this.sprite = hero270;
} else if (isRightPressed() == true) {
this.sprite = hero90;
} else if (isUpPressed() == true) {
this.sprite = hero;
System.out.println("Succsess");
} else if (isDownPressed() == true) {
this.sprite = hero180;
}
// this.sprite will contain value set on last "movement"
g.drawImage(this.sprite, x_pos, y_pos, this);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
setLeftPressed(true);
System.out.println("keyPressed");
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
setRightPressed(true);
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
setUpPressed(true);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
setDownPressed(true);
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
setLeftPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
setRightPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
setUpPressed(false);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
setDownPressed(false);
}
}
public void keyTyped(KeyEvent e) {
}
public void mouseClicked(MouseEvent e) {
System.out.println("HIT!");
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
boolean isButtonPressed = true;
}
public void mouseReleased(MouseEvent e) {
boolean isButtonPressed = false;
}
public void setDownPressed(boolean downPressed) {
this.downPressed = downPressed;
}
public boolean isDownPressed() {
return downPressed;
}
public void setLeftPressed(boolean leftPressed) {
this.leftPressed = leftPressed;
}
public boolean isLeftPressed() {
return leftPressed;
}
public void setRightPressed(boolean rightPressed) {
this.rightPressed = rightPressed;
}
public boolean isRightPressed() {
return rightPressed;
}
public void setUpPressed(boolean upPressed) {
this.upPressed = upPressed;
}
public boolean isUpPressed() {
return upPressed;
}
以下是调用它的关卡类:
public class Level extends Main {
Hero hero = new Hero();
public void paint(KeyEvent e, Graphics g, double x_pos, double x_pos2) {
repaint();
}
这是Main类中的Paint函数,它调用:
public void paint(Graphics g) {
Level level = new Level();
level.paint(e, g, x_pos, y_pos);
造成这种情况的问题似乎并不明显。
答案 0 :(得分:2)
您应该在要监听事件的类中实现java.awt.event.KeyListener
接口,在本例中为Hero,所以:
public class Hero extends Main implements java.awt.event.KeyListener
然后在某些方法中注册事件,可能是init或其他地方使用:
addKeyListener(this);
//rest of your code
或者如果你想只听一些事件,你可以代替实现接口KeyListener,注册一个适配器:
addKeyListener(new java.awt.KeyAdapter()
{
public void keyPressed(java.awt.KeyEvent e)
{
//handle just this event
}
}
);
//rest of of your code
答案 1 :(得分:1)
听起来像焦点问题:只有活动组件才会获得KeyEvents。
有几种方法可以解决这个问题,但我发现this教程很好地涵盖了这些想法。但是如果你想要一个快速而又脏的(脏?不确定,简单和简单也不错,我认为;对于大型项目只是有点臃肿)解决方案你可以为每个组件实现一个监听器并将事件转发给一些处理它的通用类。