用java写一个音乐播放器的图形化界面
时间: 2023-08-25 13:04:24 浏览: 56
好的,以下是一个简单的音乐播放器的图形化界面的Java代码示例:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class MusicPlayer extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private Clip clip;
private ArrayList<File> musicList = new ArrayList<File>();
private JTextField txtFileName;
private DefaultTableModel tableModel;
private JTable table;
public MusicPlayer() {
// 设置窗口标题
setTitle("Music Player");
// 设置窗口大小
setSize(800, 600);
// 设置窗口位置
setLocationRelativeTo(null);
// 设置窗口关闭按钮操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口布局
setLayout(new BorderLayout());
// 创建顶部面板
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
topPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
// 创建选择文件按钮
JButton btnSelectFile = new JButton("Select File");
btnSelectFile.addActionListener(this);
topPanel.add(btnSelectFile);
// 创建文件名标签
JLabel lblFileName = new JLabel("File Name:");
topPanel.add(lblFileName);
// 创建文件名文本框
txtFileName = new JTextField(20);
txtFileName.setEditable(false);
topPanel.add(txtFileName);
add(topPanel, BorderLayout.NORTH);
// 创建中间面板
JPanel middlePanel = new JPanel(new GridLayout(1, 1));
middlePanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
// 创建音乐列表表格
tableModel = new DefaultTableModel();
tableModel.addColumn("Name");
tableModel.addColumn("Duration");
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
middlePanel.add(scrollPane);
add(middlePanel, BorderLayout.CENTER);
// 创建底部面板
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
bottomPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
// 创建播放按钮
JButton btnPlay = new JButton("Play");
btnPlay.addActionListener(this);
bottomPanel.add(btnPlay);
// 创建停止按钮
JButton btnStop = new JButton("Stop");
btnStop.addActionListener(this);
bottomPanel.add(btnStop);
// 创建退出按钮
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(this);
bottomPanel.add(btnExit);
add(bottomPanel, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Select File")) {
// 弹出文件选择对话框
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
// 获取选择的文件
File file = fileChooser.getSelectedFile();
// 添加到音乐列表
musicList.add(file);
// 获取文件名并显示在文本框中
txtFileName.setText(file.getName());
// 获取音乐时长并添加到表格中
String duration = getDuration(file);
tableModel.addRow(new Object[] { file.getName(), duration });
}
} else if (e.getActionCommand().equals("Play")) {
// 获取当前选中的行
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
// 获取选中行的文件并播放
File file = musicList.get(selectedRow);
try {
// 创建音频输入流
AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(new FileInputStream(file));
// 创建音频剪辑
clip = AudioSystem.getClip();
// 打开音频剪辑并开始播放
clip.open(audioInputStream);
clip.start();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
} else if (e.getActionCommand().equals("Stop")) {
// 停止播放音乐
if (clip != null) {
clip.stop();
}
} else if (e.getActionCommand().equals("Exit")) {
// 关闭窗口
dispose();
}
}
/**
* 获取音乐时长
*
* @param file 音乐文件
* @return 音乐时长字符串
*/
private String getDuration(File file) {
try {
// 创建音频输入流
AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(new FileInputStream(file));
// 获取音频文件长度
long length = audioInputStream.getFrameLength();
// 获取音频帧率
float frameRate = audioInputStream.getFormat().getFrameRate();
// 计算时长并返回
long duration = (long) (length / frameRate);
return String.format("%02d:%02d", duration / 60, duration % 60);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
public static void main(String[] args) {
try {
// 设置窗口样式为系统默认样式
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
// 创建并显示窗口
MusicPlayer musicPlayer = new MusicPlayer();
musicPlayer.setVisible(true);
}
}
```
该播放器包含了以下功能:
1. 选择文件:点击“Select File”按钮,弹出文件选择对话框,选择音乐文件后将其添加到音乐列表中。
2. 显示音乐列表:在中间面板中使用表格显示音乐列表,包括音乐名称和时长。
3. 播放音乐:选中表格中的一行后,点击“Play”按钮,即可播放选中的音乐文件。
4. 停止音乐:在音乐播放过程中,点击“Stop”按钮即可停止当前正在播放的音乐文件。
5. 退出程序:点击“Exit”按钮即可退出程序。
希望能对你有所帮助!