代码如下:
Timer timer = new Timer(1000,new TimerListener());
timer.setDelay(20000);
我在重新设置完了延迟时间20000后为什么这个timer对象产生事件的时间间隔还是1000ms啊,在设置完20000ms这个新的延迟时间后,我用timer.getDelay()查看新的延迟时间的确是20000,那为什么这个timer产生事件的时间间隔还是1000ms啊。
Timer timer = new Timer(1000,new TimerListener());
timer.setDelay(20000);
我在重新设置完了延迟时间20000后为什么这个timer对象产生事件的时间间隔还是1000ms啊,在设置完20000ms这个新的延迟时间后,我用timer.getDelay()查看新的延迟时间的确是20000,那为什么这个timer产生事件的时间间隔还是1000ms啊。
public static void main(String[] args) {
new TimerTest();
} public TimerTest() {
this.setBounds(300, 300, 300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
Timer timer = new Timer(1000, new TestActionListener());
timer.setDelay(2000);
timer.start();
System.out.println("start : " + System.currentTimeMillis());
}
}class TestActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
long now = System.currentTimeMillis();
System.out.println(now);
}
}
不过调整首次延时需要用setInitialDelay
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;public class ImageAudioAnimation extends JApplet { private final static int NUMBER_OF_NATIONS = 7;
private int current = 0;
private ImageIcon[] icons = new ImageIcon[NUMBER_OF_NATIONS];
private AudioClip[] audioClips = new AudioClip[NUMBER_OF_NATIONS];
private AudioClip currentAudioClip; private int[] delays = {80000,84000,43000,111000,53000,49000,83000};
private Timer timer = new Timer(delays[0],new TimerListener()); private JLabel jlblImageLabel = new JLabel();
private JButton jbtResume = new JButton("Resume");
private JButton jbtSuspend= new JButton("Suspend");
private JComboBox jcboNations = new JComboBox(new Object[] {"Den","Germany","China","India","Norway","UK","US"});
public ImageAudioAnimation() {
for(int i = 0;i < NUMBER_OF_NATIONS;i++) {
icons[i] = new ImageIcon(getClass().getResource("image/flag" + i + ".png"));
audioClips[i] = Applet.newAudioClip(getClass().getResource("audio/anthem" + i + ".wav"));
} JPanel panel = new JPanel();
panel.add(jbtResume);
panel.add(jbtSuspend);
panel.add(new JLabel("Select"));
panel.add(jcboNations);
add(jlblImageLabel,BorderLayout.CENTER);
add(panel,BorderLayout.SOUTH); jbtResume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start();
}
}); jbtSuspend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stop();
}
}); jcboNations.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stop();
current = jcboNations.getSelectedIndex();
presentNation(current);
timer.start();
}
}); timer.start();
jlblImageLabel.setIcon(icons[0]);
jlblImageLabel.setHorizontalAlignment(JLabel.CENTER);
currentAudioClip = audioClips[0];
currentAudioClip.play();
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
current = (current + 1) % NUMBER_OF_NATIONS;
presentNation(current);
}
} private void presentNation(int index) {
jlblImageLabel.setIcon(icons[index]);
jcboNations.setSelectedIndex(index);
currentAudioClip = audioClips[index];
currentAudioClip.play();
timer.setDelay(delays[index]);
} public void start() {
timer.start();
currentAudioClip.play();
} public void stop() {
timer.stop();
currentAudioClip.stop();
}
}
我6楼所说的setInitialDelay楼主可以加进去看看。