import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import javax.swing.*;
public class two extends JFrame{
   JButton b;
   JPanel pp;
   ballpanel Ba;
   private final static int W=800;
   private final static int H=600;
   two()
   {
   setSize(W,H);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   setVisible(true);
   setLayout(new BorderLayout());
   pp=new JPanel();
   Ba=new ballpanel();
   pp.setLayout(new FlowLayout());
   b=new JButton("开始");
   pp.add(b);
   b.addActionListener(new action());
   add(pp,BorderLayout.SOUTH);
   add(Ba,BorderLayout.CENTER);
   }
   public static void main(String[] args)
   {
   new two();
   }
   class action implements ActionListener
   { public void actionPerformed(ActionEvent e) {
p P=new p();
P.start();

}
   
   }
   class p extends Thread
   {
   public void run()
   {
  
   while(true)
   {
   ball Ball=new ball();
       Ba.repaint();
       try {
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
}
   }
   }
   }
   class ballpanel extends JPanel
   {
 public void paintComponent(Graphics g)
 {
 super.paintComponent(g);
 Graphics2D g2 = (Graphics2D) g;
 for (ball b : balls) {
  g2.fill(b.getshape());
  System.out.println(b);
}
}
private ArrayList<ball> balls = new ArrayList<ball>();

   }
   
   class ball 
   {
   private int x,y;
   private int dx=25,dy=20;
   private final int WIDTH=30,HEIGHT=30;   
     public  ball()
     {
     x=dx+x;
     y=dy+y;
     move();
     }
     
     public void move()
     {
      if(x>=two.W)
      {
      x=two.W;
          dx=-dx;
      }
      if(y>=two.H)
      {
      y=two.H;
      dy=-dy;
      }
      if(x<0)
      {
      x=0;
      dx=-dx;
      }
      if(y<0)
      {
      y=0;
      dy=-dy;
      }
     }
     public Ellipse2D.Double getshape()
     {
      return new Ellipse2D.Double(x,y,WIDTH,HEIGHT);
     }
   }
}

解决方案 »

  1.   

    不太清楚这个程序想干什么,有点乱哈,不过小球出不来,是因为没有将小球加到重给的小球的数组中,参考一下下面的代码
        class p extends Thread {        public void run() {            while (true) {
                    ball Ball = new ball();
                    synchronized (Ba.balls) {
                        Ba.balls.add(Ball);
                    }
                    Ba.repaint();
                    try {
                        Thread.sleep(150);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
      

  2.   

    不好意思写错字了,是重绘,可以看一下你的ballpanel的paintComponent方法,重绘的小球是balls列表中,但是前面new出的小球都没有加到balls列表中呀,所以会不出来
      

  3.   

    我觉得不是这个问题,我用System.out.println(b);测试,但是没有输出,.说明,.paintComponent方法根本没有重绘
      

  4.   

    有些问题,比如每一个新创建的对象其实都是不移动的,即使重绘了也看不出效果
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Ellipse2D;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class Two extends JFrame {
    JButton b;
    JPanel pp;
    BallPanel ba;
    private final static int w = 800;
    private final static int h = 600;
    private ArrayList<Ball> balls = new ArrayList<Ball>(); Two() {
    setSize(w, h);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setLayout(new BorderLayout());
    pp = new JPanel();
    ba = new BallPanel();
    balls.add(new Ball());

    pp.setLayout(new FlowLayout());
    b = new JButton("开始");
    pp.add(b);
    b.addActionListener(new action());
    add(pp, BorderLayout.SOUTH);
    add(ba, BorderLayout.CENTER);
    } public static void main(String[] args) {
    new Two();
    } class action implements ActionListener { public void actionPerformed(ActionEvent e) {
    P p = new P();
    p.start(); } } class P extends Thread {
    public void run() { while (true) {

    System.out.println("ppp");

    Ball ball = new Ball();
    balls.add(ball);
    ba.repaint();
    try {
    Thread.sleep(150);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    } class BallPanel extends JPanel {
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    for (Ball b : balls) {
    g2.fill(b.getshape());
    b.move();
    }
    } } class Ball {
    private int x, y;
    private int dx = 25, dy = 20;
    private final int WIDTH = 30, HEIGHT = 30; public Ball() {
    x = dx + x;
    y = dy + y;
    move();
    } public void move() {
    x = dx + x;
    y = dy + y;
    if (x >= Two.w) {
    x = Two.w;
    dx = -dx;
    }
    if (y >= Two.h) {
    y = Two.h;
    dy = -dy;
    }
    if (x < 0) {
    x = 0;
    dx = -dx;
    }
    if (y < 0) {
    y = 0;
    dy = -dy;
    }
    } public Ellipse2D.Double getshape() {
    return new Ellipse2D.Double(x, y, WIDTH, HEIGHT);
    }
    }
    }
      

  5.   


    你是说的下面这个,for里的println(b)没有打印吧,上面不是已经说了,你的balls是空的,根本进不了这个for循环,第一次给你的代码,你到底有没有替换了试一试呀?for (ball b : balls) {
    g2.fill(b.getshape());
    System.out.println(b);
    }
      

  6.   

    [Quote=引用 5 楼 dracularking 的回复:]有些问题,比如每一个新创建的对象其实都是不移动的,即使重绘了也看不出效果Java codeimport java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    ……
    [/Quot
    为什么你的代码的小球原来画的不会消失,不是使用了repaint吗
      

  7.   

    哦我是照着你的源码 估摸着你本意改的
    不消失是因为每次都是重绘了n多个球,n是动态增长的,且最新的球位置都是初始点不变的
    要消失的话,去掉P线程类中下两句:
    Ball ball = new Ball();
    balls.add(ball);
      

  8.   


    去掉了,就什么都没有了..
        class BallPanel extends JPanel {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                for (Ball b : balls) {
                    g2.fill(b.getshape());
                    b.move();
                }
            }里的 b.move();为什么放在
     class P extends Thread {
            public void run() {            while (true) {
                    
                    System.out.println("ppp");
                    
                    Ball ball = new Ball();
                   ball.move();
                    balls.add(ball);
                    ba.repaint();
                    try {
                        Thread.sleep(150);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }里就不行了.
      

  9.   

    我是试过的,你是在什么基础上改的?
    发改后的全文吧:import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Ellipse2D;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class Two extends JFrame {
    JButton b;
    JPanel pp;
    BallPanel ba;
    private final static int w = 800;
    private final static int h = 600;
    private ArrayList<Ball> balls = new ArrayList<Ball>(); Two() {
    setSize(w, h);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setLayout(new BorderLayout());
    pp = new JPanel();
    ba = new BallPanel();
    balls.add(new Ball()); pp.setLayout(new FlowLayout());
    b = new JButton("开始");
    pp.add(b);
    b.addActionListener(new action());
    add(pp, BorderLayout.SOUTH);
    add(ba, BorderLayout.CENTER);
    } public static void main(String[] args) {
    new Two();
    } class action implements ActionListener { public void actionPerformed(ActionEvent e) {
    P p = new P();
    p.start(); } } class P extends Thread {
    public void run() { while(true) { System.out.println("ppp");// Ball ball = new Ball();
    // ball.move();
    // balls.add(ball);
    ba.repaint();
    try {
    Thread.sleep(150);
    } catch(InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    } class BallPanel extends JPanel {
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    for(Ball b : balls) {
    g2.fill(b.getshape());
    b.move();
    System.out.println(b.x + " " + b.y);
    }
    } } class Ball {
    private int x, y;
    private int dx = 25, dy = 20;
    private final int WIDTH = 30, HEIGHT = 30; public Ball() {
    x = dx + x;
    y = dy + y;
    move();
    } public void move() {
    x = dx + x;
    y = dy + y;
    if(x >= Two.w) {
    x = Two.w;
    dx = -dx;
    }
    if(y >= Two.h) {
    y = Two.h;
    dy = -dy;
    }
    if(x < 0) {
    x = 0;
    dx = -dx;
    }
    if(y < 0) {
    y = 0;
    dy = -dy;
    }
    } public Ellipse2D.Double getshape() {
    return new Ellipse2D.Double(x, y, WIDTH, HEIGHT);
    }
    }
    }移动了move之后不会动了是因为所有的球就只画了两个位置,所有的球可以说都是不移动的
    原来的话是排队移动的