做了个窗口,想在里面添加背景音乐,请问怎么做?急救!

解决方案 »

  1.   

    /******************************************************************
     * Beginning Java 5 Game Programming
     * by Jonathan S. Harbour
     * PlayMusic program
     ******************************************************************/import java.awt.*;
    import java.applet.*;
    import java.io.*;
    import java.net.*;
    import javax.sound.midi.*;public class PlayMusic extends Applet {    String filename = "titlemusic.mid";
        Sequence song;    private URL getURL(String filename) {
            URL url = null;
            try {
                url = new java.net.URL(getCodeBase() + filename);
            }
            catch (MalformedURLException e) { e.printStackTrace(); }
            return url;
        }    //initialize the applet
        public void init() {
            try {
                song = MidiSystem.getSequence(getURL(filename));
                Sequencer sequencer = MidiSystem.getSequencer();
                sequencer.setSequence(song);
                sequencer.open();
                sequencer.start();        } catch (InvalidMidiDataException e) {
            } catch (MidiUnavailableException e) {
            } catch (IOException e) { }
        }    //repaint the applet window
        public void paint(Graphics g) {
            int x=10, y = 1;
            if (song != null) {
                g.drawString("Midi File: " + filename, x, 15 * y++);
                g.drawString("Resolution: " + song.getResolution(), x, 15 * y++);
                g.drawString("Tick length: " + song.getTickLength(), x, 15 * y++);
                g.drawString("Tracks: " + song.getTracks().length, x, 15 * y++);
                g.drawString("Patches: " + song.getPatchList().length, x, 15 * y++);
            } else {
                g.drawString("Error loading sequence file " + filename, 10, 15);
            }
        }}这是一本JAVA游戏编程书上的源代码,可以载入MIDI文件。public void paint 方法,楼主应该不用,自己删掉吧。