【问题标题】:using java swing Timer to process and append text into textarea使用 java swing Timer 处理文本并将其附加到 textarea
【发布时间】:2014-05-11 07:53:11
【问题描述】:

我有一个 JTextArea txtProcess 和 2 种处理方法:

第一种方法是使用 for 循环处理小于 10 的数字相乘。将它们相乘后,所有结果将使用 Timer 附加到 txtProcess。

我使用计时器来延迟追加。例如,附加到 txtProcess 的第一个结果。然后 500 毫秒后,第二个结果附加到 txtProcess。以此类推,直到所有结果都附加到 txtProcess。

像这样:

int a = 10;
int result = 0;
for(int i=1; i <= a; i++){
    result = i * a;
    txtProcess.append("Result "+ i +" = "+ result);
}

下面是我尝试过的第一种方法的代码。

    void first(){
        ActionListener listen = new ActionListener(){
            public void actionPerformed(ActionEvent e){
                for(int i=1; i <= a; i++){
                    result = i * a;
                    txtProcess.append("Result "+ i +" = "+ result +"\n");
                    if(i == 9){
                        ((Timer)e.getSource()).stop();
                    }
                }
            }
        };    
        Timer mine = new Timer(500, listen);
        mine.start();
    }

但是,它并没有像我预期的那样工作。我希望结果一个一个地附加到 txtProcess,而不是同时。这是第一个问题。我该如何解决这个问题?

当第一个方法中的所有进程都已执行时,该进程继续执行第二个方法。

第一种方法和第二种方法的过程之间存在时间间隔。

我的意思是这样的:第一个方法执行完成后,第二个方法执行将在 2 秒后开始。如您所见,时间间隔为 2 秒(或可能更长)。

所以,我尝试这样:

    void second(){
        ActionListener listen = new ActionListener(){
            public void actionPerformed(ActionEvent e){
                for(int i=1; i <= a; i++){
                    result = i * a;
                    txtProcess.append("Result "+ i +" = "+ result +"\n");
                    if(i == 9){
                        ((Timer)e.getSource()).stop();
                    }
                }
            }
        };    
        Timer mine = new Timer(500, listen);
        mine.start();
    }

然后我创建了另一种方法来结合它们:

       void combine(){
            ActionListener listen = new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    first();
                    second();
                }
            };    
            Timer mine = new Timer(500, listen);
            mine.start();
        }

但是,第一种方法和第二种方法同时执行。这是第二个问题:在第一种方法和第二种方法之间创建间隔时间。我该如何解决这个问题?

注意: 您可能认为这个问题与java for-loop in GUI TextArea 重复。我已经阅读并尝试了那里的代码,但它仍然无法解决问题。

【问题讨论】:

    标签: java swing timer actionlistener jtextarea


    【解决方案1】:

    Timer 所做的是每次达到间隔时执行ActionListener 中的代码。 因此,如果您希望文本附加 10 次,则您的侦听器内不得有 for 循环。计时器将为您处理循环。

    ActionListener listener = new ActionListener(){
      private int counter = 0;
      @Override
      public void actionPerformed( ActionEvent e ){
         txtProcess.append("Result "+ counter +" = "+ result);
         counter++;
         if ( counter == 10 ){
           ((Timer)e.getSource()).stop();
         }
      }
    }
    Timer timer = new Timer( 500, listener );
    timer.start();
    

    我没有仔细检查上面的代码,所以它可能包含一个语法错误或循环只是一次太多/一次比需要少。它更能说明Timer的用法。

    【讨论】:

    • 好的,我明白了。第一个问题解决了。第二个问题呢?
    • @Speaky 如果你理解我的回答,你应该知道为什么会出现第二个问题
    猜你喜欢
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2016-08-04
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多