想写个动画变化啊,
已经写了一个函数 setImg(int num);//能够把某个按钮的图片改成"img/pic"+num+".png";图片在资源里面,已经测试没问题,单独执行切换正常.
然后写了个loop
for(int i=0;i<9;i++){
setImg(i);
Thread.Sleep(200);//Thread.currentThread()效果一样..
}
然后能看到的只有最后一张图片...
运行完就突然卡了啊..似乎卡了9*200ms啊,然后才看到图片变换,不过是瞬间的了..所以就是最后一张...
那个高手帮忙解决一下啊...本人新手上路,感谢了啊!!T_T

解决方案 »

  1.   

    200ms太快了吧.一下子就过去了啊
    1000ms才是一秒啊.
      

  2.   

    对于这样的变换最好采用独立的线程完成
    Thread t = new Thread(){
    public void run()
    {
    int i = 0;
    while(true){ 
    if(i >max)i = 0;//从头循环
    setImg(i++); 
    Thread.Sleep(200);//Thread.currentThread()效果一样.. 
    } }
      

  3.   

    设置1000ms都试验过拉,而且200ms的话,一秒才5帧啊我已经这么写了
         public void run(){
         for (int i=0;i<10;i++){
                                                    DisplayArray[0][0].setIMG(boxImg.getImg1(i));
         try{   
         Thread.currentThread().sleep(200);  
         }catch(InterruptedException es){ 
         } 
         }
         }
    算是独立了吧,但是还是不行啊
      

  4.   

    private SquareBox DisplayArray[][];
    private BgImg boxImg;//一个菜单按钮激活
    menuItem.addActionListener(
         new ActionListener(){
         public void actionPerformed(ActionEvent e){
         Thread tt=new Thread(){
         public void run(){
         for (int i=0;i<10;i++){
         test--;
         if( test>=0){
         DisplayArray[0][0].setBackground(Color.RED);
         DisplayArray[0][0].setIMG(boxImg.getImg1(test));
         }else if (test>=-7){
         DisplayArray[0][0].setBackground(Color.RED);
         DisplayArray[0][0].setIMG(boxImg.getImg2(-test));
         if (test<-1){
         DisplayArray[1][0].setBackground(Color.RED);
              DisplayArray[1][0].setIMG(boxImg.getImg1(9+test+1));
         }
        
         }
         try{   
         Thread.currentThread().sleep(500);  
         }catch(InterruptedException es){ 
         } 
         }
         }
         };
         tt.run();
         }
         }
         );public class SquareBox extends JButton{
    private static final long serialVersionUID = 3642375119924499879L;
    public static int countColumn=0;
    private int columnNum;
    public SquareBox(ImageIcon img){
    this.setIcon((Icon)img);
    this.setBackground(Color.WHITE);
    this.setBorder(null);
    this.setSize(60, 60);
    }
    public void setColumn(int x){
    columnNum=x;
    }
    public int getColumnNum(){
    return columnNum;
    }
    public void setIMG(ImageIcon img){
    this.setIcon((Icon)img);;
    }
    }package comp229.s40928497.connect4.interface_;
    import javax.swing.ImageIcon;
    public class BgImg {
    private ImageIcon[] imageSet1;
    private ImageIcon[] imageSet2;
    BgImg(){
    imageSet1=new ImageIcon[9];
    for(int i=0;i<9;i++){
    imageSet1[i]=new ImageIcon(getClass().getResource("/img/box"+i+".png"));
    }
    imageSet2=new ImageIcon[8];
    for(int i=0;i<8;i++){
    imageSet2[i]=new ImageIcon(getClass().getResource("/img/boxdown"+(i+1)+".png"));
    }
    }
    public ImageIcon getImg1(int imgNum){
    return imageSet1[imgNum];
    }
    public ImageIcon getImg2(int imgNum){
    return imageSet2[imgNum];
    }}
      

  5.   

    你先打印确定一下run方法中setIMG方法是不是按间隔调用了
      

  6.   

    tt.start();
    启动一个线程不要直接run