What am i doing wrong to add a keylistener to a JFrame?

时间:2015-12-04 21:18:36

标签: java jframe keylistener

I don't know why but my keylistener in my JFrame is not working ? Here is the code how i add it:

f.addKeyListener(new KeyListener(){
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getID() == KeyEvent.VK_UP || e.getID() == KeyEvent.VK_W){
                up = true;
                System.out.println("key pressed");
            }
            if (e.getID() == KeyEvent.VK_LEFT || e.getID() == KeyEvent.VK_D){
                left = true;
                System.out.println("key pressed");
            }
            if (e.getID() == KeyEvent.VK_DOWN || e.getID() == KeyEvent.VK_S){
                down = true;
                System.out.println("key pressed");
            }
            if (e.getID() == KeyEvent.VK_RIGHT || e.getID() == KeyEvent.VK_A){
                right = true;
                System.out.println("key pressed");
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getID() == KeyEvent.VK_UP || e.getID() == KeyEvent.VK_W){
                up = false;
            }
            if (e.getID() == KeyEvent.VK_LEFT || e.getID() == KeyEvent.VK_D){
                left = false;
            }
            if (e.getID() == KeyEvent.VK_DOWN || e.getID() == KeyEvent.VK_S){
                down = false;
            }
            if (e.getID() == KeyEvent.VK_RIGHT || e.getID() == KeyEvent.VK_A){
                right = false;
            }
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }
    });

(f = new JFrame();)

If you need the whole class:

public class TheRealGame{

private static boolean running = false;
private static boolean paused = false;
private static boolean right = false, left = false, up = false, down = false;
private static JFrame f;
private static ArrayList<JLabel> ae = new ArrayList<JLabel>();
private static Player p;
private static Playere pt;
private static JLabel playerImage;

public static void main(Playere playertype){
    pt = playertype;
    p = new Player(pt);
    f = new JFrame();
    f.setVisible(true);
    f.setSize(700, 700);
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.addKeyListener(new KeyListener(){
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getID() == KeyEvent.VK_UP || e.getID() == KeyEvent.VK_W){
                up = true;
                System.out.println("key pressed");
            }
            if (e.getID() == KeyEvent.VK_LEFT || e.getID() == KeyEvent.VK_D){
                left = true;
                System.out.println("key pressed");
            }
            if (e.getID() == KeyEvent.VK_DOWN || e.getID() == KeyEvent.VK_S){
                down = true;
                System.out.println("key pressed");
            }
            if (e.getID() == KeyEvent.VK_RIGHT || e.getID() == KeyEvent.VK_A){
                right = true;
                System.out.println("key pressed");
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getID() == KeyEvent.VK_UP || e.getID() == KeyEvent.VK_W){
                up = false;
            }
            if (e.getID() == KeyEvent.VK_LEFT || e.getID() == KeyEvent.VK_D){
                left = false;
            }
            if (e.getID() == KeyEvent.VK_DOWN || e.getID() == KeyEvent.VK_S){
                down = false;
            }
            if (e.getID() == KeyEvent.VK_RIGHT || e.getID() == KeyEvent.VK_A){
                right = false;
            }
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }
    });
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setTitle("free play - tokyo ghoul");
    Start();
    p.Paint();
}
public static void resume(){
    if (paused == false){
        return;
    }
}
public static void pause(){
    if (paused == true){
        return;
    }
}
public static void Stop(){
    if (running == false){
        return;
    }
    running = false;
}
public static void Start(){
    if (running == true){
        return;
    }
    running = true;

    new Thread(new Runnable(){
        @Override
        public void run() {
            int last = 0;
            while (running == true){
                if (paused != true){
                    if (up == true){
                        p.move(p.getX(), p.getY()+10);
                        System.out.println("went");
                    }
                    if (down == true){
                        p.move(p.getX(), p.getY()-10);
                        System.out.println("went");
                    }
                    if (left == true){
                        p.move(p.getX()-10, p.getY());
                        System.out.println("went");
                    }
                    if (right == true){
                        p.move(p.getX()+10, p.getY());
                        System.out.println("went");
                    }
                    RepaintAllLabels();
                    Enemy.UpdateAll();
                    System.out.println("Went round!");
                    f.repaint();
                    if (last == 10){
                        Random r = new Random();
                        int x = 1+r.nextInt(2), y = 1+r.nextInt(2), distance = 1+r.nextInt(570), nx = 0, ny = 0;
                        if (x == 1){

                        }
                    }else{
                        last++;
                    }
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

public static void UpdateAll(){

}

public static void Paint(JLabel imgs, int x, int y, File file){
    ImageIcon img = new ImageIcon(file.getPath());
    imgs.setBounds(x, y, img.getIconWidth(), img.getIconHeight());
    imgs.setLocation(x, y);
    imgs.setVisible(true);
    f.add(imgs);
    imgs.setVisible(true);
}

public static void Repaint(JLabel l){
    f.remove(l);
    l.setBounds((int)l.getLocation().getX(), (int)l.getLocation().getY(), l.getWidth(), l.getHeight());
    f.add(l);
}

public static void addAE(JLabel l){
    ae.add(l);
}

public static void RepaintAllLabels(){
    for (int i = 0; i < ae.size(); i++){
        Repaint(ae.get(i));
    }
}

public static void MovePlayer(int x, int y){
    playerImage.setLocation(x, y);
    playerImage.repaint();
    f.repaint();
}

public static void paintplayer(){
    if (pt == Playere.Kaneki){
        playerImage = new JLabel(new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.jpg")));
        if (playerImage == null){
            System.out.println("[ERROR THIS WAS THE ERROR THE WHOLE TIME!!");
            return;
        }
        ImageIcon imgs = new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.jpg"));
        playerImage.setBounds(p.getX(), p.getY(), imgs.getIconWidth(), imgs.getIconHeight());
        playerImage.setLocation(0, 0);
        playerImage.setVisible(true);
        f.add(playerImage);
        playerImage.setVisible(true);
    }
    if (pt == Playere.Touka){
        playerImage = new JLabel(new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.jpg")));
        playerImage.setBounds(p.getX(), p.getY(), new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.jpg")).getIconWidth(), new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.jpg")).getIconHeight());
        playerImage.setLocation(p.getX(), p.getY());
        playerImage.setVisible(true);
        f.add(playerImage);
        playerImage.setVisible(true);
    }
}

}

1 个答案:

答案 0 :(得分:0)

您不应该使用KeyEvent#getId()而是使用KeyEvent#getKeyCode(),因此您需要e.getId() == ...替换它e.getKeyCode() == ...