关于JAVA 空指针,哪位朋友帮忙看看

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package imageaudioanimation;


/**
 *
 * @author Administrator
 */
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 =
    {48000, 54000, 59000, 54000, 59000, 31000, 68000};
  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[]
    {"Denmark", "Germany", "China", "India", "Norway", "UK", "US"});


  public ImageAudioAnimation() {
    // Load image icons and audio clips
    for (int i = 0; i < NUMBER_OF_NATIONS; i++) {
      icons[i] = new ImageIcon(getClass().getResource(
        "/image/flag" + i + ".gif"));
      audioClips[i] = Applet.newAudioClip(
        getClass().getResource("/audio/anthem" + i + ".mid"));
    }


    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();
  }


  /** Main method */
  public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame("ImageAudioAnimation");


    // Create an instance of the applet
    ImageAudioAnimation applet = new ImageAudioAnimation();
    applet.init();


    // Add the applet instance to the frame
    frame.getContentPane().add(applet, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    // Display the frame
    frame.setSize(400, 300);
    frame.setVisible(true);
  }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值