我试图让音乐在 JApplet 的背景中播放。
小程序本身工作得很好,但我听不到任何音乐。
我想知道它是否必须与文件为 mp3 相关。
//AnimationDemo1.java
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.applet.*;
public class Developers extends JApplet
{
private AudioClip backgroundmusic;
public void init()
{
URL urlformusic = getClass().getResource("audio/song1.mp3");
backgroundmusic = Applet.newAudioClip(urlformusic);
backgroundmusic.loop();
add(new DevelopersPanel());
}
public void start() {
backgroundmusic.loop();
}
public void stop(){
backgroundmusic.stop();
}
public void destroy() {
backgroundmusic.stop();
}
}// end of class of extended JApplet
class DevelopersPanel extends JPanel
{
private int numImages = 3;
private ImageIcon[] loop = new ImageIcon[numImages];
private String[] description = new String[3];
private int currentImage = 0;
public DevelopersPanel()
{
description[0] = "Charlie Brown works at Charleston Restraunt" +
"as a Shift Leader, Server, and Classroom Trainer.";
description[1] = "Snoopy, well he just does his own thing.";
description[2] = "Lucy helped keep everyone working on the project sane.";
for(int x = 0;x<loop.length;x++)
{
URL url = this.getClass().getResource("image/pic" + x + ".jpg");
loop[x] = new ImageIcon(url);
}
} //end of constructor
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Dimension d = getSize();
g.drawImage(loop[currentImage].getImage(), 10, 10,d.width/2, d.height/2, this);
g.drawString(description[currentImage],d.width-d.width+20, d.height-20);
currentImage = (currentImage + 1) % numImages;
try{
Thread.sleep(3000);
}
catch(InterruptedException e){
}
repaint();
}
} //end of extended JPanel class
任何帮助将不胜感激。
我还是java新手,请保持简单。