KeyListener第一次使用时没有响应?

时间:2011-12-02 23:49:45

标签: java swing jframe keylistener runnable

我查看了这些链接,没有人帮助过我。我正在使用KeyListener和Runnalbe使用JFrame制作游戏。我在过去的游戏中从来没有遇到任何问题,但现在我正在谈论的是:

  • 我启动程序,窗口出现,消息显示一切正常。
  • 没有钥匙工作 -i关上窗户 - 第二次打开一切正常。 - 如果我重置虚拟机并再试一次,请回到方块1。

这是我的构造函数:

public Game()
{
    super("ZMan");
    addKeyListener(this);
    setSize(800,600);
    setVisible(true);


    i = createImage(800,600);
    b = i.getGraphics();
    makeLevel();
    souls = new Image[3];
    particles = new double[20][3];
    pc = 10;
    pH = 20;
    player = new Image[8];
    nme = new Image[8];
    showMessage = false;
    try
    {
        bg = ImageIO.read(new File("bg.png"));
        abg = ImageIO.read(new File("abg.png"));
        bil = ImageIO.read(new File("buildingP.png"));
        bilP = ImageIO.read(new File("building.png"));
        message = ImageIO.read(new File("./system/messageBG.png"));

        souls[2] = ImageIO.read(new File("sR.png"));
        souls[1] = ImageIO.read(new File("sG.png"));
        souls[0] = ImageIO.read(new File("sB.png"));
        rem = souls[0];
        //player
        player[0] = ImageIO.read(new File("./player/normal.png"));
        player[1] = ImageIO.read(new File("./player/pBloop.png"));
        player[2] = ImageIO.read(new File("./player/pscorp.png"));
        player[3] = ImageIO.read(new File("./player/pSqui.png"));
        player[4] = ImageIO.read(new File("./player/pCato.png"));
        player[5] = ImageIO.read(new File("./player/pChubs.png"));
        player[6] = ImageIO.read(new File("./player/pPluckers.png"));
        player[7] = ImageIO.read(new File("./player/pSpooky.png"));
        gate = ImageIO.read(new File("./gate.png"));
        levelUp = ImageIO.read(new File("./system/levelUp.png"));
        xL = ImageIO.read(new File("./player/x.png"));
        /*
        nme[0] = ImageIO.read(new File("./enemies/bloop.png"));
        nme[1] = ImageIO.read(new File("./enemies/bloop.png"));
        nme[2] = ImageIO.read(new File("./enemies/scorp.png"));
        nme[3] = ImageIO.read(new File("./enemies/squi.png"));
        nme[4] = ImageIO.read(new File("./enemies/cato.png"));
         */
        nme[0] = Toolkit.getDefaultToolkit().createImage("./enemies/bloob.png");
        nme[1] = Toolkit.getDefaultToolkit().createImage("./enemies/bloob.png");
        nme[2] = Toolkit.getDefaultToolkit().createImage("./enemies/scorp.png");
        nme[3] = Toolkit.getDefaultToolkit().createImage("./enemies/squi.png");
        nme[4] = Toolkit.getDefaultToolkit().createImage("./enemies/cato.png");
        nme[5] = Toolkit.getDefaultToolkit().createImage("./enemies/chubs.png");
        nme[6] = Toolkit.getDefaultToolkit().createImage("./enemies/pluckers.png");
        nme[7] = Toolkit.getDefaultToolkit().createImage("./enemies/spooky.png");
    }
    catch(Exception e){e.printStackTrace();}
    powerT = new String[8];
    powerT[0] = "You went back to normal";
    powerT[1] = "You are now Bloop!";
    powerT[2] = "You are now Scorp!";
    powerT[3] = "You are now Squi!";
    powerT[4] = "You are now Cato!";
    powerT[5] = "You are now Chubs!";
    powerT[6] = "You are now Pluckers";
    powerT[7] = "You are now Spooky";
    startAim();
    //music = new Music("./system/music/0.wav");
    t.start();
}

是否有人知道这个问题?

2 个答案:

答案 0 :(得分:3)

“要触发键盘事件,组件必须具有键盘焦点。” - How to Write a Key Listener。我猜测相关组件第二次关注。您可以尝试requestFocusInWindow(),或者最好查看How to Use Key Bindings

附录:有一个关键的绑定示例here

答案 1 :(得分:0)

这可能听起来很愚蠢,但更多的是理智检查。检查您的主要方法。我建议做一个调试步骤,确保第一次启动窗口时,实际上调用了Game的构造函数......没有任何关于构造函数的东西看起来很糟糕(对我来说),这让我觉得可能会对对象的实例化进行管理。

如果不是这样,我无法帮助你;你必须等待更聪明的人回答问题:)