这段时间研究了一下java中的Swing组件,虽然功能有限但还是可以实现一定的前端显示效果。基于Swing组件做了一个由Java代码实现的简易音乐播放器,予以记录。
点击“选择目录”选中目标文件夹,然后点击“search”按钮列出文件夹中所有文件。在左边列表中选中音乐文件点击“播放”或者直接双击文件名称即可播放。界面效果:
代码:
import jmp123.PlayBack;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
public class Audio_Player extends JFrame {
Thread thread = null;
String initPath = ".";
public static void main(String[] args) {
new Audio_Player();
}
public Audio_Player(){
setTitle("Player");
setLayout(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500, 330);
setLocation(600, 300);
setResizable(false);
Container container = getContentPane();
JList<String> filesJList = new JList<>();
filesJList.setFixedCellHeight(30);
filesJList.setFont(new Font("宋体", Font.BOLD, 24));
JScrollPane fileListPane = new JScrollPane(filesJList);
fileListPane.setBounds(0, 0, 300, 280);
JButton playButton = new JButton("播放");
playButton.setBounds(300, 20, 150, 30);
playButton.setFont(new Font("宋体", Font.PLAIN, 24));
JButton stopButton = new JButton("停止");
stopButton.setBounds(300, 60, 150, 30);
stopButton.setFont(new Font("宋体", Font.PLAIN, 24));
stopButton.addActionListener(e -> {
if(thread != null && thread.isAlive()){
thread.stop();
thread = null;
}
});
JTextField dirField = new JTextField();
dirField.setEditable(false);
dirField.setBackground(Color.white);
dirField.setFont(new Font("宋体", Font.BOLD, 20));
dirField.setBounds(300, 140, 160, 30);
JButton searchButton = new JButton("search");
searchButton.setBounds(300, 100, 150, 30);
searchButton.setFont(new Font("宋体", Font.PLAIN, 24));
searchButton.addActionListener(e -> {
String path = dirField.getText();
if(!path.isEmpty()){
File file = new File(path);
if(file.isDirectory()) {
String[] list = file.list();
filesJList.setListData(list);
} else {
String name = file.getName();
String[] list = new String[]{name};
filesJList.setListData(list);
}
}
});
JButton chooseDirButton = new JButton("选择目录");
chooseDirButton.setBounds(300, 180, 150, 30);
chooseDirButton.setFont(new Font("宋体", Font.PLAIN, 24));
chooseDirButton.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser(initPath);
fileChooser.setMultiSelectionEnabled(true);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.showOpenDialog(this);
File file = fileChooser.getSelectedFile();
if(file != null){
dirField.setText(file.getAbsolutePath());
initPath = file.isDirectory() ? file.getAbsolutePath() : getDir(file.getAbsolutePath());
}
});
playButton.addActionListener(e -> {
String dir = dirField.getText();
String value = filesJList.getSelectedValue();
if(!dir.isEmpty() && !value.isEmpty()){
File file = new File(dir);
String path = ( file.isDirectory() ? (dir + "\\" + value) : dir );
if(thread == null || !thread.isAlive()){
thread = new Thread("thread"){
@Override
public void run() {
try {
PlayBack player = new PlayBack(new jmp123.output.Audio());
player.open(path,"");
player.start(true);
} catch (Exception exp) {
exp.printStackTrace();
}
}
};
} else {
thread.stop();
thread = null;
thread = new Thread("thread"){
@Override
public void run() {
try {
PlayBack player = new PlayBack(new jmp123.output.Audio());
player.open(path,"");
player.start(true);
} catch (Exception exp) {
exp.printStackTrace();
}
}
};
}
thread.start();
}
});
filesJList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2){
String dir = dirField.getText();
String value = filesJList.getSelectedValue();
if(!dir.isEmpty() && !value.isEmpty()){
File file = new File(dir);
String path = ( file.isDirectory() ? (dir + "\\" + value) : dir );
if(thread == null || !thread.isAlive()){
thread = new Thread("thread"){
@Override
public void run() {
try {
PlayBack player = new PlayBack(new jmp123.output.Audio());
player.open(path,"");
player.start(true);
} catch (Exception exp) {
exp.printStackTrace();
}
}
};
} else {
thread.stop();
thread = null;
thread = new Thread("thread"){
@Override
public void run() {
try {
PlayBack player = new PlayBack(new jmp123.output.Audio());
player.open(path,"");
player.start(true);
} catch (Exception exp) {
exp.printStackTrace();
}
}
};
}
thread.start();
}
}
}
});
container.add(fileListPane);
container.add(playButton);
container.add(stopButton);
container.add(searchButton);
container.add(dirField);
container.add(chooseDirButton);
setVisible(true);
}
public String getDir(String path){
int idx = path.lastIndexOf("\\");
return path.substring(0, idx);
}
}