Java 为什么不是';我的鱼不动了吗?

Java 为什么不是';我的鱼不动了吗?,java,Java,我想让我的“鱼”用WASD键移动。当我运行这个程序时,鱼会出现,但当我按下键时它不会移动。我看了又看,但找不到问题所在。请帮忙 public class RunGame extends JFrame{ public RunGame(){ initRunGame(); } private void initRunGame(){ add(new Board()); setResizable(false);

我想让我的“鱼”用WASD键移动。当我运行这个程序时,鱼会出现,但当我按下键时它不会移动。我看了又看,但找不到问题所在。请帮忙

public class RunGame extends JFrame{

    public RunGame(){
        initRunGame();
    }

    private void initRunGame(){

        add(new Board());

        setResizable(false);
        pack();

        setTitle("Game");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable(){
            @Override
            public void run(){
                RunGame rg=new RunGame();
                rg.setVisible(true);
            }
        });
    }
}



这是迄今为止所有的代码。谢谢你的帮助

我怀疑是因为这个复制粘贴错误:

private class TAdapter extends KeyAdapter{
    ...
    @Override
    public void keyPressed(KeyEvent e){
        fish.keyReleased(e);  <-- Whoops - should be keyPressed
    }
}
私有类TAdapter扩展了KeyAdapter{
...
@凌驾
按下公共无效键(按键事件e){

到目前为止,您采取了哪些调试步骤来缩小问题的范围?
public class MoveableSprite extends Sprite{

private int dx, dy;
private Board b;

public MoveableSprite(int x, int y, int w, int h, Board b){
    super(x,y,w,h);
    this.b=b;
    setColor(Color.BLUE);
}

public void move(){
    x+=dx; y+=dy;

    if(x<0){x=0;}
    if(y<0){y=0;}
    if(x>b.getWidth()){x=b.getWidth();}
    if(y>b.getHeight()){y=b.getHeight();}
    updateShape();
}

private void updateShape(){
    shape=new Ellipse2D.Double(getX(),getY(),w,h);
}

public void keyPressed(KeyEvent e){

    int key=e.getKeyCode();

    if(key==KeyEvent.VK_A){dx=-1;}
    if(key==KeyEvent.VK_D){dx=1;}
    if(key==KeyEvent.VK_W){dy=-1;}
    if(key==KeyEvent.VK_S){dy=1;}
}

public void keyReleased(KeyEvent e){

    int key=e.getKeyCode();
      if(key==KeyEvent.VK_A||key==KeyEvent.VK_D){dx=0;}
        if(key==KeyEvent.VK_W||key==KeyEvent.VK_S){dy=0;}
    }
}
    public class Sprite {

    protected int x, y, w, h;
    private boolean isVis;
    protected Shape shape;
    protected Color color;

    public Sprite(int x, int y, int w, int h){
        this.x=x; this.y=y; this.w=w; this.h=h;
        isVis=true; color=Color.BLACK;
        initSprite();
    }

    public void setColor(Color c){
        color=c;
    }

    private void initSprite(){
        shape=new Ellipse2D.Double(x,y,w,h);
        updateDimensions();
    }

    public int getX(){return x;}

    public int getY(){return y;}

    public boolean isVisible(){return isVis;}

    public Rectangle getBounds(){
        return new Rectangle(shape.getBounds());
    }

    public Shape getShape(){
        return shape;
    }

    protected void updateDimensions(){
        Rectangle r=getBounds();
        w=(int) r.getWidth();
        h=(int) r.getHeight();
    }
}
private class TAdapter extends KeyAdapter{
    ...
    @Override
    public void keyPressed(KeyEvent e){
        fish.keyReleased(e);  <-- Whoops - should be keyPressed
    }
}