KeyListener和MouseListener无法正常工作

时间:2013-01-03 23:55:51

标签: java swing keylistener mouselistener

我正在制作一个简单的Java游戏,并且有一个很好的想法,即将我的输入处理分离到一个单独的类,然后是主游戏。我无法让我的InputHandler类实际接收输入。

主游戏类(DrawPanel.java)

package com.eriksaulnier.DesignedToFail;

import java.awt.*;
import java.awt.image.*;
import com.eriksaulnier.DesignedToFail.InputHandler;

import javax.swing.*;

public class DrawPanel extends JPanel {
private static final long serialVersionUID = 1L;
BufferedImage buffer;
InputHandler inputHandler;
Entity player;
Entity enemy;
public boolean spawnBullet = false;

public DrawPanel () {
    setIgnoreRepaint(true);
    setVisible(true);
    setFocusable(true);
    addKeyListener(inputHandler);
    addMouseListener(inputHandler);
    new InputHandler();
}

public void initialize() {
    buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
    player = new Entity(370, 270);
    enemy = new Entity(100, 100);
}

public void update() {
    player.move();
}

public void checkCollisions() {
    if (player.getBounds().intersects(enemy.getBounds()))
        player.collision = true;
    else
        player.collision = false;
}

public void drawBuffer() {
    Graphics2D b = buffer.createGraphics();
    b.setColor(Color.white);
    b.fillRect(0, 0, 800, 600);
    if (player.collision == false) {
        b.setColor(Color.blue);
        b.fillRect(player.getX(), player.getY(), player.getWidth(), player.getHeight());
        b.setColor(Color.red);
        b.fillRect(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
        b.dispose();
    }
    else {
        b.setColor(Color.black);
        b.drawString("Collision!", 350, 300);
        b.dispose();
    }
}

public void drawScreen() {
    Graphics2D g = (Graphics2D)this.getGraphics();
    g.drawImage(buffer, 0, 0, this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}

public void startGame() {
    initialize();
    while(true) {
        try {
            update();
            checkCollisions();
            drawBuffer();
            drawScreen();
            Thread.sleep(15);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

}

}

InputHandler(InputHandler.java)

package com.eriksaulnier.DesignedToFail;

import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class InputHandler extends JPanel implements KeyListener, MouseListener {
public boolean isShooting = false;
Entity player;

public InputHandler () {
    System.out.println("Listener Works!");
}

public void mouseClicked(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {
    int button = e.getButton();
    if (button == MouseEvent.BUTTON1)
        isShooting = true;
        System.out.println("Shooting!");
}

public void mouseReleased(MouseEvent e) {
    int button = e.getButton();
    if (button == MouseEvent.BUTTON1)
        isShooting = false;
        System.out.println("Not Shooting!");
}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void keyTyped(KeyEvent e) {

}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_W)
        player.up = true;
    if (key == KeyEvent.VK_S)
        player.down = true;
    if (key == KeyEvent.VK_A)
        player.left = true;
    if (key == KeyEvent.VK_D)
        player.right = true;
}

public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_W)
        player.up = false;
    if (key == KeyEvent.VK_S)
        player.down = false;
    if (key == KeyEvent.VK_A)
        player.left = false;
    if (key == KeyEvent.VK_D)
        player.right = false;
}

}

2 个答案:

答案 0 :(得分:2)

您似乎将空处理程序传递到GUI中。例如,在使用之前,您是否实例化了inputHandler变量?

例如:

public class DrawPanel extends JPanel {
  //...

  InputHandler inputHandler; // here you declare the variable 

  //...

  public DrawPanel () {
    setIgnoreRepaint(true); // why this line?
    setVisible(true); // not needed in a JPanel's code
    setFocusable(true);
    addKeyListener(inputHandler); // here you use a null variable
    addMouseListener(inputHandler); // ditto, here you use a null variable
    new InputHandler(); // I don't know what you're doing here
  }

在上面的代码中,在使用之前我没有看到inputHandler = new InputHandler()的任何地方。你看起来在哪里创建一个新的InputHandler,你没有将对象分配给任何变量或使用它,所以它似乎是一个无用的代码行,因此我评论我怎么不知道该行是什么实现。请注意,这些问题与Swing无关,而与基本核心Java无关。

另外:

  • 您的处理程序类不应扩展Swing组件,例如JPanels。它们应该只实现监听器接口。
  • 您应该避免将KeyListeners与Swing GUI一起使用,而是尽可能使用Key Bindings。请Google支持并查看Key Bindings教程以获取更多相关信息。
  • Oracle Swing教程中详细解释了处理程序的使用。再次,请谷歌这些并研究它们。我可以证明它们的实用性,因为它们是我学习Swing编码的地方。

答案 1 :(得分:2)

至少,你忘了在绑定之前创建inputHandler的实例。 你的DrawPanel构造函数应该像:

public DrawPanel () {
    inputHander=new InputHandler();
    ...
}

如果在解决此问题后它无效,请告诉我们,我会深入了解一下。