世界杯2018_世界杯北美区预选赛 - jmkxjj.com

java在窗口添加背景音

有两种方法:

方法一如下:

可以使用Java Swing里的JFrame类,在JFrame对象上添加一个JLabel,将背景音乐文件加载到JLabel上面就可以了。下面是一个例子:

import java.awt.BorderLayout;

import java.io.File;

import javax.swing.*;

public class MainFrame extends JFrame {

private JLabel bgLabel;

public MainFrame() {

// 设置窗口标题

setTitle("Java背景音乐");

// 设置窗口大小

setSize(300, 200);

// 设置窗口位置

setLocationRelativeTo(null);

// 设置窗口关闭操作

setDefaultCloseOperation(EXIT_ON_CLOSE);

// 创建背景标签

bgLabel = new JLabel(new ImageIcon("bg.jpg"));

// 加载背景音乐文件

File bgMusicFile = new File("bgMusic.wav");

if (bgMusicFile.exists()) {

AudioClip bgMusic = JApplet.newAudioClip(bgMusicFile.toURI().toURL());

bgMusic.loop();

}

// 添加背景标签到窗口

add(bgLabel, BorderLayout.CENTER);

}

public static void main(String[] args) {

MainFrame frame = new MainFrame();

frame.setVisible(true);

}

}

在这个例子中,我们将一个背景图片放在窗口中央的JLabel上,然后在程序启动时加载了一个背景音乐文件。注意,必须使用绝对路径加载文件,否则会出现找不到文件的异常。

方法二如下:

import com.brid.util工具类.Musicutil;

public class GameApp {

public static void main(String[] args) {

Musicutil music=new Musicutil("D:\\java\\小游戏\\src\\music/bei.wav");

music.start();

@SuppressWarnings("unused")

int musicOpenLab =1;

}

}

import java.io.File;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;

import javax.sound.sampled.AudioInputStream;

import javax.sound.sampled.AudioSystem;

import javax.sound.sampled.DataLine;

import javax.sound.sampled.FloatControl;

import javax.sound.sampled.LineUnavailableException;

import javax.sound.sampled.SourceDataLine;

import javax.sound.sampled.UnsupportedAudioFileException;

public class Musicutil extends Thread {

private String fileName;

private final int EXTERNAL_BUFFER_SIZE = 520888;

public Musicutil(String wavFile) {

this.fileName = wavFile;

}

@SuppressWarnings("unused")

public void run() {

File soundFile = new File(fileName); // 播放音乐的文件名

if (!soundFile.exists()) {

System.err.println("Wave file not found:" + fileName);

return;

}

while (true) { // 设置循环播放

AudioInputStream audioInputStream = null; // 创建音频输入流对象

try {

audioInputStream = AudioSystem.getAudioInputStream(soundFile); // 创建音频对象

} catch (UnsupportedAudioFileException e1) {

e1.printStackTrace();

return;

} catch (IOException e1) {

e1.printStackTrace();

return;

}

AudioFormat format = audioInputStream.getFormat(); // 音频格式

SourceDataLine auline = null; // 源数据线

DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

try {

auline = (SourceDataLine) AudioSystem.getLine(info);

auline.open(format);

} catch (LineUnavailableException e) {

e.printStackTrace();

return;

} catch (Exception e) {

e.printStackTrace();

return;

}

if (auline.isControlSupported(FloatControl.Type.PAN)) {

FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);

}

auline.start();

int nBytesRead = 0;

byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

try {

while (nBytesRead != -1) {

nBytesRead = audioInputStream.read(abData, 0, abData.length);

if (nBytesRead >= 0)

auline.write(abData, 0, nBytesRead);

}

} catch (IOException e) {

e.printStackTrace();

return;

} finally {

auline.drain();

// auline.close();

}

}

}

}

2025-10-08 17:12:37
友情链接