如题,有一个TextArea和一个按钮,在一个按钮事件里执行很多操作,每执行一个就在那个TextArea中写入记录.
现在的问题是,TextArea中的内容是在那些操作都执行完才一起显示出来的,好像是要等操作造成的阻塞结束之后才显示.
怎么能记TextArea的内容每改变一次,显示就更新一次呢?
谢谢~

解决方案 »

  1.   

    http://www.cnlei.org/blog/article.asp?id=298其实是 同步 执行 和 异步执行 的问题
    建议楼主去看看这方面的资料
      

  2.   

    以下是关键代码:static class albtnStart implements ActionListener
    {
    public void actionPerformed(ActionEvent evt)
          {



    HashMap<String,String> mfn=new HashMap<String,String>();
    mfn.put("proin", "in.pro.txt");
                            //此处省略N行mfn.put("***","***");

    //--------------------------------------------------------------
    writelog("Creating temp folder...\n");
    String strtempdir=CRtn.createTmpDir();//create a temp dir and returns the path
    if(strtempdir!=null){writelog("Done (temp folder '"+strtempdir+"').\n");}
    else{writelog("Failed.\n");return;}
    //---------------------------------------------------------------
    writelog("Verifying Protein sequence file..."); if(!CRtn.copyfile(strifPro,strtempdir+mfn.get("proin")))
    {writelog("The input file Protein cannot be opened.\n");return;}
    else{writelog("Done.\n");}

    writelog("Verifying DNA sequence file...");
    if(!CRtn.copyfile(strifDNA,strtempdir+mfn.get("dnain")))
    {writelog("The input file DNA cannot be opened.\n");return;}
    else{writelog("Done.\n");}

    writelog("Looking for Clustalw...");
    if(!CRtn.copyfile(strPthClu,strtempdir+mfn.get("clus")))
    {writelog("Cannot find Clustalw at the specified location.\n");return;}
    else{writelog("Done.\n");}

    writelog("Checking Phylip...");

    if(!CRtn.copyfile(strPthPhy+mfn.get("prot"),strtempdir+mfn.get("prot")))
    {writelog("Cannot find Phylip/protdist.\n");return;}
    if(!CRtn.copyfile(strPthPhy+mfn.get("dnad"),strtempdir+mfn.get("dnad")))
    {writelog("Cannot find Phylip/dnadist.\n");return;}
    if(!CRtn.copyfile(strPthPhy+mfn.get("seqb"),strtempdir+mfn.get("seqb")))
    {writelog("Cannot find Phylip/seqboot.\n");return;}
    if(!CRtn.copyfile(strPthPhy+mfn.get("neigh"),strtempdir+mfn.get("neigh")))
    {writelog("Cannot find Phylip/neighbor.\n");return;}
    if(!CRtn.copyfile(strPthPhy+mfn.get("cons"),strtempdir+mfn.get("cons")))
    {writelog("Cannot find Phylip/consense.\n");return;}
    writelog("Done.\n");
    JButton btn=(JButton)evt.getSource();
    btn.setEnabled(false);
    writelog("Processing...\n");

    //--------------------------------------------
    //run clustal for the protein seq

    if(CRtn.launchClu(strtempdir,mfn)==false)
    {
    writelog("clustalw.exe failed.\n");
    return;
    }
    else
    {
    writelog("Done with clustalw.exe...\n");
    }
                              }
                        } public static void writelog(String logtext)
    {
    txtLog.append(logtext);
    //txtLog.selectAll();
    txtLog.setSelectionStart(txtLog.getText().length());

    }CRtn.launchClu是调用一个外部程序,这个程序要执行10秒左右.问题就是,txtLog的内容要等到按钮事件代码全执行完(也就是launchClu返回)才能显示.怎么让它每改变一次都能显示出来?
      

  3.   

    你把你的writelog方法也成一个线程
    在里面写个内部类:private class WriteLogThread extends Thread{
        private String logtext;
        public WriteLogThread(String s){
           logtext = s;
        }
        pubulic void run(){
           txtLog.append(logtext);
           txtLog.setSelectionStart(txtLog.getText().length());  
        }
    }public void actionPerformed(ActionEvent evt){
        ............................
        if(....){
           new WriteLogThread(...).start();
        }          
    }
    如果你对JDK1。5的并发工具包熟悉的话,最好采用JDK1。5中的java.util.concurrent并发工具包ExecutorsService exec =Executors.newSingThreadExecutor();
    public void actionPerformed(ActionEvent evt){
        ............................
        if(....){
           exec.execute(new WriteLogThread(...));
            exec.shutdown();
           这样可以保证任意时刻在任何线程中都只有唯一的任务在运行,不需要在共享的TextArea上处理同步的问题
        }          
    }
      

  4.   

    多谢六楼帮忙~
    不过,前一个方法不行啊,还是显示不出,
    后一个方法,第一次写log时可以,写第二次时就抛出异常java.util.concurrent.RejectedExecutionException.
      

  5.   

    有没有一个方法能让程序阻塞在TextArea的更新上呢?
      

  6.   

    哈哈,我的问题解决了~~~ 
    在writelog最后加一句 
    txtLog.paintImmediately(txtLog.getBounds());
    就是立即重绘所在的矩形区域,这样就行了,文本域的内容实时更新.