Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';I’我正在尝试创建一个显示循环中许多改变颜色的组件的显示器_Java_Loops_Graphics_Jframe_Delay - Fatal编程技术网

Java I';I’我正在尝试创建一个显示循环中许多改变颜色的组件的显示器

Java I';I’我正在尝试创建一个显示循环中许多改变颜色的组件的显示器,java,loops,graphics,jframe,delay,Java,Loops,Graphics,Jframe,Delay,我对上述问题的看法是,java中的图形函数似乎只在方法结束时起作用,这意味着它们在循环过程中永远不会产生输出,并且无法延迟视觉变化以防止它们同时发生。如果你试图用睡眠来延迟视觉上的改变,代码只会花更长的时间来编译,然后在最后一次完成所有视觉上的改变 下面是我正在使用的代码的一个小陷阱: public class simulation{ //this class is inside of my main class so it inherits all the objects publ

我对上述问题的看法是,java中的图形函数似乎只在方法结束时起作用,这意味着它们在循环过程中永远不会产生输出,并且无法延迟视觉变化以防止它们同时发生。如果你试图用睡眠来延迟视觉上的改变,代码只会花更长的时间来编译,然后在最后一次完成所有视觉上的改变

下面是我正在使用的代码的一个小陷阱:

public class simulation{     //this class is inside of my main class so it inherits all the objects 

public void update(){

//drawProgression is supposed to be called from here in a loop, and update is called from the main class
//I hanven't found a way to make this work though

}

//This method is meant to turn a grid of 12 panels green one by one
//pnM# are the panels
//This method works, but I can't find a way to loop it, or delay the actions so the user can see the changes as they happen
private void drawProgression(){

       if(progressionCounter>11){
           progressionCounter=0;
       }

       if(progressionCounter==0){
       pnM1.setBackground(GREEN);
       pnM12.setBackground(GRAY);
       }
       if(progressionCounter==1){
       pnM2.setBackground(GREEN);
       pnM1.setBackground(GRAY);
       }
       if(progressionCounter==2){
       pnM3.setBackground(GREEN);
       pnM2.setBackground(GRAY);
       }   
       if(progressionCounter==3){
       pnM4.setBackground(GREEN);
       pnM3.setBackground(GRAY);
       }
       if(progressionCounter==4){
       pnM5.setBackground(GREEN);
       pnM4.setBackground(GRAY);
       }
       if(progressionCounter==5){
       pnM6.setBackground(GREEN);
       pnM5.setBackground(GRAY);
       }
       if(progressionCounter==6){
       pnM7.setBackground(GREEN);
       pnM6.setBackground(GRAY);
       }
       if(progressionCounter==7){
       pnM8.setBackground(GREEN);
       pnM7.setBackground(GRAY);
       }
       if(progressionCounter==8){
       pnM9.setBackground(GREEN);
       pnM8.setBackground(GRAY);
       }
       if(progressionCounter==9){
       pnM10.setBackground(GREEN);
       pnM9.setBackground(GRAY);
       }
       if(progressionCounter==10){
       pnM11.setBackground(GREEN);
       pnM10.setBackground(GRAY);
       }
       if(progressionCounter==11){
       pnM12.setBackground(GREEN);
       pnM11.setBackground(GRAY);
       }

       progressionCounter++;
   }
   }

我试图用Java或Java JFrame做的事情可能吗?因为我已经被难住几个小时了,我该怎么办呢。谢谢。

您可以使用javax.swing.Timer来解决此MVC可能很有用,您的progressionCounter将是您的模型,然后当progressionCounter增加时,您会自动调用update(),它运行您的函数,然后重新绘制pnM。您可以使用javax.swing.Timer来解决此MVC,您的progressionCounter将是您的模型,然后当progressionCounter递增时,您会自动调用update(),它运行您的函数,然后重新绘制pnM