如何在JAVA中使Animation与KeyBoard输入一起运行?

今天困扰我的问题是,我试图绘制一个红色的矩形,该矩形在用户按下空格时会移动.问题是,每当我按空格键时矩形都不会移动.有什么想法为什么以及如何解决这个问题?

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

    }

}

解决方法:

对您的代码进行了以下更改.现在按退格键可以移动矩形.

>将KeyListener添加到JFrame.
>更改了KeyBoard事件侦听器方法.

码:

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

}

}
上一篇:在Java中使用CardLayout


下一篇:Java简易实现记事本的打开与保存