SWING产生声音

package test;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import java.io.*;
import java.net.MalformedURLException;

public class TestMusic {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  try {
      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
          if ("Nimbus".equals(info.getName())) {
              UIManager.setLookAndFeel(info.getClassName());
              break;
          }
      }
  } catch (Exception e) {
   JOptionPane.showMessageDialog(null, "未找到新皮肤,请升级JDK到6.0 update 10");
  }
  new Frame1();
 }

}

class Frame1 extends JFrame{
 JButton btnPlay;
 JTextField txtNum;
 JLabel lblMsg;
 public Frame1(){
  this.setBounds(200,100,400,300);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.setLayout(new FlowLayout());
  
  lblMsg = new JLabel("请输入数字:");
  this.add(lblMsg);
  txtNum = new JTextField(10);
  this.add(txtNum);
  btnPlay = new JButton("播放");
  btnPlay.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent arg0) {
//    File f = new File("E:/s1009/素材/声音/start.wav");
//    try {
//     AudioClip music = Applet.newAudioClip(f.toURL());
//     music.play();
//    } catch (MalformedURLException e) {
//     // TODO Auto-generated catch block
//     e.printStackTrace();
//    }
    int number = Integer.parseInt(txtNum.getText());
    readNumber(number);
    
   }});
  this.add(btnPlay);
  
  this.setVisible(true);
 }
 
 //播放指定的音频文件
 private void readSingleNumber(String fileName){
  File f = new File(fileName);
  try {
   AudioClip ac = Applet.newAudioClip(f.toURL());
   ac.play();
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 //对整个数字进行拆解
 private void readNumber(int number){
  int qian = number/1000;//获得千位的数字
  int bai = number/100;//获得百位的数字
  int shi = number/10;//获得十位的数字
  int ge = number;//获得个位的数字
  int numberLength = String.valueOf(number).length();
  //千
  switch(numberLength){
  case 4:
   //千
   if(qian!=0){
    readSingleNumber("sounds/" + String.valueOf(qian) + ".wav");
    try {
     Thread.sleep(600);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    readSingleNumber("sounds/千.wav");
    try {
     Thread.sleep(600);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  case 3:
//   百
   if(qian!=0 && bai==0 && shi==0 && ge==0){}
   else if(bai==0 && qian!=0){
    readSingleNumber("sounds/0.wav");
    try {
     Thread.sleep(600);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   else{
    readSingleNumber("sounds/" + String.valueOf(bai) + ".wav");
    try {
     Thread.sleep(600);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    readSingleNumber("sounds/百.wav");
    try {
     Thread.sleep(600);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  case 2:
//   十
   if(bai!=0 && shi==0 && ge==0){}  
   else if(shi==0 && bai!=0){
    readSingleNumber("sounds/0.wav");
    try {
     Thread.sleep(600);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   else if(shi==0 && bai==0){}
   else{
    readSingleNumber("sounds/" + String.valueOf(shi) + ".wav");
    try {
     Thread.sleep(600);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    readSingleNumber("sounds/十.wav");
    try {
     Thread.sleep(600);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  case 1:
//   个
   if(ge!=0){
    readSingleNumber("sounds/" + String.valueOf(ge) + ".wav");
    try {
     Thread.sleep(600);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  readSingleNumber("sounds/元.wav");
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值