我的程序非常简单 - 它会生成一个随机数,然后在该索引处增加一个数组,基本上计算它出现的频率,它显示在条形图类型JPanel中。它每隔“updateInterval”毫秒执行此操作,默认情况下为50。我试图在用户输入时更改此间隔。我的初始代码是:
Timer t = new Timer();
GenerateNumberTask genTask = new GenerateNumberTask(); //extends TimerTask
public void startTimer(){
t.scheduleAtFixedRate(genTask, 0, updateInterval);
}
出于显而易见的原因,更改'updateInterval'一旦开始就不会改变使用的间隔。我以为之后在所需的区域调用下一个代码(它正在改变的ActionListener按钮)将使用新的间隔更新计时器,但事实并非如此。
t.purge();
genTask = new GenerateNumberTask();
t.startTimer();
之后,我调查了ScheduledExecutorService,但这让我更加困惑。
接下来的事情似乎是创建一个新的Thread(),将该任务作为Runnable运行,并将Thread.sleep作为间隔运行。
正确的方法是什么?
编辑:根据@MadProgrammer的建议,我已将其更改为摇摆计时器,但问题仍然存在。public void startTimer(){
t = new Timer(timeBetweenUpdates, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//irrelevant code
}
});
t.start();
}
稍后,在actionlistener
中//timeBetweenUpdates changes before this line
t.setDelay(timeBetweenUpdates);
仍然,结果一样。
编辑2:解决了它。我不小心在构造函数和main方法中调用了startTimer()。
答案 0 :(得分:1)
您应该使用Swing(javax.swing
)Timer
,这将设置一个常规回调,该回调使用事件调度线程的上下文执行,从而可以安全地从内部更新UI
有关详细信息,请参阅Concurrency in Swing和How to use Swing Timers
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestTimer {
public static void main(String[] args) {
new TestTimer();
}
public TestTimer() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
TestPane testPane = new TestPane();
Timer timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
testPane.repaint();
}
});
JSlider slider = new JSlider(10, 1000);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
timer.setDelay(slider.getValue());
}
});
slider.setValue(10);
timer.start();
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(testPane);
frame.add(slider, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private long lastUpdate = -1;
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
String text = "...";
if (lastUpdate > 0) {
long duration = System.currentTimeMillis() - lastUpdate;
text = Long.toString(duration);
}
FontMetrics fm = g2d.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
g2d.drawString(text, x, y);
lastUpdate = System.currentTimeMillis();
g2d.dispose();
}
}
}