【问题标题】:KeyInput won't work while running a while (JCreator,Applet)运行一段时间后 KeyInput 不起作用(JCreator、Applet)
【发布时间】:2014-07-15 12:27:09
【问题描述】:

假设每当您按下一个键时,它应该访问代码,但它没有,我或我的老师也不知道为什么。代码如下:

class KeyInput implements KeyListener{
Actor player;
Graphics gBuffer;

public KeyInput(Actor player, Graphics gBuffer){
    JTextField typingArea = new JTextField(20);
    typingArea.addKeyListener(this);
    this.player = player;
    this.gBuffer = gBuffer;
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT) {
        player.modActor(1);
        gBuffer.drawString("ENTER",150,100);
    }
    if (key == KeyEvent.VK_RIGHT) {
        player.modActor(2);
        gBuffer.drawString("ENTER",150,100);
    }
    if (key == KeyEvent.VK_UP) {
        player.modActor(3);
        gBuffer.drawString("ENTER",150,100);
    }
    if (key == KeyEvent.VK_DOWN) {
        player.modActor(4);
        gBuffer.drawString("ENTER",150,100);
    }
}

}

这是节目期间播放的时间:

   public void paint(Graphics g){ //main method to be called to play(g);
    do{
        drawBackground(gBuffer);
        gBuffer.setFont(new Font("Calibri",Font.BOLD,20));
        gBuffer.setColor(Color.CYAN);
        gBuffer.drawString("Score: " + (int)score,18,20);
        gBuffer.drawString("Score: " + (int)score,22,20);
        gBuffer.drawString("Score: " + (int)score,20,22);
        gBuffer.drawString("Score: " + (int)score,20,18);
        gBuffer.setColor(Color.BLACK);
        gBuffer.drawString("Score: " + (int)score,20,20);
        play(gBuffer);
        g.drawImage(virtualMem,0,0,this);
        if(lose==false){
            gBuffer.setFont(new Font("Calibri",Font.BOLD,50));
            gBuffer.setColor(Color.CYAN);
            gBuffer.drawString("GAME OVER",42,200);
            gBuffer.drawString("GAME OVER",38,200);
            gBuffer.drawString("GAME OVER",40,202);
            gBuffer.drawString("GAME OVER",40,198);
            gBuffer.setColor(Color.BLACK);
            gBuffer.drawString("GAME OVER",40,200);
            gBuffer.drawString("Final Score: " + (int)score,20,260);
        }
    }while(lose);

}

键输入需要移动我拥有的演员,但它根本不动,是的,我每次都在小程序内单击。知道发生了什么吗?

编辑:我现在放弃了 keyListener 并尝试了 keyBindings,但我是新手,可能有问题,请帮助我...

public void init() {
    panel = new JPanel();
    this.setSize(400,700);
    this.setFocusTraversalKeysEnabled(false);
    this.addKeyListener(this);
    player = new Actor(180,500);
    virtualMem = createImage(400,800);
    gBuffer = virtualMem.getGraphics();

    panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "up");
    panel.getActionMap().put("up", new AbstractAction(){
        public void actionPerformed(ActionEvent e) {
         gBuffer.drawString("IT WORKS", 150, 150);
         repaint();
        }
    });

    panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "down");
    panel.getActionMap().put("down", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
         gBuffer.drawString("IT WORKS", 150, 150);
         repaint();
        }
    });

    panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "left");
    panel.getActionMap().put("left", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
         gBuffer.drawString("IT WORKS", 150, 150);
         repaint();
        }
    });

    panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "right");
    panel.getActionMap().put("right", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
         gBuffer.drawString("IT WORKS", 150, 150);
         repaint();
        }
    });

}

while 还是一样的(play)。当我点击某些东西时,它也不会访问这些方法。

【问题讨论】:

  • 为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。但坚持使用键绑定..

标签: java awt paint keylistener jcreator


【解决方案1】:

我担心paint() 中的无限循环可能会干扰事件管理(请记住,有一个线程可以完成这两件事)。为什么不在每个重要事件(例如,按键)上触发repaint()?

【讨论】:

  • 试过了,还是不行。我注意到它甚至没有访问 KeyPressed 方法,因为我试图在它被调用时直接输出一些东西但仍然无法工作。我还尝试仅将方法放在游戏类中,但也不起作用。这是游戏的小图,以防万一。 puu.sh/92pQ2/9c34196c4a.png 应该移动的是中心向下的蓝色东西
猜你喜欢
  • 2016-04-19
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多