/*ch0912.java*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ch0912 extends Applet implements ActionListener,Runnable
{
  AudioClip clip;
  Button b1,b2,b3;
  Thread thread;
  TextField text;
  Date date;
  GregorianCalendar calendar;
  String strDate,strTime,strStatus;
  String newmoth;
  int month,qq;
  Thread datimeThread;
  public void init()
  {
    thread=new Thread(this);
    thread.setPriority(Thread.MIN_PRIORITY);
    thread.start();
    new datetime();
    b1=new Button("播放");
    b2=new Button("循环播放");
    b3=new Button("停止");
    text=new TextField(15);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    add(b1);
    add(b2);
    add(b3);
    add(text);
  }

  public void stop()
  {
    clip.stop();          //离开页面时停止播放
  }
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==b1)
    {
      clip.play();
    }
    else if(e.getSource()==b2)
    {
      clip.loop();
    }
    else if(e.getSource()==b3)
    {
      clip.stop();
    }
  }

  public void run()
  { clip=getAudioClip(getCodeBase(),"liangzhu.mid");
    if(clip!=null)
    {
      b1.setBackground(Color.gray);
      text.setText("可以开始播放");
    }
  }
          
  class datetime extends Thread
  {
    datetime()
    {
      datimeThread=new Thread(this);
      datimeThread.start();
    }
    public void run()
    {
      while(datimeThread!=null)
      {
        displayDatetime();
        try
        {
          datimeThread.sleep(1000);
        }
        catch(InterruptedException e)
        {
          showStatus("Thread interrupted");
        }
      }
    }

    public void displayDatetime()
    {
      date=new Date();
      calendar=new GregorianCalendar();
      calendar.setTime(date);
      strTime=calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
      qq=calendar.get(Calendar.MONTH)+1;
      strDate=qq+"/"+calendar.get(Calendar.DATE)+"/"+calendar.get(Calendar.YEAR);
      strStatus=strTime+"     "+strDate;
      showStatus(strStatus);
    }
  }
}
