java - TimerTask 的问题

标签 java swing timer delay timertask

看看这段代码:

public Reminder() {

    a[0]=1000;
    a[1]=3000;
    a[2]=1000;
   a[3]=5000;
    timer = new Timer();

  timer.schedule(new RemindTask(),0,  a[i]);

  }
 //////////////////////
   class RemindTask extends TimerTask  {

    public void run() {

  point =point +arr[i].length();

     doc.setCharacterAttributes(0,point+1, textpane.getStyle("Red"), true);
     i++;

    }
    }

我希望在每个任务之后更改延迟,因此时间存储在数组中。 当i++执行时(指向数组的指针),时序没有改变;后续的延迟率与第一个延迟值相同。为什么它没有改变?

编辑:

如果需要,这里有一个 SSCCE:

import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;


public class Reminder {
static   JFrame frame;
Toolkit toolkit;
Timer timer;
int point=0;
static   StyledDocument doc;
 static   JTextPane textpane;
 String[] arr={"Tes"," hiiii"," what"," happpn"};
public int i=0;
long[] a=new long[4];
public Reminder() {

    a[0]=1000;
    a[1]=3000;
    a[2]=1000;
   a[3]=5000;
    timer = new Timer();

  timer.schedule(new RemindTask(),0,  a[i]);

 }

 class RemindTask extends TimerTask  {

    public void run() {

  point =point +arr[i].length();

     doc.setCharacterAttributes(0,point+1, textpane.getStyle("Red"), true);
     i++;


    }
  }
 public static void newcompo()
{

    JPanel panel = new JPanel();
    doc = (StyledDocument) new DefaultStyledDocument();
  textpane = new JTextPane(doc);
    textpane.setText("Test hiiii what happpn");
    javax.swing.text.Style style = textpane.addStyle("Red", null);
    StyleConstants.setForeground(style, Color.RED);
           panel.add(textpane);
    frame.add(panel);
    frame.pack();

 }
  public static void main(String args[]) throws InterruptedException,   InvocationTargetException {
      SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setVisible(true);

           newcompo();
        }
    });



 Reminder aa=  new Reminder();

  }
 }

最佳答案

使用 Swing 时,最好使用 javax.swing.Timer 而不是 javax.util.Timer。这将为您提供 setDelay 方法:

timer = new Timer(0, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        if (i > a.length) { // check when to stop
            timer.stop();
            return;
        }

        point = point + arr[i].length();
        doc.setCharacterAttributes(0, point + 1, textpane.getStyle("Red"), true);
        i++;

        // Change delay period
        timer.setDelay(a[i]);
    }
});
timer.setDelay(a[0]);
timer.start();

这将要求您更改延迟数组a的类型,从

long[] a = new long[4];

对此:

int[] a = new int[4];

关于java - TimerTask 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14183734/

相关文章:

java - 让程序运行5分钟

java - 如何删除 char 数组中的空白位置?

Java - 调用方法中的累积总计 - 按值/引用调用

java - JLabel 在 JFrame 中不显示任何内容

java - 使用自定义 Swing 组件进行文本换行的 JOptionPane

C# 如何在一天中的特定时间触发事件?

java - 用Java模拟cURL

java - jar 在 Windows 上运行但在 Ubuntu 上不运行

java - JTable - 触发列数据更改事件

javascript - vue js Prop 重置计时器