想做个小球运动的代码,点击J键会向自动向右移,不过除了点问题,高手帮解决下
public class Point implements Runnable{
int x;
int y;
int speed=5; public  Point(int x,int y) {
this.x=x;
this.y=y;
} public  Point () {

}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
} public void run() {
while(true)
{
try {
Thread.sleep(50);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
x++;

System.out.println("子弹坐标:x="+x+"y="+y);
}
}     public void  moveright() {
x++;

}
    public void moveleft() {
x--;
}
    public void moveup()
    {
     y--;
    }
    public void movedown()
    {
     y++;
    }

}
================================================
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;import javax.swing.JFrame;
import javax.swing.JPanel;public class Test2 extends JFrame { public static void main(String[] args) {
Test2 test2=new Test2();


}
public  Test2() {
Point point=new Point();
Mypanel mypanel=new Mypanel();
add(mypanel);
Thread thread=new Thread(mypanel);
thread.start();
this.addKeyListener(mypanel);
this.setSize(400,360);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}}class Mypanel extends JPanel implements KeyListener,Runnable
{
Point point=new Point(10,10);
public void paint(Graphics g) {
super.paint(g);
g.fillOval(point.getX(), point.getY(), 10, 10);
this.repaint();

}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_J)
{
point.run();
Thread th1=new Thread(point);

th1.start();

}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
point.movedown();
}else if (e.getKeyCode()==KeyEvent.VK_UP) {
point.moveup();
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT) {
point.moveright();
}else if (e.getKeyCode()==KeyEvent.VK_LEFT) {
point.moveleft();
}

}
public void keyReleased(KeyEvent e) {


}
public void keyTyped(KeyEvent e) {


}
public void run() { }
}

解决方案 »

  1.   

    Runnable 接口不是干这个的。
      

  2.   


    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;class Point implements Runnable {    int x;
        int y;
        int speed = 5;    public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }    public Point() {
        }    public int getX() {
            return x;
        }    public void setX(int x) {
            this.x = x;
        }    public int getY() {
            return y;
        }    public void setY(int y) {
            this.y = y;
        }    public void run() {
            while (true) {
                try {
                    Thread.sleep(50);
                } catch (Exception e) {
    // TODO: handle exception
                    e.printStackTrace();
                }
                x++;
                System.out.println("子弹坐标:x=" + x + "y=" + y);
            }
        }    public void moveright() {
            x++;    }    public void moveleft() {
            x--;
        }    public void moveup() {
            y--;
        }    public void movedown() {
            y++;
        }
    }public class Test2 extends JFrame {    public static void main(String[] args) {
            Test2 test2 = new Test2();
        }    public Test2() {
            Mypanel mypanel = new Mypanel();
            Thread thread = new Thread(mypanel);
            thread.setDaemon(true);
            thread.start();
            add(mypanel);
            this.addKeyListener(mypanel);
            this.setSize(400, 360);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    }class Mypanel extends JPanel implements KeyListener, Runnable {    Point point = new Point(10, 10);    public void paint(Graphics g) {
            super.paint(g);
            g.fillOval(point.getX(), point.getY(), 10, 10);
        }    public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_J) {
                //point.run(); 这里的问题
                Thread th1 = new Thread(point);
                th1.start();        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                point.movedown();
            } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                point.moveup();
            } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                point.moveright();
            } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                point.moveleft();
            }    }    public void keyReleased(KeyEvent e) {
        }    public void keyTyped(KeyEvent e) {
        }    public void run() {
            //界面重绘
            while (true) {
                this.repaint();
            }
        }
    }