当前位置:编程学习 > JAVA >>

这个添加按键音乐的代码错在哪里?怎么没声音?(红色字体部分)

import javax.swing.*; 
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import java.applet.*;
import java.net.*;
public class MemoryTestArea extends JPanel implements ActionListener,Runnable {   
                              //实现ActionListener和,Runnable接口,所创建的对象
  int row,col;
  File gradeFile; 
  ArrayList<Block> allBlockList;      //数组表allBlockList 每个单元存放一个Block对象                     
  String  imageFileName[];            //字符串数组**中每个单元是一幅图像文件的名字
  LinkedList<ImageIcon> openIconList;             
  LinkedList<Block>    openBlockList;
  //java.util 的 类 LinkedList<E> 链接列表   主要方法为  
  //add(E e) 将指定元素添加到此列表的结尾。   
  int success=0;                       //用户找到的具有同样的方块的个数
  Thread hintThead;                    //用来提示测试区中的Block对象
  JButton hintButton,pauze,kk,restart; //声明各个按钮
  int usedTime=0;
  JTextField showUsedTime,hintMessage;
  javax.swing.Timer timer;
  Record record;
  JPanel center,south,north;
  File musicFile;
  URI uri;
  URL url;
  AudioClip clip;
  Boolean  timerisstart=false;        //定义一个布尔值来判断计时是否开始
  MemoryTestArea(){
     setLayout(new BorderLayout());   //布局设置
     allBlockList=new ArrayList<Block>(); 
     openIconList=new LinkedList<ImageIcon>();
     openBlockList=new LinkedList<Block>();
     hintThead=new Thread(this);
     hintMessage=new JTextField();
     hintMessage.setHorizontalAlignment(JTextField.CENTER);    //设置文本对齐方式
     hintMessage.setEditable(false);                           //不可编辑
     hintMessage.setFont(new Font("宋体",Font.BOLD,18));       //字体设置
                                  //设置三个容器
     center=new JPanel();
     south=new JPanel();
     north=new JPanel();
     hintButton=new JButton("提示");
     
     pauze=new JButton("暂停游戏"); 
     kk=new JButton("继续游戏");
     restart=new JButton("重新开始");
     hintButton.addActionListener(this);
     kk.addActionListener(this);            //为继续游戏按钮添加事件监听
     
     pauze.addActionListener(this);         //为暂停游戏按钮添加事件监听
     
     restart.addActionListener(this);
     
     showUsedTime=new JTextField(8);
     showUsedTime.setEditable(false);
     showUsedTime.setHorizontalAlignment(JTextField.CENTER);
     
     south.add(new JLabel("用时:")) ;            //在容器中添加标签
     south.add(showUsedTime);
     south.add(new JLabel("提示图标位置(导致次数增加):")) ;
     south.add(hintButton);
     
     north.add(pauze);
     north.add(kk);
     north.add(restart);
     north.add(hintMessage);
     
     add(south,BorderLayout.SOUTH);
     
     add(north,BorderLayout.NORTH);
     
     timer=new javax.swing.Timer(1000,this); 
     record=new Record();    // 创建record文件,当用户成功完成时弹出对话框,写入record 
 //保存用户 游戏的游戏记录
  } 
  public void initBlock(int m,int n,String name[],File f){
     row=m;
     col=n;
     gradeFile=f;
     center.removeAll();
     imageFileName=name;
     ImageIcon icon[]=new ImageIcon[imageFileName.length];
     for(int i=0;i<icon.length;i++){
        icon[i]=new ImageIcon(imageFileName[i]); 
     } 
     if(allBlockList.isEmpty()){
          for(int i=0;i<row*col;i++){
             allBlockList.add(new Block());
          }
     }
     else{
           allBlockList.clear(); 
           for(int i=0;i<row*col;i++){
             allBlockList.add(new Block());
           }
     }
     for(int i=0;i<allBlockList.size();i++){
        allBlockList.get(i).addActionListener(this);
        ////get()方法:get(int index)返回此列表中指定位置处的元素。
        allBlockList.get(i).setOpenStateIcon(icon[i%row]); 
/*调用 Block 的方法  加载图片:      void setOpenStateIcon(ImageIcon icon) 
                    {
               openStateIcon=icon;
                    }
*/

     } 
     Collections.shuffle(allBlockList);             //随机排列allBlockList中的节点        
     center.setLayout(new GridLayout(row,col));
     for(int i=0;i<allBlockList.size();i++){
        center.add(allBlockList.get(i));
     }
     add(center,BorderLayout.CENTER);
     if(timer.isRunning()){
         timer.stop();
     }
     hintMessage.setText("您需要用鼠标单击出"+col+"个同样图标的方块");
     usedTime=0;
     showUsedTime.setText(null);
     validate(); 
  }
  public void setImageName(String name[]){
     imageFileName=name;
  }
  public void actionPerformed(ActionEvent e){                               
ImageIcon icon[]=new ImageIcon[imageFileName.length];
  if(e.getSource()==pauze){
  timer.stop();
  for(int i=0;i<allBlockList.size();i++)           
      {
        allBlockList.get(i).setEnabled(false);
      }
  }
  if(e.getSource()==kk){
  timer.start();                                   
      timerisstart=true;
      for(int i=0;i<allBlockList.size();i++)           
      {
        allBlockList.get(i).setEnabled(true);
      }
  }
  
  if(e.getSource()==restart){                     //设置重新开始的按钮。
  for(int i=0;i<allBlockList.size();i++)           
    {
      allBlockList.get(i).setOpenStateIcon(icon[i%row]);  
Collections.shuffle(allBlockList);          //使用默认随机源对指定列表进行置换
  }
  usedTime=0;
  timer.stop(); 
  hintMessage.setText("请点击等级选择开始游戏!");
      timerisstart=false;
      for(int i=0;i<allBlockList.size();i++)           
      {
        allBlockList.get(i).setEnabled(true);
      }
   }

    if(e.getSource() instanceof Block)  
    //它的作用是测试它左边的对象是否是它右边的类的实例,返回boolean类型的数据
    ////判断是否为 Block 对象;
    {
       if(!timer.isRunning())
           timer.start();
        Block block=(Block)e.getSource();
        ImageIcon openStateIcon=block.getOpenStateIcon();
        block.setIcon(openStateIcon);
        if(openIconList.size()==0) {                                 
          openIconList.add(openStateIcon);
          openBlockList.add(block);
          success=1;                                            
        }
        else{
           ImageIcon temp=openIconList.getLast();        
           if(temp==openStateIcon&&!(openBlockList.contains(block))){  
           
           /*判断表示"翻开的图片和第一个翻开的图片相同,而且这个翻开的图片不是已经翻开的图片
            这也就是要找出的"用户成功翻开相同图标方块"这个行为的判断;*/
           success=success+1; 
/////////////////////////成功翻开图片时播放音乐///////////////////////////////
     try{musicFile=new File("1.wav");
         uri=musicFile.toURI();
         url=uri.toURL();
         clip=Applet.newAudioClip(url);
         }
         
      catch(Exception ee){}
      clip.play();
////////////////////////成功翻开图片时播放音乐///////////////////////////////
               openIconList.add(openStateIcon);                     
               openBlockList.add(block);
                if(success==col){ 
                     for(int i=0;i<allBlockList.size();i++){           
                        allBlockList.get(i).setEnabled(false);
                     }
                     for(int j=0;j<openBlockList.size();j++){
                        Block b=openBlockList.get(j);
                        b.setDisabledIcon(b.getOpenStateIcon());  //设置对象b在不能激活状态下的图标
                     } 
                     timer.stop();
                     record.setTime(usedTime);
                     record.setGradeFile(gradeFile);
                     record.setVisible(true);
                  }    
           }
           else if((temp!=openStateIcon)&&(!(openBlockList.contains(block)))){ 
           
           /*判断表示"翻开的图片和第一个翻开的图片不同,而且这个翻开的图片不是已经翻开的图片
            这也就是要找出的"用户翻开不相同图标方块后"这个行为的判断。*/
                openIconList.clear();                          
                openBlockList.clear();
                openIconList.add(openStateIcon);                
                openBlockList.add(block);
                success=1; 
usedTime=usedTime+10;
////////////////////////失败翻开图片时播放音乐////////////////////////////
       try{musicFile=new File("qq.wav");
           uri=musicFile.toURI();
           url=uri.toURL();
           clip=Applet.newAudioClip(url);
         }
      catch(Exception ee){}
             clip.play();
////////////////////////失败翻开图片时播放音乐////////////////////////////
                for(int i=0;i<allBlockList.size();i++){            
                   if(allBlockList.get(i)!=block)
                      allBlockList.get(i).setIcon(null);
                }                
           }  
        }
    }
   if(e.getSource()==hintButton){ 
       if(!hintThead.isAlive())
           hintThead=new Thread(this);
       for(int i=0;i<allBlockList.size();i++)           
           allBlockList.get(i).removeActionListener(this);
       usedTime=usedTime+10;
       try{
           hintThead.start();  
       }
       catch(IllegalThreadStateException ex){}
    }
   if(e.getSource()==timer){ 
       usedTime++;
       showUsedTime.setText("您的用时:"+usedTime+"秒");  
    }
  }
  public void run(){  //实现Runnable接口的方法
     for(int i=0;i<allBlockList.size();i++)           
        allBlockList.get(i).setIcon(allBlockList.get(i).getOpenStateIcon());
     try{ Thread.sleep(1200);
     }
     catch(InterruptedException exp){}
     for(int i=0;i<allBlockList.size();i++)           
        allBlockList.get(i).addActionListener(this);
     for(int i=0;i<allBlockList.size();i++)
       if(!openBlockList.contains(allBlockList.get(i)))           
         allBlockList.get(i).setIcon(null);
  }
}
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,