大家帮我看一下这个程序,在每次定时器产生时间并用监听器处理这个事件时,监听器都会重新设置新的delay,可是为什么我这个程序的delay却从来没有改变过呢 ,每次计时间隔都是一样的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();
  }
}

解决方案 »

  1.   

    javax.swing.Timer 满足不了你的需要。
      

  2.   

    我看那教材上就是这么写的啊。实在找不出这个bug在哪里了
      

  3.   

    Applet 的 start/stop 方法:void  start()
    Called by the browser or applet viewer to inform this applet that it should start its execution.
    void  stop()
    Called by the browser or applet viewer to inform this applet that it should stop its execution.这里你不留神override了这两个方法。
      

  4.   

    我覆盖这两个方法是为让browser去响应我的最小化和最大化激活窗口的工作啊
      

  5.   

      private void presentNation(int index) {
      jlblImageLabel.setIcon(icons[index]);
      jcboNations.setSelectedIndex(index);
      currentAudioClip = audioClips[index];
      currentAudioClip.play();
      timer.setDelay(delays[index]);
    System.out.println("delay" + timer.getDelay());
      }我在  timer.setDelay(delays[index]);语句后加了System.out.println("delay" + timer.getDelay());
    结果显示delay已经被更新了,但是问题是为什么计时器还是按照第一次设置的频率产生事件呢。
      

  6.   

    你wav声频文件的长度是多少秒(分)?
      

  7.   

    这个程序的问题就是每一个音频播放的事件都是 private Timer timer = new Timer(delays[0],new TimerListener());这条语句创建timer对象的时候设置的时间delays[0] == 80000
      

  8.   

    你使用 javax.swing.Timer 是不是为了循环播放?
    是的话,没必要使用 Timer,AudioClip可以使用其loop方法循环播放。