首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

jframe中加载音乐文件的有关问题

2012-02-20 
jframe中加载音乐文件的问题.请问怎么在JFrame中加载音乐文件.写了一个,不知怎么改,请指教.谢谢.packagegr

jframe中加载音乐文件的问题.
请问怎么在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();
                }
        }
}



[解决办法]
String str_= "file: " + str_utl+ "\\ "+ "love is blue_e ";//此处加载上音乐文件

试试

热点排行