0

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

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();
        }
    });

}

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

4

1 回答 1

2

I am afraid that maybe that infinite loop inside paint() is interfering with the events management (remember that there is a single thread for those 2 things to be done). Why don't you just fire a repaint() on every significant event (for example, a key press)?

于 2014-05-26T17:34:22.893 回答