动态更新JFrame的问题

时间:2014-01-24 18:11:00

标签: java swing dynamic netbeans jframe

我正在使用NetNeans 我有一个JPanel,它应该显示一些已经输入的数据和一个进度条,而程序的其余部分执行一些冗长的处理(大约1分钟长)。处理完成后,应该使用包含已处理数据的几个JTextField更新相同的JPanel。 问题: 在整个处理过程中,JPanel显示完全空白。处理完成后,所有数据都会显示出来。令我困惑的是。这是我编程的方式:

    /*The parent class which calls the output JPanel
     *Lister is the output JPanel. The constructor calls initComponents(), and fills
     *text fields that contain the data that has already been input.
     *fillresult then triggers the processing function that takes a while. Once the
     *processing function is done, fillresult fills the processed data into the
     *respective Text Fields
     */
    ListWords = new Lister();
    System.out.println("l");
    ListWords.setVisible(true);
    ListWords.fillResult();

   //Lister constructor:
   Lister(){
         initComponents();
       MatchWords = new Matcher();//Matcher() only contains the initialization of a List

         jTextField1.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(0, 1));
         jTextField2.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(1, 2));
         jTextField3.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(2, 3));
         jTextField4.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(3, 4));
         jTextField5.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(4, 5));
         jTextField6.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(5, 6));
         jTextField7.setText(Scrabbulous.scrab.EntryWind.getCombo().substring(6));
   } 

   //fillresult():
 public void fillResult(){
    int rIndex = 0;
    MatchWords.makeWords(rIndex);
    while(MatchWords.realWords.Head == null && rIndex < 2){
        rIndex++;
        updateStatusBar();
        MatchWords.makeWords(rIndex);
    }
    Wordpoints = new String[MatchWords.realWords.getSize()];
    for(int i=0; i<Wordpoints.length; i++){
        Wordpoints[i] = "";
    }

    for(int i=0; i<MatchWords.realWords.getSize(); i++){
        int total = 0;
        for(int j=0; j<MatchWords.realWords.getNode(i).getWord().length(); j++){
            for(int k=0; k<Scrabbulous.scrab.scralphabet.length; k++){
                if(MatchWords.realWords.getNode(i).getWord().charAt(j) == Scrabbulous.scrab.scralphabet[k].getLetter()){
                    total += Scrabbulous.scrab.scralphabet[k].getValue();
                    Wordpoints[i] = ""+total;
                }
            }
        }
    }

    try{
        jTextField8.setText(MatchWords.realWords.Head.getWord());
        jTextField13.setText(Wordpoints[0]);
    }catch(NullPointerException e){
        jTextField8.setText("No Match Found");
        jTextField13.setText("-");
    }
    try{
        jTextField9.setText(MatchWords.realWords.getNode(1).getWord());
        jTextField14.setText(Wordpoints[1]);
    }catch(NullPointerException e){
        jTextField9.setText("No Match Found");
        jTextField14.setText("-");
    }
    try{
        jTextField10.setText(MatchWords.realWords.getNode(2).getWord());
        jTextField15.setText(Wordpoints[2]);
    }catch(NullPointerException e){
        jTextField10.setText("No Match Found");
        jTextField15.setText("-");
    }
    try{
        jTextField11.setText(MatchWords.realWords.getNode(3).getWord());
        jTextField16.setText(Wordpoints[3]);
    }catch(NullPointerException e){
        jTextField11.setText("No Match Found");
        jTextField16.setText("-");    
    }
    try{
        jTextField12.setText(MatchWords.realWords.getNode(4).getWord());
        jTextField17.setText(Wordpoints[4]);
    }catch(NullPointerException e){
        jTextField12.setText("No Match Found");
        jTextField17.setText("-");
    }

请有人帮忙吗?哦,是的,我读到了添加.refactor()和/或repaint(),但至少在构造函数中初始化的jForms应该显示出来? :/ 此外,值得一提的是,当前在fillresult()函数中的所有代码最初都在构造函数中,并且输出正在显示。但是,由于我想在这里使用进度条,我已将其转移到新功能。请帮忙吗?

1 个答案:

答案 0 :(得分:1)

  

“问题:在整个处理过程中,JPanel显示完全空白。处理完成后,所有数据都会显示出来。”

发生的事情是一切都发生在一个线程上,事件调度线程(EDT)。为了使事件发生,例如JPanel更新,需要完成之前的过程。

如前所述,美国东部时间:

  

“事件调度线程上的任务必须快速完成;如果没有,则会备份未处理的事件,并且用户界面将无法响应。”

要解决此问题,您需要在后台线程中运行长进程。您应该查看Concurrency in Swing - 特别是Worker Threads and SwingWorker部分。