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