KeyPressed在每个程序中都不起作用

时间:2014-06-08 01:33:36

标签: java

我将一些程序从学校的计算机(mac)转移到我的家用电脑上。一旦在我的计算机上,我注意到现在每个程序中的键都不起作用。我花了好几个小时试图找出为什么KeyPressed不起作用。两台计算机都使用Eclipse

是因为Java版本不同还是因为操作系统不同? 谢谢!

代码示例:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.JApplet;

public class Skeleton extends JApplet implements Runnable, MouseMotionListener, MouseListener, KeyListener {

    Random generator = new Random();
    boolean GameRunning = true, PlayAgain = true;
    int width, height;
    int score = 0;
    Image offscreenImage;

    int XX;
    int XY;
    Image Cart;
    Image candy[];
    int candyX[];
    int candyY[];
    boolean up = false, down = false, left = false, right = false;
    boolean candyRemaining[];
    Graphics offscr;
    Random r;
    Thread t;
    boolean inBounds = true;
    boolean onScreen = true;
    Image Title;
    boolean hasStarted = false;

    public Skeleton() {
        r = new Random();
        t = new Thread(this);
        t.start();
    }

    public void init() {
        XX = 0;
        XY = 0;
        candy = new Image[3];
        candyRemaining = new boolean[3];
        candyY = new int[3];
        candyX = new int[3];
        addKeyListener(this);
        addMouseMotionListener(this);
        addMouseListener(this);
        setVisible(true);
        setSize(500, 500);
        width = getSize().width;
        height = getSize().height;
        Title = getImage(getCodeBase(), "BKG.png");
        offscreenImage = createImage(width, height);
//      offscr = offscreenImage.getGraphics();
        Cart = getImage(getCodeBase(), "Untitled-1.png");
        candy[0] = getImage(getCodeBase(), "candy1.png");
        candy[1] = getImage(getCodeBase(), "candy2.png");
        candy[2] = getImage(getCodeBase(), "candy3.png");
        candyRemaining[0] = true;
        candyRemaining[1] = true;
        candyRemaining[2] = true;
        candyX[0] = 100;
        candyX[1] = 300;
        candyX[2] = 400;
        candyY[0] = 425;
        candyY[1] = 0;
        candyY[2] = 210;
    }

    public void paint(Graphics g) {
        super.paint(g);
        DecimalFormat df = new DecimalFormat("0.00");
        Random generator = new Random();
        if (hasStarted) {
            if (inBounds) {
                g.drawImage(Cart, XX, XY, this);
                this.Candy(g);
                if (left) {
                    XX -= 5;
                    if (XX < 0) {
                        XX += 5;
                    }
                }
                if (right) {
                    XX += 5;
                    if (XX > 500) {
                        XX -= 5;
                    }
                }
                if (down) {
                    XY += 5;
                    if (XX > 500) {
                        XY -= 5;
                    }
                }
                if (up) {
                    XY -= 5;
                    if (XY < 0) {
                        XY += 5;
                    }
                }
            } else {
                GameRunning = false;
            }
        } else {
            g.drawImage(Title, 0, 0, this);
        }
    }

    public void Candy(Graphics g) {
        for (int count = 0; count < 3; count++) {
            if (candyRemaining[count]) {
                g.drawImage(candy[count], candyX[count], candyY[count], this);
                if (candyX[count] < XX + 50 && candyX[count] + 50 > XX) {
                    if (candyY[count] < XY + 50 && candyY[count] + 50 > XY) {
                        System.out.println(count);
                        candyRemaining[count] = false;
                    }
                }
            }
        }
    }

    public void update(Graphics g) {
        paint(g);
    }

    public void run() {
        while (PlayAgain == true) {
            while (GameRunning == true) {
                repaint();
                try {
                    Thread.sleep(2);
                } catch (InterruptedException e) {
                };
            }
            if (GameRunning == false) {
            }
        }
    }

    public void mouseDragged(MouseEvent ev) {
    }

    public void mouseMoved(MouseEvent ev) {
    }

    public void mouseClicked(MouseEvent ev) {
        if (!hasStarted) {
            hasStarted = true;
        }
        if (GameRunning == false) {
            if (ev.getX() > 0 && ev.getX() < 1000 && ev.getY() > 0 && ev.getY() < 1000) {
                GameRunning = true;
            }
        }
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        onScreen = true;
        inBounds = true;
    }

    public void mouseExited(MouseEvent ev) {
        onScreen = false;
        inBounds = false;
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("Pressed");
        int key = e.getKeyCode();
        if (key == e.VK_A) {
            left = true;
        }
        if (key == e.VK_D) {
            right = true;
        }
        if (key == e.VK_S) {
            down = true;
        }
        if (key == e.VK_W) {
            up = true;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("Released");
        int key = e.getKeyCode();
        if (key == e.VK_A) {
            left = false;
        }
        if (key == e.VK_D) {
            right = false;
        }
        if (key == e.VK_S) {
            down = false;
        }
        if (key == e.VK_W) {
            up = false;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
}

3 个答案:

答案 0 :(得分:1)

请勿使用对象e作为密钥代码,请使用static KeyEvent类。此外,如果您使用switch语句而不是if语句,则代码看起来会更漂亮:

switch (e.getKeyCode()) {
        case KeyEvent.VK_A: // here's the change
            left = true;
        break;

答案 1 :(得分:0)

为了比较keypressed或release,你正在使用当前KeyEvent类的对象。 每次按下键时,都会创建新的KeyEvent对象,并且对于按键,您正在检查当前没有保持键值的对象,它只是按住了按下了哪个键。 所以要检查引用KeyEvent类的静态变量。

   switch(e.getKeyCode){
      case: KeyEvent.VK_A;
        Left =false;
      break;
      case: KeyEvent.VK_D;
        right=false;
      break;
      .
      .
      .
      .
      }

答案 2 :(得分:0)

KeyListener关注焦点是富有成效的。只有当组件具有焦点且具有焦点时才能通知KeyListener关键事件

简短的回答是使用

setFocusable(true);
requestFocusInWindow();

init方法

的末尾

更合适的答案是使用key bindings API,它使您能够控制生成关键事件所需的焦点水平

作为旁注,使用类似JPanel的内容并覆盖它paintComponent并将其用作主要游戏组件而不是JApplet更合适},它不是双缓冲的,并且可以在更新时闪烁......

你还应该将游戏逻辑从你的绘画方法中移出,并可能进入你的主游戏循环(或从你的游戏循环调用)。可以在任何时间调用一个绘制周期,多次在您的控制之外,这可能会影响您的输出。

我也怀疑你需要500fps,Thread.sleep(40);甚至Thread.sleep(16);可能会给你更稳定的结果