日期:2014-05-20  浏览次数:20778 次

哎,到底问题出在哪里?看了好久没看出来!
我试着做个坦克大战游戏,但是今晚就出问题了:就是每次把鼠标单击一下界面后,程序就不受控制了,监听不到任何消息了,求大神帮忙!代码如下:
Java code
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;
public class Yumentank extends JFrame{
Mypanel1 mp=null;
Yumentank()
{
    mp=new Mypanel1();
    this.addKeyListener(mp);
    
    this.add(mp);
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       this.setSize(500,400);
       this.setVisible(true);
}
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Yumentank mytank=new Yumentank();
    }

}
//坦克基类
class Tank{
    int x;
    int y;
    int direction;
    int speed=1;
    Tank(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
    public void setX(int x)
    {
        this.x=x;
    }
    public void setY(int y)
    {
        this.y=y;
    }
    public int getX()
    {
        return this.x;
    }
    public int getY()
    {
        return this.y;
    }
    public void setDirction(int n)
    {
        direction=n;
    }
    public int getDirection()
    {
        return direction;
    }
}
//我的坦克
class Mytank extends Tank
{
    Mytank(int x,int y)
    {
        super(x,y);
        speed=3;
    }
    public void goUp()
    {
        direction=0;
        y-=speed;
    }
    public void goDown()
    {
        direction=1;
        y+=speed;
    }
    
    public void goLeft()
    {
        direction=2;
        x-=speed;
    }
    public void goRight()
    {
        direction=3;
        x+=speed;
    }
}

//面板
class Mypanel1  extends Panel implements KeyListener
{
    
    Mytank tank;
    Mypanel1(){
        tank=new Mytank(0,0);
    }
    public void paint(Graphics g)
    { 
        super.paint(g);
        this.drawTank(g, tank.getX(), tank.getY(),0,tank.getDirection());
        
        
    }
    public void drawTank(Graphics g,int x,int y,int type,int direct)
    {
        switch(direct)//go up
        {
        case 0:
        {
        g.setColor(Color.DARK_GRAY);
        g.fill3DRect(0, 0, 500, 400, false);
        g.setColor(Color.blue);
        g.fill3DRect(x, y, 10, 40,false);
        g.fill3DRect(x+30, y, 10, 40,false);
        
        g.fill3DRect(x+10, y+10, 20, 20,false);
        g.setColor(Color.lightGray);
        g.fillOval(x+10, y+13, 20, 15);
        g.fill3DRect(x+18, y, 3, 20,false);
        break;
        }
        case 1://gou down
        {
        g.setColor(Color.DARK_GRAY);
        g.fill3DRect(0, 0, 500, 400, false);
        g.setColor(Color.blue);
        g.fill3DRect(x, y, 10, 40,false);
        g.fill3DRect(x+30, y, 10, 40,false);
        
        g.fill3DRect(x+10, y+10, 20, 20,false);
        g.setColor(Color.lightGray);
        g.fillOval(x+10, y+13, 20, 15);
        g.fill3DRect(x+18, y+20, 3, 20,false);
        break;
        }
        case 2: //go left
        {
            g.setColor(Color.DARK_GRAY);
            g.fill3DRect(0, 0, 500, 400, false);
            g.setColor(Color.BLUE);
            g.fill3DRect(x, y, 40, 10,false);
            g.fill3DRect(x, y+30, 40, 10,false);
            
            g.fill3DRect(x+10, y+10, 20, 20,false);
            g.setColor(Color.lightGray);
            g.fillOval(x+12, y+11, 15, 20);
            g.fill3DRect(x, y+20, 20, 3,false);
            break;
        }
        case 3://go right
        {
            g.setColor(Color.DARK_GRAY);
            g.fill3DRect(0, 0, 500, 400, false);
            g.setColor(Color.BLUE);
            g.fill3DRect(x, y, 40, 10,false);
            g.fill3DRect(x, y+30, 40, 10,false);
            
            g.fill3DRect(x+10, y+10, 20, 20,false);
            g.setColor(Color.lightGray);
            g.fillOval(x+12, y+11, 15, 20);
            g.fill3DRect(x+20, y+20, 20, 3,false);
            break;
        }
        }
    }
    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getKeyCode()==KeyEvent.VK_UP)
        {
            this.tank.direction=0;
            this.tank.goUp();
            //System.out.println("go up");
            
        }
        else if(arg0.getKeyCode()==KeyEvent.VK_DOWN)
        {
            this.tank.direction=1;
            this.tank.goDown();
        }
        else if(arg0.getKeyCode()==KeyEvent.VK_LEFT)
        {
            this.tank.direction=2;
            this.tank.goLeft();
        }
        else if(arg0.getKeyCode()==KeyEvent.VK_RIGHT)
        {
            this.tank.direction=3;
            this.tank.goRight();
        }
            this.repaint();
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
}