如何在JAVA中使用KeyBoard输入运行动画?

时间:2016-04-18 05:09:21

标签: java swing awt keylistener

今天我的脑海里浮现的问题是,我正在尝试绘制一个红色矩形,每当用户按下空格时它就会移动。问题是每当我按空格时矩形都不会移动。任何想法为什么以及如何解决这个问题?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

public class Animation extends JPanel implements ActionListener, KeyListener{

    int x = 0, y = 0, velx=0;

    public Animation(){
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public static void main(String args[]){
        JFrame frame = new JFrame("Animation Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(500, 500);
        Animation a = new Animation();
        frame.add(a);

    }

    public void paintComponent(Graphics g){
    super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 500, 500);
        g.setColor(Color.RED);
        g.fillRect(x, 50, 30, 20);
        }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        x = x + velx;
        repaint();
    }

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

    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        int keyCode = e.getKeyCode();
        if(keyCode == e.VK_SPACE){
            velx = 2;
        }

    }

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

    }

}

2 个答案:

答案 0 :(得分:3)

问题是KeyListener只是......代码的痛苦。

它只会在注册的组件可对焦并且具有键盘焦点时响应键事件。问题是,焦点变化,管理和控制都很痛苦。

相反,您应该使用旨在帮助解决此问题的Key Bindings API

关于SO的关键绑定有很多例子,可能像this这样可能会有帮助

答案 1 :(得分:1)

对您的代码进行了以下更改。现在按后退空格应该移动矩形。

  1. 将KeyListener添加到JFrame。
  2. 对KeyBoard事件侦听器方法的更改。
  3. <强> CODE:

    public class Animation extends JPanel implements ActionListener, KeyListener{
    
    int x = 0, y = 0, velx=0;
    
    public Animation(){
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }
    
    public static void main(String args[]){
        JFrame frame = new JFrame("Animation Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(500, 500);
        Animation a = new Animation();
        frame.add(a);
        frame.repaint();
        frame.addKeyListener(a);
    
    }
    
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 500, 500);
        g.setColor(Color.RED);
        g.fillRect(x, 50, 30, 20);
     }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        x = x + velx;
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub
        int keyCode = e.getKeyCode();
        if(keyCode == e.VK_SPACE){
            velx = 2;
            x = x + velx;
        }
        repaint();
    }
    
    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    
    }
    
    }