代码与问题如下
源代码的作用是为了定时轮流显示不同国家的国旗和国歌,不过还有一个组合框可以选择国家,还有两个按键暂停和启动国歌,但是每次自动换国歌或者选择组合框里的国家时,只有相应的国歌会变,国旗不会变,可是在国歌改变后改变窗口大小时国旗又会变成相应的,想请大神解决一下。
public class ImageAudioAnimation extends JApplet {
private final static int NUMBER_OF_NATIONS=7;
private final static String[] nations={"China","Denmark","Germany","India","Norway","UK","US"};
private int current=0;
private AudioClip[] audioClips=new AudioClip[NUMBER_OF_NATIONS];
private AudioClip currentAudioClip;
private ImagePanel imagePanel;
private ImagePanel[] imagePanels=new ImagePanel[NUMBER_OF_NATIONS];
private int[] delays={4800,54000,59000,54000,59000,31000,68000};//根据不同国歌的时间长短
private Timer timer=new Timer(delays[0],new ActionListener() {
public void actionPerformed(ActionEvent e) {
current=(current+1)%NUMBER_OF_NATIONS;
presentNation(current);
}
});
private JButton jbtResume=new JButton("Resume");
private JButton jbtSuspend=new JButton("Suspend");
private JComboBox<String> jcboNations=new JComboBox<String>(nations);
public ImageAudioAnimation() {
for(int i=0;i<NUMBER_OF_NATIONS;i++) {
imagePanels[i]=new ImagePanel(new ImageIcon(getClass().getResource(nations[i]+".gif")));
audioClips[i]=Applet.newAudioClip(this.getClass().getResource(nations[i]+".mid"));
}
JPanel panel=new JPanel();
panel.add(jbtResume);
panel.add(jbtSuspend);
panel.add(new JLabel("Select"));
panel.add(jcboNations);
add(panel,BorderLayout.SOUTH);
imagePanel=imagePanels[0];
add(imagePanel);
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();
currentAudioClip=audioClips[0];
currentAudioClip.play();
}
private void presentNation(int index) {
this.remove(imagePanel);
this.imagePanel=imagePanels[index];
this.add(imagePanel);
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();
}
protected class ImagePanel extends JPanel {
private Image image;
public ImagePanel(ImageIcon imageIcon) {
image=imageIcon.getImage();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(image!=null) {
g.drawImage(image, 0, 0, getWidth(),getHeight(),this);
}
}
}
}