大家帮我看一下这个程序,在每次定时器产生时间并用监听器处理这个事件时,监听器都会重新设置新的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();
}
}
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();
}
}
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了这两个方法。
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已经被更新了,但是问题是为什么计时器还是按照第一次设置的频率产生事件呢。
是的话,没必要使用 Timer,AudioClip可以使用其loop方法循环播放。