简体   繁体   English

使用Java swing Timer处理文本并将其追加到textarea

[英]using java swing Timer to process and append text into textarea

I have a JTextArea txtProcess and 2 methods to process: 我有一个JTextArea txtProcess和2个要处理的方法:

First method is to process multiplying numbers less than 10 using for loop. 第一种方法是使用for循环处理小于10的数字。 After multiplying them, all results will be appended to txtProcess using Timer. 将它们相乘后,所有结果将使用Timer附加到txtProcess。

I use timer to delay the appending. 我使用计时器来延迟附加。 For example, first result appended to txtProcess. 例如,第一个结果附加到txtProcess。 Then 500 miliseconds later, second result appended to txtProcess. 然后500毫秒后,第二个结果附加到txtProcess。 And so forth until all results appended to txtProcess. 依此类推,直到所有结果都附加到txtProcess。

Like this: 像这样:

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

Below is piece of code I've tried for first method. 以下是我为第一种方法尝试过的代码。

    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();
    }

But, it doesn't work as I expected. 但是,它不符合我的预期。 I expected the results append to txtProcess one by one, not simultaneously. 我希望结果被一个接一个地追加到txtProcess上,而不是同时进行。 This is the first problem . 这是第一个问题 How can I fix this? 我怎样才能解决这个问题?

When all process in first method already executed, the process continue to second method. 当第一种方法中的所有过程都已执行时,该过程将继续执行第二种方法。

There is time interval between process of first method to second method. 第一种方法的处理与第二种方法的处理之间存在时间间隔。

I mean like this: after first method execution is finished, the second method execution will be started 2 seconds later. 我的意思是这样:第一个方法执行完成后,第二个方法执行将在2秒后开始。 As you see, the time interval is 2 seconds (or probably longer). 如您所见,时间间隔为2秒(或更长)。

So, I tried like this: 所以,我这样尝试:

    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();
    }

Then I created another method to combine both of them: 然后,我创建了另一个方法来将两者结合起来:

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

But, both first and second method executed simultaneously. 但是,第一方法和第二方法同时执行。 This is the second problem : to create interval time between first method and second method. 这是第二个问题 :在第一种方法和第二种方法之间创建间隔时间。 How can I fix this? 我怎样才能解决这个问题?

Note: You might think this question is duplicated with java for-loop in GUI TextArea . 注意:您可能会认为此问题与GUI TextArea中的java for-loop重复。 I already read and tried the code there, but it still can't fix the problems. 我已经在这里阅读并尝试了代码,但仍然无法解决问题。

What the Timer does is executing the code in the ActionListener each time the interval is reached. 每当间隔达到时, Timer所做的就是在ActionListener执行代码。 So if you want text to append 10 times, you must not have a for loop inside your listener. 因此,如果您希望文本追加10次,则侦听器内不得有for循环。 The timer will take care of the looping for you. 计时器将为您处理循环。

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();

I haven't carefully checked the code above, so it might contain a syntax error or loop just one time too much / one time less then needed. 我没有仔细检查上面的代码,因此它可能包含语法错误,或者循环次数过多/所需次数少了一次。 It serves more to illustrate the usage of the Timer . 它更多地用于说明Timer的用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM