类有点多,就贴一个主窗口类的代码出来吧,写了一个星期,总算是搞出了点成果,嘿嘿。。。
主要功能:
注册,登录,播放音乐,搜索音乐,下载音乐,登录后可获得更多权限的服务
设置的功能主要有:微窗,换皮肤,透明度,触键音效,按键音效,透明度,心情驿站(写并且保存心情),另外还有一个便签的功能。
播放音乐,搜索音乐,下载音乐,和便签功能可以在未登录的状态下使用,其它的高级功能必须在登录之后才有效。
此外可以往播放列表中添加歌曲和删除歌曲,设置播放模式(随机,顺序,单曲循环,列表循环)
大概就是这样了,不说了,贴代码
package rgy.com.entity;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Random;
import javax.media.CannotRealizeException;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.GainControl;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.Time;
import javax.print.DocFlavor.STRING;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.sun.awt.AWTUtilities;
@SuppressWarnings("restriction")
public class MainFrame2 extends JFrame {
// 添加属性
int xOld = 0;
int yOld = 0;
//
JLabel label_close, label_mini;
JPanel panel_close, panel_mini;
JLabel label_login, label_register;
JTextField tf_search;
JLabel label_search;
JLabel label_username;
JLabel label_free, label_download, label_local;
JPanel panel_pbar;// 进度面板
JSlider slider_progress;
JLabel label_fast, label_slow;
JLabel label_currenttime, label_alltime;
JLabel label_startAndPause, label_former, label_next, label_volume;
VolumeDialog vDialog;
JLabel label_OFFandON;
JLabel label_yueyinBox;
//
BoxDialog boxDialog;
MoodBoxDialog moodBoxDialog;
SmallFrame smallFrame;
NotepadDialog notepadDialog;
SkinBoxDialog skinBoxDialog;
RegisterDialog registerDialog;
LoginDialog loginDialog;
// 歌曲列表
JList<String> list;
DefaultListModel<String> listModel;
JPopupMenu popupmenu;
JMenuItem popmenuitem[];
//
boolean switchControl = false;// 悦音轩大门的开关
//
boolean flag_touch = false;
boolean flag_click = false;
//
Player player = null;
GainControl gc = null;
//
double second_all, second_current;
int progress = 0;
//
int music_count = 0;
//
UpdateProgressThread th = null;
//
public final static int model_random = 0;// 随机播放
public final static int model_order = 1;// 顺序播放
public final static int model_onecircle = 2;// 单曲循环
public final static int model_listcircle = 3;// 列表循环
int playModel = model_onecircle;// 默认为顺序播放
//
int volume = 70;// 音量
int transparence = 100;// 透明度
//
JLabel background;
String skinpic=null;
//
boolean isSuccess=false;
//
public MainFrame2(String str) {
super(str);
this.setBounds(420, 160, 635, 400);
// 添加背景图片
((JPanel) this.getContentPane()).setOpaque(false);
ImageIcon image = new ImageIcon("img/background.jpg");
background = new JLabel(image);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
// 添加功能代码
initSet();// 初始化设置
createInterface();// 设计界面
initList();// 初始化列表
registerEvent();// 管理所有注册事件
}
public void initSet() {
File file = new File("E:\\113Player\\save\\set.rgy");
if (file.exists()) {
try {
FileInputStream fin = new FileInputStream(file);
DataInputStream din = new DataInputStream(fin);
ObjectInputStream objin=new ObjectInputStream(fin);
//
flag_touch = din.readBoolean();
flag_click = din.readBoolean();
volume = din.readInt();
transparence = din.readInt();
playModel = din.readInt();
skinpic=(String)objin.readObject();
//
objin.close();
din.close();
fin.close();
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
return;
}
}
public void initList() {
File file = new File("E:\\113Player\\save\\list.rgy");
if (file.exists()) {
try {
FileInputStream fin = new FileInputStream(file);
ObjectInputStream objin = new ObjectInputStream(fin);
music_count = Integer.parseInt((String) objin.readObject());
for (int index = 0; index < music_count; index++) {
String str = (String) objin.readObject();
listModel.addElement(str);
}
//
objin.close();
fin.close();
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
return;
}
}
public void saveSet() {
try {
FileOutputStream fout = new FileOutputStream(
"E:\\113Player\\save\\set.rgy");
DataOutputStream dout = new DataOutputStream(fout);
ObjectOutputStream objout=new ObjectOutputStream(fout);
//
dout.writeBoolean(flag_touch);
dout.writeBoolean(flag_click);
//
int yinliang = vDialog.slider.getValue();
dout.writeInt(yinliang);
//
int toumingdu = boxDialog.slider_tp.getValue();
dout.writeInt(toumingdu);
//
dout.writeInt(playModel);
//对象字节流
String str_bgpic=background.getIcon().toString();
objout.writeObject(str_bgpic);
//
objout.close();
dout.close();
fout.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void saveList() {
try {
FileOutputStream fout = new FileOutputStream(
"E:\\113Player\\save\\list.rgy");
ObjectOutputStream objout = new ObjectOutputStream(fout);
String str_mcount = "" + music_count;
objout.writeObject(str_mcount);
for (int index = 0; index < music_count; index++) {
String str = listModel.getElementAt(index);
objout.writeObject(str);
}
objout.close();
fout.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void createInterface() {
// 去掉窗口的装饰
this.setUndecorated(true);
// 改变窗体上的图标,换成自己的图标
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.getImage("img/TT.jpg");
setIconImage(img);
// 不使用布局
this.setLayout(null);
// 关闭与最小化
ImageIcon imageIcon1 = new ImageIcon("img/close3.png");
label_close = new JLabel(imageIcon1);
label_close.setBounds(0, 0, imageIcon1.getIconWidth(),
imageIcon1.getIconHeight());
this.add(label_close);
//
ImageIcon imageIcon2 = new ImageIcon("img/mini3.png");
label_mini = new JLabel(imageIcon2);
label_mini.setBounds(35, 0, imageIcon2.getIconWidth(),
imageIcon2.getIconHeight());
this.add(label_mini);
// 歌曲列表
this.listModel = new DefaultListModel<String>();
this.list = new JList<String>(this.listModel);
JScrollPane sp = new JScrollPane(this.list);
sp.setBounds(5, 40, 200, 350);
this.list.setBackground(Color.ORANGE);
this.list.setFont(new Font("微软雅黑", 0, 13));
this.add(sp);
// 快捷菜单
popupmenu = new JPopupMenu();
String menuitemstr[] = { "播放", "删除", "随机播放", "顺序播放", "单曲循环", "列表循环" };
popmenuitem = new JMenuItem[menuitemstr.length];
for (int i = 0; i < popmenuitem.length; i++) {
popmenuitem[i] = new JMenuItem(menuitemstr[i]);// 菜单项
popmenuitem[i].setFont(new Font("楷体", 0, 14));
popupmenu.add(popmenuitem[i]);// 快捷菜单加入菜单项
}
this.list.add(popupmenu);
// 登录与注册
ImageIcon imageIcon3 = new ImageIcon("img/login.png");
label_login = new JLabel(imageIcon3);
label_login.setBounds(this.getWidth() - 122, 0, 61, 30);
this.add(label_login);
ImageIcon imageIcon4 = new ImageIcon("img/register.png");
label_register = new JLabel(imageIcon4);
label_register.setBounds(this.getWidth() - 61, 0, 61, 30);
this.add(label_register);
// 歌曲搜索(连接服务器搜索)
ImageIcon imageIcon5 = new ImageIcon("img/search.png");
label_search = new JLabel(imageIcon5);
label_search.setBounds(270, 0, 30, 30);
this.add(label_search);
//
tf_search = new JTextField("请输入歌曲名");
tf_search.setBounds(305, 3, 200, 27);
this.add(tf_search);
// logo
ImageIcon imageIcon6 = new ImageIcon("img/logo.gif");
JLabel label_logo = new JLabel(imageIcon6);
label_logo.setBounds(230, 0, 30, 30);
this.add(label_logo);
// 113悦音 “乐”在其中
ImageIcon imageIcon7 = new ImageIcon("img/yueyin.png");
JLabel label_yueyin = new JLabel(imageIcon7);
label_yueyin.setBounds(230, 35, 400, 50);
this.add(label_yueyin);
// 用户名的label
label_username = new JLabel("未登录");
label_username.setFont(new Font("楷体", 1, 17));
label_username.setBounds(100, 0, 100, 30);
label_username.setHorizontalAlignment(JLabel.RIGHT);
label_username.setForeground(Color.BLUE);
this.add(label_username);
// 随便听听,音乐下载,本地音乐
ImageIcon imageIcon8 = new ImageIcon("img/free.png");
label_free = new JLabel(imageIcon8);
label_free.setBounds(230, 100, 110, 142);
this.add(label_free);
//
ImageIcon imageIcon9 = new ImageIcon("img/download.png");
label_download = new JLabel(imageIcon9);
label_download.setBounds(360, 100, 110, 142);
this.add(label_download);
//
ImageIcon imageIcon10 = new ImageIcon("img/local.png");
label_local = new JLabel(imageIcon10);
label_local.setBounds(490, 100, 110, 142);
this.add(label_local);
// 歌曲进度条
panel_pbar = new JPanel();
panel_pbar.setBounds(225, 250, 380, 40);
panel_pbar.setOpaque(false);
panel_pbar.setLayout(null);
this.add(panel_pbar);
slider_progress = new JSlider(JSlider.HORIZONTAL, 0, 1000, 0);
slider_progress.setBounds(5, 10, 380, 30);
slider_progress.setUI(new MySliderUI());
slider_progress.setOpaque(false);
panel_pbar.add(slider_progress);
// 快进
ImageIcon imageIcon17 = new ImageIcon("img/fast.png");
label_fast = new JLabel(imageIcon17);
label_fast.setBounds(580, 315, 25, 25);
this.add(label_fast);
// 快退
ImageIcon imageIcon18 = new ImageIcon("img/slow.png");
label_slow = new JLabel(imageIcon18);
label_slow.setBounds(230, 315, 25, 25);
this.add(label_slow);
// 当前时间标志
label_currenttime = new JLabel("00:00");
label_currenttime.setBounds(230, 290, 40, 20);
this.add(label_currenttime);
// 歌曲总时间标志
label_alltime = new JLabel("00:00");
label_alltime.setBounds(580, 290, 40, 20);
this.add(label_alltime);
// 歌曲控制按钮
ImageIcon imageIcon11 = new ImageIcon("img/start.png");
label_startAndPause = new JLabel(imageIcon11);
label_startAndPause.setBounds(390, 300, 50, 50);
this.add(label_startAndPause);
//
ImageIcon imageIcon12 = new ImageIcon("img/former.png");
label_former = new JLabel(imageIcon12);
label_former.setBounds(340, 305, 40, 40);
this.add(label_former);
//
ImageIcon imageIcon13 = new ImageIcon("img/next.png");
label_next = new JLabel(imageIcon13);
label_next.setBounds(450, 305, 40, 40);
this.add(label_next);
//
ImageIcon imageIcon14 = new ImageIcon("img/volume.png");
label_volume = new JLabel(imageIcon14);
label_volume.setBounds(500, 307, 40, 40);
this.add(label_volume);
// 音量滑块对话框
vDialog = new VolumeDialog(this, volume);
vDialog.setVisible(false);
// 音效开关 (OFF/ON)
ImageIcon imageIcon15 = new ImageIcon("img/OFF.png");
label_OFFandON = new JLabel(imageIcon15);
label_OFFandON.setBounds(575, 375, 60, 24);
this.add(label_OFFandON);
// 悦音轩
ImageIcon imageIcon16 = new ImageIcon("img/yueyinxuan.png");
label_yueyinBox = new JLabel(imageIcon16);
label_yueyinBox.setBounds(230, 350, 114, 40);
this.add(label_yueyinBox);
// 悦音轩(包括透明度设置)
boxDialog = new BoxDialog(MainFrame2.this, transparence);
boxDialog.setVisible(false);
// 心情驿站
moodBoxDialog = new MoodBoxDialog(boxDialog);
moodBoxDialog.setVisible(false);
// 微窗
smallFrame = new SmallFrame(this);
smallFrame.setVisible(false);
// 便签
notepadDialog = new NotepadDialog(this);
notepadDialog.setVisible(false);
//皮肤
skinBoxDialog=new SkinBoxDialog(this);
skinBoxDialog.setVisible(false);
//登录框
loginDialog=new LoginDialog(this);
loginDialog.setVisible(false);
//注册框
registerDialog=new RegisterDialog(this);
registerDialog.setVisible(false);
//换皮肤
if (skinpic != null) {
ImageIcon image = new ImageIcon(skinpic);
this.background.setIcon(image);
this.moodBoxDialog.background.setIcon(image);
}
//
this.setVisible(true);
}
public void registerEvent() {
// 拖动窗口
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
int xOnScreen = e.getXOnScreen();
int yOnScreen = e.getYOnScreen();
int xx = xOnScreen - xOld;
int yy = yOnScreen - yOld;
MainFrame2.this.setLocation(xx, yy);
vDialog.setLocation(MainFrame2.this.getX() + 635,
MainFrame2.this.getY() + 250);
boxDialog.setLocation(MainFrame2.this.getX() + 205,
MainFrame2.this.getY() + 400);
moodBoxDialog.setLocation(MainFrame2.this.getX() + 635,
MainFrame2.this.getY());
notepadDialog.setLocation(MainFrame2.this.getX() + 635,
MainFrame2.this.getY() + 200);
skinBoxDialog.setLocation(MainFrame2.this.getX() + 230,
MainFrame2.this.getY() + 100);
}
});
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
xOld = e.getX();
yOld = e.getY();
}
});
// 点击两次关闭程序
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1
&& e.getClickCount() == 2) {
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
// 保存(设置与歌曲列表)
saveSet();
saveList();
//
System.exit(0);
}
}
});
/**
* 关闭与最小化按钮,并为其添加事件
*/
label_close.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/close.png");
label_close.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/close3.png");
label_close.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/close2.png");
label_close.setIcon(icon);
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
}
public void mouseReleased(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/close.png");
label_close.setIcon(icon);
// 保存(设置与歌曲列表)
saveSet();
saveList();
//
System.exit(0);
}
});
// 最小化
label_mini.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/mini.png");
label_mini.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/mini3.png");
label_mini.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/mini2.png");
label_mini.setIcon(icon);
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
}
// 最小化
public void mouseReleased(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/mini.png");
label_mini.setIcon(icon);
MainFrame2.this.setState(Frame.ICONIFIED);
// 便签不可见,便签开关关闭
ImageIcon icon1 = new ImageIcon("img/OFF.png");
label_OFFandON.setIcon(icon1);
MainFrame2.this.notepadDialog.setVisible(false);
// 悦音轩关闭
MainFrame2.this.boxDialog.setVisible(false);
// 音量面板关闭
vDialog.setVisible(false);
}
});
/**
* 为登录注册添加效果响应事件
*/
label_login.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/login1.png");
label_login.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/login.png");
label_login.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
//
loginDialog.setVisible(true);
}
});
label_register.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/register1.png");
label_register.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/register.png");
label_register.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
//
registerDialog.setVisible(true);
}
});
/**
* 搜索
*/
label_search.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/search1.png");
label_search.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/search.png");
label_search.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
// 补充事件监听
new ClientSearchThread(MainFrame2.this).start();
}
});
tf_search.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
tf_search.setText("");
}
});
tf_search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 补充事件监听
new ClientSearchThread(MainFrame2.this).start();
}
});
/**
* 随便听听
*/
label_free.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/free2.png");
label_free.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/free.png");
label_free.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
File sound = new File("sound/click.wav");
PlayMusic.play(sound, flag_click);
// 补充事件监听
playModel = model_random;// 本地音乐随机播放
//
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
//
Random r = new Random();
int index = r.nextInt(music_count);
list.setSelectedIndex(index);// 随机一曲
//
String musicPath = list.getSelectedValue();
File file = new File(musicPath);
if (file.exists()) {
musicGo(musicPath);
controlLis();
showAllTime();
//
if (th == null) {
th = new UpdateProgressThread();
th.start();
}
} else {
new TipDialog(MainFrame2.this, "文件路径错误,请删除该记录", TipDialog.TIP_ERROR);
File errorSound = new File("sound/error.wav");
PlayMusic.play(errorSound, true);
}
System.out.println("随便听听");
}
});
/**
* 音乐下载
*/
label_download.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/download2.png");
label_download.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/download.png");
label_download.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
// 补充事件监听
new ClientDownLoadThread(MainFrame2.this).start();
}
});
/**
* 本地音乐
*/
label_local.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/local2.png");
label_local.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/local.png");
label_local.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
ImageIcon icon = new ImageIcon("img/pause.png");
label_startAndPause.setIcon(icon);
// 补充事件监听
String currentfile, selectfile, currentDirectory = null;
FileDialog fd = new FileDialog(MainFrame2.this, "打开媒体文件",
FileDialog.LOAD);
fd.setDirectory(currentDirectory);
fd.setVisible(true);
if (fd.getFile() == null) {
return;
}
selectfile = fd.getFile();
currentDirectory = fd.getDirectory();
currentfile = currentDirectory + selectfile;
String musicPath = currentfile;
//
for(int i=0;i<listModel.getSize();i++){
if(listModel.getElementAt(i).equals(musicPath)){
new TipDialog(MainFrame2.this,"歌曲已存在!",TipDialog.TIP_ERROR);
File sound=new File("sound/error.wav");
PlayMusic.play(sound,true);
return;
}
}
//
listModel.addElement(musicPath);// 加入歌曲列表
list.setSelectedIndex(music_count);
music_count++;
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
musicGo(musicPath);
controlLis();
player.start();
showAllTime();
if (gc == null) {
gc = player.getGainControl();
}
//
if (th == null) {
th = new UpdateProgressThread();
th.start();
}
//
}
});
/**
* 音乐控制按钮:开始,下一曲,上一曲
*/
label_startAndPause.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
if (label_startAndPause.getIcon().toString()
.equals("img/start.png")) {
ImageIcon icon = new ImageIcon("img/start1.png");
label_startAndPause.setIcon(icon);
} else {
ImageIcon icon = new ImageIcon("img/pause1.png");
label_startAndPause.setIcon(icon);
}
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
if (label_startAndPause.getIcon().toString()
.equals("img/start1.png")) {
ImageIcon icon = new ImageIcon("img/start.png");
label_startAndPause.setIcon(icon);
} else {
ImageIcon icon = new ImageIcon("img/pause.png");
label_startAndPause.setIcon(icon);
}
}
public void mousePressed(MouseEvent e) {
if (label_startAndPause.getIcon().toString()
.equals("img/start1.png")) {
ImageIcon icon = new ImageIcon("img/pause1.png");
label_startAndPause.setIcon(icon);
// 补充事件监听
player.start();
} else {
ImageIcon icon = new ImageIcon("img/start1.png");
label_startAndPause.setIcon(icon);
// 补充事件监听
player.stop();
}
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
}
});
/**
* 上一曲
*/
label_former.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/former1.png");
label_former.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/former.png");
label_former.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
File sound = new File("sound/click.wav");
PlayMusic.play(sound, flag_click);
// 补充事件监听
int index = MainFrame2.this.list.getSelectedIndex();
if (index > 0) {
MainFrame2.this.list.setSelectedIndex(index - 1);// 上一曲
// 播放
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
String musicPath = list.getSelectedValue();
File file = new File(musicPath);
if (file.exists()) {
musicGo(musicPath);
controlLis();
showAllTime();
//
if (th == null) {
th = new UpdateProgressThread();
th.start();
}
} else {
new TipDialog(MainFrame2.this, "文件路径错误,请删除该记录", TipDialog.TIP_ERROR);
File errorSound = new File("sound/error.wav");
PlayMusic.play(errorSound, true);
}
}
}
});
/**
* 下一曲
*/
label_next.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/next1.png");
label_next.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/next.png");
label_next.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
File sound = new File("sound/click.wav");
PlayMusic.play(sound, flag_click);
// 补充事件监听
int index = MainFrame2.this.list.getSelectedIndex();
if (index < music_count) {
MainFrame2.this.list.setSelectedIndex(index + 1);// 下一曲
// 播放
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
String musicPath = list.getSelectedValue();
File file = new File(musicPath);
if (file.exists()) {
musicGo(musicPath);
controlLis();
showAllTime();
//
if (th == null) {
th = new UpdateProgressThread();
th.start();
}
} else {
new TipDialog(MainFrame2.this, "文件路径错误,请删除该记录", TipDialog.TIP_ERROR);
File errorSound = new File("sound/error.wav");
PlayMusic.play(errorSound, true);
}
}
}
});
/**
* 快进
*/
label_fast.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
//
ImageIcon icon = new ImageIcon("img/fast1.png");
label_fast.setIcon(icon);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/fast.png");
label_fast.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
//
second_current = player.getMediaTime().getSeconds();
if (second_current < second_all - 5) {
second_current = second_current + 5;// 前进5秒
player.setMediaTime(new Time(second_current));
}
String currenttime = (int) (second_current / 60) + ":"
+ (int) second_current % 60;
label_currenttime.setText(currenttime);
double rate = second_current / second_all;
progress = (int) (rate * 1000);
slider_progress.setValue(progress);
}
});
/**
* 快退
*/
label_slow.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
//
ImageIcon icon = new ImageIcon("img/slow1.png");
label_slow.setIcon(icon);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/slow.png");
label_slow.setIcon(icon);
}
public void mousePressed(MouseEvent e) {
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
//
second_current = player.getMediaTime().getSeconds();
if (second_current > 5) {
second_current = second_current - 5;// 后退5秒
player.setMediaTime(new Time(second_current));
}
String currenttime = (int) (second_current / 60) + ":"
+ (int) second_current % 60;
label_currenttime.setText(currenttime);
double rate = second_current / second_all;
progress = (int) (rate * 1000);
slider_progress.setValue(progress);
}
});
/**
* 音量
*/
label_volume.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
if (label_volume.getIcon().toString().equals("img/volume.png")) {
ImageIcon icon = new ImageIcon("img/volume1.png");
label_volume.setIcon(icon);
}
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
vDialog.setLocation(MainFrame2.this.getX() + 635,
MainFrame2.this.getY() + 250);
vDialog.setVisible(true);
}
public void mouseExited(MouseEvent e) {
if (label_volume.getIcon().toString().equals("img/volume1.png")) {
ImageIcon icon = new ImageIcon("img/volume.png");
label_volume.setIcon(icon);
} else {
ImageIcon icon = new ImageIcon("img/volume2.png");
label_volume.setIcon(icon);
}
}
public void mousePressed(MouseEvent e) {
if (label_volume.getIcon().toString().equals("img/volume1.png")) {
ImageIcon icon = new ImageIcon("img/volume2.png");
label_volume.setIcon(icon);
vDialog.slider.setValue(0);
} else {
ImageIcon icon = new ImageIcon("img/volume1.png");
label_volume.setIcon(icon);
vDialog.slider.setValue(70);
}
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
}
});
/**
* 音量对话框消失
*/
vDialog.slider.addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent e) {
vDialog.setVisible(false);
}
});
/**
* 小开关(便签)
*/
label_OFFandON.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (label_OFFandON.getIcon().toString().equals("img/OFF.png")) {
ImageIcon icon = new ImageIcon("img/ON.png");
label_OFFandON.setIcon(icon);
MainFrame2.this.notepadDialog.setVisible(true);
} else {
ImageIcon icon = new ImageIcon("img/OFF.png");
label_OFFandON.setIcon(icon);
MainFrame2.this.notepadDialog.setVisible(false);
}
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
}
});
/**
* 悦音轩
*/
label_yueyinBox.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/yueyinxuan1.png");
label_yueyinBox.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mouseExited(MouseEvent e) {
ImageIcon icon = new ImageIcon("img/yueyinxuan.png");
label_yueyinBox.setIcon(icon);
File file = new File("sound/touch.WAV");
PlayMusic.play(file, flag_touch);
}
public void mousePressed(MouseEvent e) {
File file = new File("sound/click.wav");
PlayMusic.play(file, flag_click);
//
if (isSuccess == true) {
if (switchControl == false) {
boxDialog.setVisible(true);
switchControl = true;
} else {
boxDialog.setVisible(false);
switchControl = false;
}
}
}
});
/**
* 音量调节
*/
vDialog.slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
volume = vDialog.slider.getValue();
float vol = (float) volume / 100;
if (gc != null) {
gc = player.getGainControl();
gc.setLevel(vol);
}
}
});
/**
* 歌曲列表
*/
list.addMouseListener(new MouseAdapter() {// 鼠标事件处理方法,右击弹出菜单
public void mouseClicked(MouseEvent e) {
if (e.getModifiers() == MouseEvent.BUTTON3_MASK) // 单击的是鼠标右键
popupmenu.show(list, e.getX(), e.getY()); // 在鼠标单击处显示快捷菜单
if (e.getClickCount() == 2) { // 双击事件
ImageIcon icon = new ImageIcon("img/pause.png");
label_startAndPause.setIcon(icon);
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
String musicPath = list.getSelectedValue();
File file = new File(musicPath);
if (file.exists()) {
musicGo(musicPath);
controlLis();
showAllTime();
//
if (th == null) {
th = new UpdateProgressThread();
th.start();
}
} else {
new TipDialog(MainFrame2.this, "文件路径错误,请删除该记录", TipDialog.TIP_ERROR);
File errorSound = new File("sound/error.wav");
PlayMusic.play(errorSound, true);
}
}
}
});
/**
* 快捷菜单
*/
popmenuitem[0].addActionListener(new ActionListener() {// 播放
@Override
public void actionPerformed(ActionEvent e) {
ImageIcon icon = new ImageIcon("img/pause.png");
label_startAndPause.setIcon(icon);
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
String musicPath = list.getSelectedValue();
File file = new File(musicPath);
if (file.exists()) {
musicGo(musicPath);
controlLis();
showAllTime();
//
if (th == null) {
th = new UpdateProgressThread();
th.start();
}
} else {
new TipDialog(MainFrame2.this, "文件路径错误,请删除该记录", TipDialog.TIP_ERROR);
File errorSound = new File("sound/error.wav");
PlayMusic.play(errorSound, true);
}
}
});
popmenuitem[1].addActionListener(new ActionListener() {// 删除记录
@Override
public void actionPerformed(ActionEvent e) {
int index = list.getSelectedIndex();
listModel.removeElementAt(index);
if (player != null) {
player.stop();
player.setMediaTime(new Time(0));
}
music_count--;
}
});
popmenuitem[2].addActionListener(new ActionListener() {// 随机播放
@Override
public void actionPerformed(ActionEvent e) {
playModel = model_random;
}
});
popmenuitem[3].addActionListener(new ActionListener() {// 顺序播放
@Override
public void actionPerformed(ActionEvent e) {
playModel = model_order;
}
});
popmenuitem[4].addActionListener(new ActionListener() {// 单曲循环
@Override
public void actionPerformed(ActionEvent e) {
playModel = model_onecircle;
}
});
popmenuitem[5].addActionListener(new ActionListener() {// 列表循环
@Override
public void actionPerformed(ActionEvent e) {
playModel = model_listcircle;
}
});
}
/**
* 初始化player,并为其添加监听事件
*
* @param musicPath
*/
public void musicGo(String musicPath) {
File musicFile = new File(musicPath);
MediaLocator locator = new MediaLocator("file:"
+ musicFile.getAbsolutePath());
try {
player = Manager.createRealizedPlayer(locator);
player.prefetch();// 预读文件
} catch (Exception ex) {
if(ex instanceof CannotRealizeException){
new TipDialog(this, "无法识别该乐曲", TipDialog.TIP_ERROR);
File sound=new File("sound/error.wav");
PlayMusic.play(sound, true);
}
}
}
/**
* 为player的控制加监听
*/
public void controlLis() {
// 必须初始化之后才可以加监听
if (player != null) {
player.addControllerListener(new ControllerListener() {
@Override
public void controllerUpdate(ControllerEvent e) {
// 调节音量
gc = player.getGainControl();
volume = vDialog.slider.getValue();
float vol = (float) volume / 100;
gc.setLevel(vol);
//
if (e instanceof PrefetchCompleteEvent) {// 预读完成执行
player.start();
return;
}
if (e instanceof EndOfMediaEvent) {// 一曲目结束执行
int index;
String musicPath;
switch (playModel) {
case model_random:
player.setMediaTime(new Time(0));
//
Random r = new Random();
index = r.nextInt(music_count);
list.setSelectedIndex(index);// 随机一曲
musicPath = list.getSelectedValue();
musicGo(musicPath);
controlLis();
showAllTime();
//
System.out.println("随机模式");
break;
case model_order:
System.out.println("顺序播放");
player.setMediaTime(new Time(0));
//
index = list.getSelectedIndex();
if (index < music_count - 1) {
list.setSelectedIndex(index + 1);// 下一曲
musicPath = list.getSelectedValue();
musicGo(musicPath);
controlLis();
showAllTime();
}
break;
case model_onecircle:
player.setMediaTime(new Time(0));
player.start();
//
showAllTime();
//
System.out.println("单曲循环模式");
break;
case model_listcircle:
System.out.println("列表循环");
player.setMediaTime(new Time(0));
//
index = list.getSelectedIndex();
if (index < music_count - 1) {
list.setSelectedIndex(index + 1);// 下一曲
musicPath = list.getSelectedValue();
musicGo(musicPath);
controlLis();
showAllTime();
} else {
System.out.println("回到第一首");
list.setSelectedIndex(0);// 回到第一首
musicPath = list.getSelectedValue();
musicGo(musicPath);
controlLis();
showAllTime();
}
break;
default:
break;
}
}
}
});
}else{
return;
}
}
/**
* 显示该首歌曲总时长
*/
public void showAllTime() {
if(player!=null){
second_all = player.getDuration().getSeconds();
String alltime = (int) (second_all / 60) + ":" + (int) second_all % 60;
label_alltime.setText(alltime);
}else {
return;
}
}
class UpdateProgressThread extends Thread {
public void run() {
while (true) {
if (player != null) {
second_current = player.getMediaTime().getSeconds();
String currenttime = (int) (second_current / 60) + ":"
+ (int) second_current % 60;
label_currenttime.setText(currenttime);
double rate = second_current / second_all;
progress = (int) (rate * 1000);
slider_progress.setValue(progress);
try {
Thread.sleep(50);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
new MainFrame2("113悦音");
}
}
贴点效果图吧,感觉直观些…………
工程代码:
http://download.csdn.net/detail/u011700203/8659411