且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

一个简单的播放音乐片段的 Applet小程序

更新时间:2022-08-13 08:26:24

 

import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
import java.applet.Applet ;
import  java.net.* ;
import java.applet.AudioClip ;
public  class PlaySound  extends Applet
{
 private  JButton  p1,p2,p3 ;
 private  JComboBox  com ;
 private  String song[]={"1.wav","2.wav","2.wav","4.wav"} ;
 JPanel  p =new JPanel() ;
 String url ="1.wav" ;
 AudioClip au ;
 public  void  init()
 {
  p1=new JButton("播放") ;
  p2=new JButton("循环") ;
  p3=new JButton("停止") ;
  com=new JComboBox(song) ;
  add(com) ;
  add(p1);
  add(p2);
  add(p3);
  com.addItemListener(new ItemListener()
  {
   public  void itemStateChanged(ItemEvent e)
  {
   url=(String)com.getSelectedItem()  ; 
   au.stop() ;
    try
{
 au=getAudioClip(new URL(getCodeBase()+url));
}
catch(Exception e1)
{
}

  }
  }
  ) ;
  
  p1.addActionListener(new ActionListener()
  {
  public  void actionPerformed(ActionEvent e)
 {
       au.play() ;   
 }

 }
  );
   p2.addActionListener(new ActionListener()
  {
  public  void actionPerformed(ActionEvent e)
 {
       au.loop() ;   
 }

 }
  );
   p3.addActionListener(new ActionListener()
  {
  public  void actionPerformed(ActionEvent e)
 {
       au.stop() ;   
 }

 }
  );
 try
 {
 au=getAudioClip(new URL(this.getCodeBase()+url));
}
catch(Exception e)
{  
}
 }
 
 

 
 
}