请问怎么在JFrame中加载音乐文件.
写了一个,不知怎么改,请指教.谢谢.
package graphics_test;/********************************
* author @scoladi

* Graphics_JFrame_test Graphics_test.java;

*******************************/import java.awt.*;
import java.applet.*;
import java.io.*;//用来找那个文件路径的
import javax.swing.*;import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
public class Graphics_test extends JFrame{
    JPanel_t jp;
    Graphics_test()
    {
        try
        {
            jp=new JPanel_t();
        }
        catch(MalformedURLException m)
        {
            JOptionPane.showMessageDialog(null, "URL出错,不得不退出.");//程序进入这里了.
            try{              /*********************/
                Thread.sleep(200);//用于控制其退出时间的.
            }                 /********************/
            catch(InterruptedException ine)//此处不能重名呀
            {
                JOptionPane.showMessageDialog(null, "线程控制出错,不得不退出!");
                System.exit(0);
            }
            dispose();
            System.exit(0);
        }
        Container conx = this.getContentPane();
                                       /*****************************/
        conx.setLayout(new BorderLayout());//目地是把jpanel加载到整个框架之中
                                       /*****************************/
        conx.add(jp,BorderLayout.CENTER);
    }
    public static void main(String[] args){
        new Graphics_test();
    }
                             /***************************/
    class JPanel_t extends JPanel//在此处加载音乐,图片.
    {                        /**************************/
        AudioClip clip;               /****************************/
        JPanel_t() throws MalformedURLException//在此处进行异常声音,原来其的定义是有异常产生的.
        {                             /***************************/
            String str_utl;
            str_utl=System.getProperty("user.dir");//查找到工程文件夹的位置
            System.out.println(str_utl);
            System.out.println(System.getProperty("user.dir"));
            String str_=str_utl+"\\"+"love is blue_e";//此处加载上音乐文件
            System.out.println(str_);
                                /***************/
            URL utl_=new URL(str_);//此处出现了问题.执行到这一步,就会被捕获.弹出 URL出错,不得不退出对话框
                                /**************/
            System.out.println("测试:进行资源链接地址之后.");
                                            /*************************/
            clip=java.applet.Applet.newAudioClip(utl_);//*1.采用这种方式把音乐加载进来,因为其是静态类
                                                       //2. 文件路径问题
                                            /*************************/
            clip.play();
        }
    }
}

解决方案 »

  1.   

    String str_="file:" + str_utl+"\\"+"love is blue_e";//此处加载上音乐文件试试
      

  2.   

    package graphics_test;/********************************    
     * author @scoladi
     * 
     * Graphics_JFrame_test Graphics_test.java;
     * 
     *******************************/import java.awt.*;
    import java.applet.*;
    import java.io.*;//用来找那个文件路径的
    import javax.swing.*;
    import sun.audio.*;//此是为什么呢.
    import java.awt.event.*;
    import java.net.URL;
    import java.net.MalformedURLException;
    public class Graphics_test extends JFrame{
    JPanel_t jp;
    Graphics_test()
    {
    try
    {
    jp=new JPanel_t();
    }
    catch(MalformedURLException m)
    {
    JOptionPane.showMessageDialog(null, "URL出错,不得不退出.");
    try{              /*********************/
    Thread.sleep(200);//用于控制其退出时间的.
    }                 /********************/
    catch(InterruptedException ine)//此处不能重名呀
    {
    JOptionPane.showMessageDialog(null, "线程控制出错,不得不退出!");
    System.exit(0);
    }
    dispose();
    System.exit(0);
    }
    Container conx = this.getContentPane();
                                   /*****************************/
            conx.setLayout(new BorderLayout());//目地是把jpanel加载到整个框架之中
                                           /*****************************/
            conx.add(jp,BorderLayout.CENTER);
    }
    public static void main(String[] args){
    new Graphics_test();
    }
                             /***************************/
    class JPanel_t extends JPanel//在此处加载音乐,图片.
    {                        /**************************/
    AudioClip clip;
    FileInputStream fis;
    AudioStream audioStream;
                                  /****************************/
    JPanel_t() throws MalformedURLException//在此处进行异常声音,原来其的定义是有异常产生的.
    {                             /***************************/
    String str_utl;
    str_utl=System.getProperty("user.dir");//查找到工程文件夹的位置
    System.out.println(str_utl);
    System.out.println(System.getProperty("user.dir"));/*****************************/
    String str_="file:"+str_utl+"\\"+"love is blue_e.wav";//"file:"不能忘记是关键之处.
    System.out.println(str_);                          /*****************************/
    //URL utl=new URL("file:"+str_utl);
                        /***************/
    URL utl_ =new File("love is blue_e.wav").toURI().toURL();//此处出现了问题.
                        /**************/
    System.out.println("测试:进行资源链接地址之后.");
                                    /*************************/
    clip=java.applet.Applet.newAudioClip(utl_);//*1.采用这种方式把音乐加载进来,因为其是静态类
                                               //2. 文件路径问题
                                    /*************************/
    if(clip!=null)
    {               /*************************/
    clip.loop();//测试均能通过,为什么就是没有声音呢.
                /*************************/
    JOptionPane.showMessageDialog(null, "执行音乐文件.");
    }
    else
    {
    JOptionPane.showMessageDialog(null, "音乐文件加载错误.");
    }
    System.out.println("程序结束:测试.");
    }
    }
    }
    没有声音,请帮一下.谢谢.