利用Applet中的AudioClip播放声音class SoundThread implements Runnable{
private AudioClip m = null;
private URL url = null;
private boolean isRun = true; public SoundThread() {
url = this.getClass().getResource("/folder.wav");
try {
m = Applet.newAudioClip(url);
} catch (Exception e) {
e.printStackTrace();
}
} public void stop(){
isRun = false;
m.stop();
} public void run() {
long time = System.currentTimeMillis();
long lasttime = time + 3 * 1000;
while (time < lasttime && isRun) {
m.play();
time = System.currentTimeMillis();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}
public static void main(String[] args){
SoundThread st = new SoundThread();
new Thread(st).start();
}
}
private AudioClip m = null;
private URL url = null;
private boolean isRun = true; public SoundThread() {
url = this.getClass().getResource("/folder.wav");
try {
m = Applet.newAudioClip(url);
} catch (Exception e) {
e.printStackTrace();
}
} public void stop(){
isRun = false;
m.stop();
} public void run() {
long time = System.currentTimeMillis();
long lasttime = time + 3 * 1000;
while (time < lasttime && isRun) {
m.play();
time = System.currentTimeMillis();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}
public static void main(String[] args){
SoundThread st = new SoundThread();
new Thread(st).start();
}
}
《Swing Hacks》,
public void paint(Graphics g)
{
int cw = getWidth();
int ch = getHeight();
URL url = getClass().getResource("about.jpg");
ImageIcon scaledSymbol = new ImageIcon(new ImageIcon(url).getImage()
.getScaledInstance(cw, ch, Image.SCALE_SMOOTH));
g.drawImage(scaledSymbol.getImage(), 0, 0, this);
}
声音
public void mousePressed(MouseEvent e)
{ URL url = this.getClass().getResource("beep.au");
try
{
FileInputStream fileau = new FileInputStream(
new File(url.getFile()));
AudioStream as = new AudioStream(fileau);
AudioPlayer.player.start(as);
}
catch (Exception ex)
{
ex.printStackTrace();
}
.......
}能凑合着用,不是很完美。
JLabel myImg = new JLabel(new ImageIcon(this.getClass().getResource("URL")));
至于添加背景音乐,在Eclipse中好像不行,需要另外下载包.