Keylistener在Java实现中。

时间:2016-09-14 13:18:57

标签: java keylistener

好的,所以我正在使用java,并且在按下按键时尝试调用函数。我正在使用Keylistener但是当按下左箭头键时我可以打印它来测试或测试123。

import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.util.List;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.ArrayList;
    import javax.swing.Timer;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class game extends JPanel implements KeyListener{

    int level = 1;
    //Player player = new Player(this);
    private int frameCount = 0;
    Player mainPlayer = new Player();
    static List<Platform1> level1 = new 

的ArrayList();

public game(){

}

public void move(){
    //player.move();
}

@Override
public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g.fillRect(mainPlayer.x, mainPlayer.y, mainPlayer.width, mainPlayer.height);
    if(level == 1){
        for(int i = 0; i<level1.size();i++){
            g.fillRect(level1.get(i).x,level1.get(i).y,level1.get(i).width,level1.get(i).height);
        }
    }
    //player.paint(g2d);
}

public void timer1(){
     int delay = 10; //milliseconds
      ActionListener taskPerformer = new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
           frameCount++;
           mainPlayer.updateLocation();

          }
      };
      new Timer(delay, taskPerformer).start();;

try {
    Thread.sleep(10);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

public static void main(String args[]) throws InterruptedException, Exception{
    int Width = 1000;
    int Height = 600;
    game game = new game();
    JFrame frame = new JFrame("quest Kings");
    frame.setSize(Width, Height);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setBackground(Color.green);
    frame.add(game);
    frame.setResizable(false);
    level1.add(0,new Platform1(400,64,128,400));
    level1.add(0,new Platform1(400,64,328,200));

    //What to do after the program starts
    while(true){
        game.move();
        game.repaint();
        game.timer1();

    }
}


    public void keyTyped(KeyEvent e){
    }

    public void keyReleased(KeyEvent e){
        //player.keyReleased(e);
    }

    public void keyPressed(KeyEvent e){
        System.out.println("Testing");
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_LEFT) {
           mainPlayer.setDirection(-1);
           System.out.println("Testing123");
        } 
        else if (keyCode == KeyEvent.VK_RIGHT) {
            mainPlayer.setDirection(1);

        }
        else if (keyCode == KeyEvent.VK_UP) {
            mainPlayer.startJump();

        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要设置监听器,只要自己的类实现它,您必须使用this

JFrame frame = new JFrame("quest Kings");
frame.addKeyListener(this);

或者您可以使用创建的实例(不需要IMHO,但我应该检查更多代码)

game game = new game();
JFrame frame = new JFrame("quest Kings");
frame.addKeyListener(game);