代码如下:
Timer timer = new Timer(1000,new TimerListener());
timer.setDelay(20000);
我在重新设置完了延迟时间20000后为什么这个timer对象产生事件的时间间隔还是1000ms啊,在设置完20000ms这个新的延迟时间后,我用timer.getDelay()查看新的延迟时间的确是20000,那为什么这个timer产生事件的时间间隔还是1000ms啊。

解决方案 »

  1.   

    那我用timer.setDelay(20000);设置的不也是延时时间吗
      

  2.   

    既然timer.setDelay(20000);设置的也是延时时间,那么为什么程序还是按照最初设定的1000ms产生延时呢
      

  3.   

    为什么我不会出现这种情况呢?!public class TimerTest extends JFrame {
    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);
    }
    }
      

  4.   

    setDelay确实是延时,
    不过调整首次延时需要用setInitialDelay
      

  5.   

    请看完整程序,这个程序,在每次定时器产生事件并用监听器处理这个事件时,监听器都会重新设置新的delay,可是为什么我这个程序的delay却从来没有改变过呢 ,每次计时间隔都是一样的,延时都是第一次在初始化timer时的那个时间间隔import java.awt.*;
    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.   

    为什么总是要修改延时呢?
    我6楼所说的setInitialDelay楼主可以加进去看看。
      

  7.   

    我自己编译了一下怎么可以啊?用setDelay是可以的啊~还有楼主这个简单的程序为什么还要弄个对话框啊~