📄 mediaplayer.java
字号:
package play;
//程序主文件
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;
import java.io.*;
import java.util.*;//为了导入Vector
//import com.sun.java.swing.plaf.windows.*;
@SuppressWarnings("serial")
public class MediaPlayer extends JFrame implements ActionListener, Runnable {
private JMenuBar bar;// 菜单条
private JMenu fileMenu, choiceMenu, aboutMenu;
private JMenuItem openItem, openDirItem, closeItem, about, infor,pauseItem,startItem,preItem,nextItem;
private JCheckBoxMenuItem onTop;
@SuppressWarnings("unused")
private boolean top = false, loop;// 设定窗口是否在最前面
private Player player;// Play是个实现Controller的接口
private File file, listFile;// 利用File类结合JFileChooser进行文件打开操作,后则与list.ini有关
private Container c;
// private UIManager.LookAndFeelInfo[] look;
@SuppressWarnings("unused")
private String title, listIniAddress;// 标题
private FileDialog fd;
private JPanel panel, panelSouth;
private Icon icon; // 开始进入的时候要显示的图标,它为抽象类,不能自己创建
private JLabel label, listB;// 用来显示图标
private JList list;// 播放清单
private JScrollPane scroll;// 使播放清单具有滚动功能
private ListValues listWriteFile;// 用于向文件中读取对象
private ObjectInputStream input;// 对象输入流
private ObjectOutputStream output;// 对象输出流
private JPopupMenu popupMenu;// 鼠标右键弹出菜单
private JMenuItem del, delAll, reName; // 弹出菜单显示的菜单项,包括删除,全部删除和重命名
private Vector fileName, dirName, numList;
@SuppressWarnings("unused")
private String files, dir;
private int index;// 曲目指针
@SuppressWarnings("unused")
private Properties prop;// 获得系统属性
private int indexForDel;// 标志要删除的列表项目的索引
private ButtonGroup buttonGroup;// 控制按钮组
private JRadioButtonMenuItem[] buttonValues;
private String[] content = { "随机播放", "顺序播放", "单曲循环" };
private JLabel jlabel;
private File[] fileList;
private DialogDemo dialog1;
// private JDialogTest dialog2;//用于显示播放清单
private static boolean isWhile = true;
private long currentTime;
int i = 0;
MediaPlayer()// 构造函数
{
super("java音频播放器1.1版");// 窗口标题
c = getContentPane();
c.setLayout(new BorderLayout());
c.setBackground(new Color(40, 40, 95));
fileName = new Vector(1);
dirName = new Vector(1);
numList = new Vector(1);// 构造三个容器用于支持播放清单
// vectorToString=new String[];
// prop=new Properties(System.getProperties());
// listIniAddress=prop.getProperty("user.dir")+"\\list.ini";
// listFile=new File(listIniAddress);//本来这些代码用来取的系统属性,后来
// 发现根本就不用这么麻烦
File file = new File("E:/photo");
if (file.isDirectory())
fileList = file.listFiles();
listFile = new File("list.ini");// 直接存于此目录
Thread readToList = new Thread();// 注意编线程程序的时候要注意运行的时候含有的变量亿定义或者初始化,
// 这就要求线程要等上述所说的情况下再运行,否则很容易发生错误或则异常
list = new JList();
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setSelectionForeground(new Color(0, 150, 150));
list.setVisibleRowCount(10);
list.setFixedCellHeight(12);
list.setFixedCellWidth(250);
list.setFont(new Font("Serif", Font.PLAIN, 12));
list.setBackground(new Color(40, 40, 95));
list.setForeground(new Color(0, 128, 255));
// list.setOpaque(false);
list.setToolTipText("点右键显示更多功能");// 创建播放清单并设置各个属性
list.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) // 判断是否双击
{
index = list.locationToIndex(e.getPoint());// 将鼠标坐标转化成list中的选项指针
createPlayer2();
// System.out.println("Double clicked on Item " +
// index);,此是测试的时候加的
}
}
/*
* public void mousePressed(MouseEvent e) {
* checkMenu(e);//自定义函数,判断是否是右键,来决定是否显示菜单 }
*/
public void mouseReleased(MouseEvent e) {
checkMenu(e);// 与上面的一样,判断是否鼠标右键
}
});
// listB=new JLabel(new ImageIcon("qingdan.gif"),SwingConstants.CENTER);
scroll = new JScrollPane(list);// 用于存放播放列表
// dialog2=new JDialogTest(MediaPlayer.this,"播放清单",scroll);
// dialog2.setVisible(true);
/*
* look=UIManager.getInstalledLookAndFeels(); try {
* UIManager.setLookAndFeel(look[2].getClassName());
* SwingUtilities.updateComponentTreeUI(this); } catch(Exception e) {
* e.printStackTrace(); }
*/// 与下面的代码实现相同的功能,但执行速度要慢,原因:明显转了个大弯
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
catch(Exception e) { e.printStackTrace(); }
bar = new JMenuBar();
setJMenuBar(bar);// 此两行创建菜单栏并放到此窗口程序
// bar.setBackground(new Color(48,91,183));
fileMenu = new JMenu("文件");
bar.add(fileMenu);
choiceMenu = new JMenu("控制");
bar.add(choiceMenu);
aboutMenu = new JMenu("帮助");
bar.add(aboutMenu);
openItem = new JMenuItem("打开文件");
openDirItem = new JMenuItem("打开目录");
closeItem = new JMenuItem("退出程序");
openItem.addActionListener(this);
openDirItem.addActionListener(this);
closeItem.addActionListener(this);
fileMenu.add(openItem);
fileMenu.add(openDirItem);
fileMenu.add(closeItem);
pauseItem = new JMenuItem("暂停");
startItem = new JMenuItem("播放");
pauseItem.addActionListener(this);
startItem.addActionListener(this);
preItem = new JMenuItem("上一首");
nextItem = new JMenuItem("下一首");
preItem.addActionListener(this);
nextItem.addActionListener(this);
onTop = new JCheckBoxMenuItem("播放时位于最前面", top);
choiceMenu.add(startItem);
choiceMenu.add(pauseItem);
choiceMenu.add(preItem);
choiceMenu.add(nextItem);
choiceMenu.addSeparator();// 加分割符号
choiceMenu.add(onTop);
onTop.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (onTop.isSelected())
top = true;
else
top = false;
setAlwaysOnTop(top);
}
});
onTop.setSelected(true);
setAlwaysOnTop(top);
buttonGroup = new ButtonGroup();
buttonValues = new JRadioButtonMenuItem[3];
for (int bt = 0; bt < 3; bt++) {
buttonValues[bt] = new JRadioButtonMenuItem(content[bt]);
buttonGroup.add(buttonValues[bt]);
choiceMenu.add(buttonValues[bt]);
}
buttonValues[1].setSelected(true);
choiceMenu.addSeparator();
/*
* loopItem=new JCheckBoxMenuItem("是否循环"); choiceMenu.add(loopItem);
* loopItem.addItemListener(new ItemListener() { public void
* itemStateChanged(ItemEvent e) { loop=!loop; } } );
*/
infor = new JMenuItem("软件简介");
aboutMenu.add(infor);
infor.addActionListener(this);
about = new JMenuItem("关于作者");
about.addActionListener(this);
aboutMenu.add(about);
// 菜单栏设置完毕
dialog1 = new DialogDemo(MediaPlayer.this,"软件简介");
panel = new JPanel();
panel.setLayout(new BorderLayout());
c.add(panel, BorderLayout.BEFORE_LINE_BEGINS);
panelSouth = new JPanel();
panelSouth.setLayout(new BorderLayout());
c.add(panelSouth, BorderLayout.SOUTH);
icon = new ImageIcon("icon\\Player.jpg");
label = new JLabel(icon);
panel.add(label);
ImageIcon icon = new ImageIcon(fileList[(int) (Math.random() * fileList.length)].getAbsolutePath());
Image image = icon.getImage();
image = image.getScaledInstance(300, 205, Image.SCALE_SMOOTH);
icon.setImage(image);
jlabel = new JLabel();
jlabel.setIcon(icon);
panel.add(jlabel, BorderLayout.BEFORE_FIRST_LINE);
popupMenu = new JPopupMenu();
del = new JMenuItem("删除");// 鼠标右键弹出菜单对象实例化
popupMenu.add(del);
del.addActionListener(this);
delAll = new JMenuItem("全部删除");
popupMenu.add(delAll);
delAll.addActionListener(this);
reName = new JMenuItem("重命名");
popupMenu.add(reName);
reName.addActionListener(this);
scroll = new JScrollPane(list);// 用于存放播放列表
listB = new JLabel(new ImageIcon("icon\\Player.jpg"),
SwingConstants.CENTER);
panelSouth.add(listB, BorderLayout.NORTH);
panelSouth.add(scroll, BorderLayout.CENTER);
readToList.start();// 启动先程,加载播放列表
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);// 设定窗口关闭方式
// this.setTitle("d");编译通过,说明可以再次设定标题
this.setLocation(400, 250);// 设定窗口出现的位置
// this.setSize(350,320);//窗口大小
setSize(300, 400);
this.setResizable(false);// 设置播放器不能随便调大小
this.setVisible(true);// 此句不可少,否则窗口会不显示
currentTime = System.currentTimeMillis();
/*
* SwingWorker worker = new SwingWorker() { public Object construct() {
*
* while (isWhile) { try { Thread.sleep(5000); long nowTime =
* System.currentTimeMillis(); if (nowTime - currentTime > 6000) {
* currentTime = System.currentTimeMillis(); if (i < fileList.length) if
* (fileList[i].isFile()) { ImageIcon iconShow = new ImageIcon(
* fileList[i].getAbsolutePath()); Image imageshow = iconShow.getImage();
* imageshow = imageshow.getScaledInstance( 300, 199, Image.SCALE_SMOOTH);
* iconShow.setImage(imageshow); jlabel.setIcon(iconShow); } if (i ==
* fileList.length) i = 0; else i++; } } catch (InterruptedException ex) { //
* 记载错误日志 } } return "OK"; }
*
* public void finished() { return; } }; worker.start();
*/
SwingWorker worker = new SwingWorker() {
public Object construct() {
while (isWhile) {
try {
i = (int) (Math.random() * fileList.length);
Thread.sleep(6000);
long nowTime = System.currentTimeMillis();
if (nowTime - currentTime >= 6000) {
ImageIcon iconShow = new ImageIcon(
fileList[i].getAbsolutePath());
Image imageshow = iconShow.getImage();
imageshow = imageshow.getScaledInstance(
300, 205, Image.SCALE_SMOOTH);
iconShow.setImage(imageshow);
currentTime = System.currentTimeMillis();
jlabel.setIcon(iconShow);
if (i == fileList.length)
i = 0;
}
} catch (InterruptedException ex) {
// 记载错误日志
}
}
return "OK";
}
public void finished() {
return;
}
};
worker.start();
}
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent e) {
if (e.getSource() == openItem)// getSource()判断发生时间的组键
{
// System.out.println("d");测试用
openFile();
// createPlayer();
// setTitle(title);
}
if (e.getSource() == openDirItem)// 打开目录
{
openDir();
}
if (e.getSource() == closeItem)// 推出播放器
{
exity_n();
// System.exit(0);
}
if (e.getSource() == pauseItem) {
pause();
}
if (e.getSource() == startItem) {
startCurrentPlayer();
}
if (e.getSource() == preItem) {
prePlayer();
}
if (e.getSource() == nextItem) {
nextPlayer();
}
if (e.getSource() == about) {
JOptionPane.showMessageDialog(this, "此简易播放器由成都理工大学信息工程学院计算机系3班\n" + "施济瑜\n "
+ " 完成 ", "参与者",
JOptionPane.INFORMATION_MESSAGE);
}
if (e.getSource() == del) {
// index
// delPaintList(index);
fileName.removeElementAt(indexForDel);
dirName.removeElementAt(indexForDel);
numList.removeAllElements();// 从三个容器里面移除此项
Enumeration enumFile = fileName.elements();
while (enumFile.hasMoreElements()) {
numList.addElement((numList.size() + 1) + "."
+ enumFile.nextElement());
// numList添加元素,显示播放里表中
}
// list.setListData(fileName);
list.setListData(numList);
if (index < indexForDel)
list.setSelectedValue(numList.elementAt(index), true);
else {
if (index == indexForDel)
;
else if (index != 0)
list.setSelectedValue(numList.elementAt(index - 1), true);
}
// list.setSelectedIndex(index);
}
if (e.getSource() == delAll)// 全部删除
{
fileName.removeAllElements();
dirName.removeAllElements();
numList.removeAllElements();
list.setListData(numList);
}
if (e.getSource() == reName)// 重命名
{
String name;// =JOptionPane.showInputDialog(this,"请输入新的名字");
try {
name = reNames();
fileName.setElementAt(name, indexForDel);
numList.setElementAt((indexForDel + 1) + "." + name,
indexForDel);
} catch (ReName e2)// 自定义的异常
{
}
}
if (e.getSource() == infor) {
dialog1.setVisible(true);
}
}
private void nextPlayer(){
index++;
createPlayer2();
}
private void prePlayer(){
index--;
if(index<0){
index = 0;
}
createPlayer2();
}
public static void main(String[] args) {
try{
final MediaPlayer mp = new MediaPlayer();
mp.setIconImage(new ImageIcon("E:/photo/DSC02183.JPG").getImage());// 改变默认图标
mp.addWindowListener(new WindowAdapter()// 注册窗口事件
{
public void windowClosing(WindowEvent e) {
// System.exit(0);
mp.exity_n();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -