📄 musicframe.java
字号:
//package music;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;
public class MusicFrame extends JFrame
{
private JButton playButn = new JButton();
private JButton stopButn = new JButton();
private JButton pauseButn = new JButton();
private JButton upButn = new JButton();
private JButton backButn = new JButton();
private static DefaultListModel nameList = new DefaultListModel();
private JList filenamesList = new JList(nameList);
private JPanel butnPanel = new JPanel();
private JMenuBar musicBar = new JMenuBar();
private JMenu file = new JMenu("file");
private JMenuItem open = new JMenuItem("open");
private JFileChooser chooser = new JFileChooser("E:\11");
private static String filename = null;
private JScrollPane scroll = new JScrollPane(filenamesList);
private Mp3Ecoding mp = new Mp3Ecoding();
private static boolean isStop = false;
private static boolean isPause = false;
private static boolean isPlay = false;
private static boolean isCycle = false;
public MusicFrame()
{
super("音乐播放器");
nameList.addElement("11.wav");//外里面加文件
filenamesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
filenamesList.setVisibleRowCount(10);
Dimension d = new Dimension(500,300);
this.filenamesList.setPreferredSize(d);
ULinit();
}
private void addButnIconText(JButton button,String text,ImageIcon icon)
{
button.setText(text);
button.setIcon(icon);
button.setIconTextGap(5);
butnPanel.add(button);
}
private void ULinit()
{
addButnIconText(playButn,"play",new ImageIcon("play.png"));
playButn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
isPlay = true;
isPause = false;
isStop = false;
new Thread(mp).start();
}
});
addButnIconText(stopButn,"stop",new ImageIcon("stop.png"));
stopButn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
isStop = true;
isPause = true;
isPlay = false;
}
});
addButnIconText(pauseButn,"pause",new ImageIcon("pause.png"));
pauseButn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
isPlay = false;
isPause = true;
isStop = false;
}
});
addButnIconText(upButn,"up",new ImageIcon("up.png"));
upButn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
}
});
addButnIconText(backButn,"CyclePlay",new ImageIcon("back.png"));
backButn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
isCycle = true;
isStop = false;
isPause = false;
isPlay = true;
/*if(mp == null)
{
mp = new Mp3Ecoding();
}*/
//new Thread(mp).start();
}
});
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
openFile();
}
});
filenamesList.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent lse)
{
if (!lse.getValueIsAdjusting())
{
isStop = true;
isPause = true;
filename = (String)filenamesList.getSelectedValue();
/*if(mp == null)
{
mp = new Mp3Ecoding();
}*/
}
}
});
file.add(open);
musicBar.add(file);
this.setJMenuBar(musicBar);
this.getContentPane().add(butnPanel,"South");
this.getContentPane().add(scroll,"Center");
}
private void openFile()
{
try
{
int interval = 0;
interval = chooser.showOpenDialog(this);
if(interval == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
nameList.addElement(new String(file.getName()));
}
}catch(HeadlessException e)
{
e.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}
}
public static boolean getIsPause()
{
return isPause;
}
public static boolean getIsStop()
{
return isStop;
}
public static boolean getIsPlay()
{
return isPlay;
}
public static DefaultListModel getNameList()
{
return nameList;
}
public static boolean getIsCycle()
{
return isCycle;
}
public static String getFilename()
{
return filename;
}
//main function
public static void main(String[] args)
{
MusicFrame musicf = new MusicFrame();
musicf.setLocation(250,250);
musicf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
musicf.pack();
musicf.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -