简体   繁体   中英

KeyListener in Java

Hello everyone I am new to Java so i think the answer to this question is very easy but I can't find out what part i'm doing wrong.. I added a keyListener to my pacman game but somehow it won't work.. i've used the following code:

package h04PacMan;

import java.awt.event.*;

import javax.swing.*;

public class PacManBediening extends JPanel implements ActionListener, KeyListener {

private JButton links, rechts, boven, beneden;
PacMan pacman;

public PacManBediening(PacMan pacman) {

    this.pacman = pacman;

    links = new JButton("<");
    links.addActionListener(this);
    add(links);

    rechts = new JButton(">");
    rechts.addActionListener(this);
    add(rechts);

    boven = new JButton("^");
    boven.addActionListener(this);
    add(boven);

    beneden = new JButton("v");
    beneden.addActionListener(this);
    add(beneden);

}

/*
 * bediening bij een klik
 */

@Override
public void actionPerformed(ActionEvent e) {

    if(e.getSource() == links) {

        pacman.setRichtingEnSnelheid( -10 );
        pacman.setBesturing(0);
        pacman.setView(180);
        //System.out.println("links");
    }
    else if(e.getSource() == rechts) {
        pacman.setRichtingEnSnelheid( +10 );
        pacman.setBesturing(0);
        pacman.setView(0);
        //System.out.println("rechts");
    }
    else if(e.getSource() == boven) {
        pacman.setRichtingEnSnelheid( -10);
        pacman.setBesturing(1);
        pacman.setView(90);
        //System.out.println("boven");
    }
    else {
        pacman.setRichtingEnSnelheid( +10);
        pacman.setBesturing(1);
        pacman.setView(270);
        //System.out.println("beneden");
    }


}

@Override
public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if(key == KeyEvent.VK_LEFT) {
        pacman.setRichtingEnSnelheid( -10 );
        pacman.setBesturing(0);
        pacman.setView(180);
        System.out.println("links");
    }
    else if(key == KeyEvent.VK_RIGHT) {
        pacman.setRichtingEnSnelheid( +10 );
        pacman.setBesturing(0);
        pacman.setView(0);
        System.out.println("rechts");
    }
    else if(key == KeyEvent.VK_UP) {
        pacman.setRichtingEnSnelheid( -10);
        pacman.setBesturing(1);
        pacman.setView(90);
        System.out.println("boven");
    }
    else if(key == KeyEvent.VK_DOWN) {
        pacman.setRichtingEnSnelheid( +10);
        pacman.setBesturing(1);
        pacman.setView(270);
        System.out.println("beneden");
    }

}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

}

can someone tell me what to add or do different?

You're missing a couple of lines in your PacManBediening constructor.

this.pacman = pacman;
this.setFocusable(true);
this.addKeyListener(this);
  • KeyListener isn't designated for Swing JComponents , I wouldn't going this way,

  • I think there is simple and possible to lost the Focus from focusable JComponents

  • use KeyBindings as most scallable workaround, rather that "catching or hunting for Focus" for KeyListener

  • for Swing JComponents are all internal short_cuts, key shortcuts, built_in methods, notifiers, based on KeyBindings

  • code example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM