TimerTask问题 [英] Issue with TimerTask
本文介绍了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 ++
(指向数组的指针)时,计时不会改变;随后的延迟率与第一个延迟值相同。
I want delay to be changed after each task,so the timings are stored in an array.
When i++
is preformed(pointer to array),the timings are not changed;the subsequent rate of delay is same as the first delay value.Why it dosn't change?
编辑:
如果需要,这里有一个SSCCE: p>
Here's an SSCCE if required:
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.util.Timer
上使用 javax.swing.Timer
。这将为您提供 setDelay
方法:
When using Swing, better to use javax.swing.Timer
over javax.util.Timer
. That will give you the setDelay
method:
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的类型
,来自
This will required you to change the type of your delay array a
, from
long[] a = new long[4];
为此:
int[] a = new int[4];
这篇关于TimerTask问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文