Java Jframe在从JButton启动时停止动态更新

Java Jframe在从JButton启动时停止动态更新,java,swing,jframe,Java,Swing,Jframe,我想和大家分享一个问题,看看有没有人有什么想法。让我解释一下 我创建了一个相当复杂的图,它在运行时根据下面的代码片段进行动态更新 在主要区域内: if(singleRun){ try{ FileInputStream fin = new FileInputStream("C:\\eclipse\\data\\so.gad"); ObjectInputStream ois = new ObjectInputStream(fin); SO = (G

我想和大家分享一个问题,看看有没有人有什么想法。让我解释一下

我创建了一个相当复杂的图,它在运行时根据下面的代码片段进行动态更新

在主要区域内:

if(singleRun){
     try{
       FileInputStream fin = new FileInputStream("C:\\eclipse\\data\\so.gad");
       ObjectInputStream ois = new ObjectInputStream(fin);
       SO = (GAStatisticObject) ois.readObject();
       ois.close();
     }
   catch(FileNotFoundException FNFE){System.out.println("file not found"); System.exit(1);}
   catch(ClassNotFoundException CNFE){System.out.println("class not found");System.exit(1);}
   catch(IOException IOE){System.out.println("error during data save: "); IOE.printStackTrace 
       ();System.exit(1);}

    GenotypeSimulator GS = new GenotypeSimulator(SO, populationSize); //statistic object, population size, enable immediately
    GS.simulateEvolution();     
}
基本上,从文件和对象文件中读取数据 创建一个新的模拟引擎(该引擎依次创建GUI和图表) 然后开始模拟,基本上每次读取一个数据点的数据文件并更新图形。(模拟器代码如下)

public void simulateEvolution(){

对于(int x=0;xYou希望避免在上进行长时间的模拟计算,对吗?)直到“evolveSolution()”方法完成并立即更新。“阻止EDT”的典型症状。不要阻止EDT(事件调度线程)-发生这种情况时GUI将“冻结”。而不是调用
Thread.sleep(n)
为重复任务实施Swing
计时器
,或为长时间运行的任务实施SwingWorker
。有关更多详细信息,请参阅。强制性:感谢我所了解的一切。这显然与java如何处理事件有关。似乎一旦操作事件(侦听按钮单击)发生调用时,这将停止更新其他GUI,直到事件完成(因此新帧在最后更新)。我将模拟器包装到一个新线程中,这解决了问题。谁知道呢?
public void simulateEvolution(){

   for(int x=0; x<genNumberSolutionFound; x++){
    //System.out.println("updating data. GEN # = " + (x+1));   

    //if(x!=0){   
       try {Thread.sleep(1000);} 
       catch (InterruptedException e) {e.printStackTrace();}
    //}

    //data series 1
    XYDataItem point1 = null;
    if(x<genNumberSolutionFound-1){//parent vector is always 1 less in length then the rest
       point1 = new XYDataItem(new Double(x+1), new Double(selectedFitnessAvg.get(x)));    //parent select avg fitness
    }
    XYDataItem point2 = new XYDataItem(new Double(x+1), new Double(populationFitnessAvg.get(x)));  //avg fitness
    XYDataItem point3 = new XYDataItem(new Double(x+1), new Double(populationSmallestFitness.get(x))); //low fitness
    XYDataItem point4 = new XYDataItem(new Double(x+1), new Double(populationLargestFitness.get(x))); //high fitness

    //data series 2
    XYDataItem point5 = new XYDataItem(new Double(x+1), new Double(cumulativeAllelDistributionVector.get(x))); //diversity

    Vector<XYDataItem> DI = new Vector<XYDataItem>();
    DI.add(point1); //parent avg fitness
    DI.add(point2); //avg fitness
    DI.add(point3); //low fitness
    DI.add(point4); //high fitness
    DI.add(point5); //average diversity

    LGP.updateAllDataSeries(DI, new Integer(sizeOfPopulation*(x+1)));
    BGP.updateDataSet(x+1, instantaneousAllelDistributionMatrix.get(x));
   }

   BGP.setGARunComplete();