这是Main.java
package ombproj;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;public class Main {
public static void main(String[] args) throws UnsupportedEncodingException {
OMB_GUI A = new OMB_GUI();
A.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
这是OMB_GUI
public class OMB_GUI extends JFrame { .................. OMB_GUI() {
NumListMusic = 0;
musicl = new MusicListItem[20];
for (int i = 0; i < musicl.length; i++) {
musicl[i] = new MusicListItem();
} mal = new MyActionListener();
this.setTitle("Online Music Box"); container = new JPanel(); setContentPane(container);
container.setLayout(new BorderLayout()); container.add(PPlayer = new JPanel(), BorderLayout.NORTH); container.add(PT5list = new JPanel(), BorderLayout.WEST); container.add(PMlist = new JPanel(), BorderLayout.CENTER); container.add(PMmlist = new JPanel(), BorderLayout.EAST); container.add(PLrclist = new JPanel(), BorderLayout.SOUTH); PPlayer.setLayout(new GridLayout(3, 1));
PPlayer.add(PMusicinf = new JPanel());
PPlayer.add(PProgress = new JPanel());
PPlayer.add(PCon_Search = new JPanel()); PMusicinf.add(new JLabel("音乐信息")); // PProgress.setLayout(new GridLayout(1, 2));
PProgress.add(PTime = new JPanel());
PProgress.add(PNx_Pe = new JPanel());
PNx_Pe.add(BNext = new JButton("下一首"));
BNext.addActionListener(mal);
PNx_Pe.add(BPrevios = new JButton("上一首"));
BPrevios.addActionListener(mal);
JPanel PControl, PSearch;
PCon_Search.setLayout(new GridLayout(1, 2));
PCon_Search.add(PControl = new JPanel());
PCon_Search.add(PSearch = new JPanel()); PControl.add(BPlay = new JButton("播放"));
BPlay.addActionListener(mal);
PControl.add(BPause = new JButton("暂停"));
BPause.addActionListener(mal);
PControl.add(BStop = new JButton("停止"));
BStop.addActionListener(mal);
TSearch = new JTextField();
TSearch.setColumns(10); PSearch.add(TSearch);
PSearch.add(BSearch = new JButton("搜索"));
PSearch.add(BHide_pl = new JButton("显示播放列表"));
BHide_pl.addActionListener(mal);
BSearch.addActionListener(mal); //---------------------------------------------------------- PT5list.setLayout(new GridLayout(15, 1));
PT5list.add(BT500 = new JButton("歌曲Top500"));
BT500.addActionListener(mal);
PT5list.add(BSingers = new JButton("歌手Top50"));
BSingers.addActionListener(mal);
PT5list.add(BNewSongs = new JButton("新歌Top200"));
BNewSongs.addActionListener(mal); PMlist.setLayout(new BorderLayout());
PMlist.add(PList = new JPanel(), BorderLayout.CENTER);
PMlist.add(PListControl = new JPanel(), BorderLayout.SOUTH); // PMlist.add(new JLabel("歌曲列表")); // PMmlist.add(new JLabel("播放列表"));
PMmlist.setLayout(new BorderLayout());
PMmlist.add(PMusiclist = new JPanel(), BorderLayout.CENTER);
PMmlist.add(PMusiclistcontrol = new JPanel(), BorderLayout.SOUTH); /******************************************************/
//-----------------播放歌词部分
LRC_label1 = new JLabel();
LRC_label2 = new JLabel();
LRC_label3 = new JLabel();
PLrclist.setLayout(new GridLayout(3, 1));
PLrclist.add(LRC_label1);
PLrclist.add(LRC_label2);
PLrclist.add(LRC_label3);
/****************************************/
this.setSize(800, 600); this.setVisible(true);
/********************************************
try {
// Lrc_Thread=new Thread( GUI_point = new Point(); GUI_point = this.getLocationOnScreen();
int point_x,point_y;
point_x=(int)GUI_point.getX();
point_y=(int)GUI_point.getY();
System.out.println(point_x);
System.out.println(point_y);
GUI_point =new Point(point_x,point_y+600);
Lrc_Thread =new Thread(new OMB_lrc("你到底爱不爱我",GUI_point));
//lrc.setToLocation(GUI_point);
Lrc_Thread.run();
// Lrc_Thread.start();
} catch (IOException ex) {
Logger.getLogger(OMB_GUI.class.getName()).log(Level.SEVERE, null, ex);
}
****************************************/
} private void showPlayList() {
PMusiclist.removeAll();
PMusiclistcontrol.removeAll();
String[] Lists = new String[musicl.length];
for (int i = 0; i < musicl.length; i++) {
Lists[i] = musicl[i].getMusicName();
}
Mmlist = new JList(Lists);
Mmlist.setFixedCellWidth(100);
Mmlist.setFixedCellHeight(20);
PMusiclist.add(Mmlist);
BHide_pl.setText("隐藏播放列表");
} private class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) {
if (e.getSource() == BPlay) {
String playurl = "";
NowPlayingIndex = Mmlist.getSelectedIndex();
playurl = musicl[NowPlayingIndex].getURLString();
if (pl_Thread != null) {
pl_Thread.stop();
}
try {
//System.out.println(musicl[NowPlayingIndex].getMusicName());
Lrc_Thread = new OMB_lrc("大海", LRC_label1, LRC_label2, LRC_label3);
Lrc_Thread.start();
} catch (IOException ex) {
//Logger.getLogger(OMB_GUI.class.getName()).log(Level.SEVERE, null, ex);
}
pl_Thread = new Thread(new OMBPlayer(playurl));
pl_Thread.start();
BPause.setText("暂停");
} else if (e.getSource() == BStop) {
pl_Thread.stop();
} else if (e.getSource() == BPause) {
if (pl_Thread != null) {
if (BPause.getText().compareTo("暂停") == 0) {
pl_Thread.suspend();
BPause.setText("继续");
} else {
pl_Thread.resume();
BPause.setText("暂停");
}
}
}
}
....................
package ombproj;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;public class Main {
public static void main(String[] args) throws UnsupportedEncodingException {
OMB_GUI A = new OMB_GUI();
A.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
这是OMB_GUI
public class OMB_GUI extends JFrame { .................. OMB_GUI() {
NumListMusic = 0;
musicl = new MusicListItem[20];
for (int i = 0; i < musicl.length; i++) {
musicl[i] = new MusicListItem();
} mal = new MyActionListener();
this.setTitle("Online Music Box"); container = new JPanel(); setContentPane(container);
container.setLayout(new BorderLayout()); container.add(PPlayer = new JPanel(), BorderLayout.NORTH); container.add(PT5list = new JPanel(), BorderLayout.WEST); container.add(PMlist = new JPanel(), BorderLayout.CENTER); container.add(PMmlist = new JPanel(), BorderLayout.EAST); container.add(PLrclist = new JPanel(), BorderLayout.SOUTH); PPlayer.setLayout(new GridLayout(3, 1));
PPlayer.add(PMusicinf = new JPanel());
PPlayer.add(PProgress = new JPanel());
PPlayer.add(PCon_Search = new JPanel()); PMusicinf.add(new JLabel("音乐信息")); // PProgress.setLayout(new GridLayout(1, 2));
PProgress.add(PTime = new JPanel());
PProgress.add(PNx_Pe = new JPanel());
PNx_Pe.add(BNext = new JButton("下一首"));
BNext.addActionListener(mal);
PNx_Pe.add(BPrevios = new JButton("上一首"));
BPrevios.addActionListener(mal);
JPanel PControl, PSearch;
PCon_Search.setLayout(new GridLayout(1, 2));
PCon_Search.add(PControl = new JPanel());
PCon_Search.add(PSearch = new JPanel()); PControl.add(BPlay = new JButton("播放"));
BPlay.addActionListener(mal);
PControl.add(BPause = new JButton("暂停"));
BPause.addActionListener(mal);
PControl.add(BStop = new JButton("停止"));
BStop.addActionListener(mal);
TSearch = new JTextField();
TSearch.setColumns(10); PSearch.add(TSearch);
PSearch.add(BSearch = new JButton("搜索"));
PSearch.add(BHide_pl = new JButton("显示播放列表"));
BHide_pl.addActionListener(mal);
BSearch.addActionListener(mal); //---------------------------------------------------------- PT5list.setLayout(new GridLayout(15, 1));
PT5list.add(BT500 = new JButton("歌曲Top500"));
BT500.addActionListener(mal);
PT5list.add(BSingers = new JButton("歌手Top50"));
BSingers.addActionListener(mal);
PT5list.add(BNewSongs = new JButton("新歌Top200"));
BNewSongs.addActionListener(mal); PMlist.setLayout(new BorderLayout());
PMlist.add(PList = new JPanel(), BorderLayout.CENTER);
PMlist.add(PListControl = new JPanel(), BorderLayout.SOUTH); // PMlist.add(new JLabel("歌曲列表")); // PMmlist.add(new JLabel("播放列表"));
PMmlist.setLayout(new BorderLayout());
PMmlist.add(PMusiclist = new JPanel(), BorderLayout.CENTER);
PMmlist.add(PMusiclistcontrol = new JPanel(), BorderLayout.SOUTH); /******************************************************/
//-----------------播放歌词部分
LRC_label1 = new JLabel();
LRC_label2 = new JLabel();
LRC_label3 = new JLabel();
PLrclist.setLayout(new GridLayout(3, 1));
PLrclist.add(LRC_label1);
PLrclist.add(LRC_label2);
PLrclist.add(LRC_label3);
/****************************************/
this.setSize(800, 600); this.setVisible(true);
/********************************************
try {
// Lrc_Thread=new Thread( GUI_point = new Point(); GUI_point = this.getLocationOnScreen();
int point_x,point_y;
point_x=(int)GUI_point.getX();
point_y=(int)GUI_point.getY();
System.out.println(point_x);
System.out.println(point_y);
GUI_point =new Point(point_x,point_y+600);
Lrc_Thread =new Thread(new OMB_lrc("你到底爱不爱我",GUI_point));
//lrc.setToLocation(GUI_point);
Lrc_Thread.run();
// Lrc_Thread.start();
} catch (IOException ex) {
Logger.getLogger(OMB_GUI.class.getName()).log(Level.SEVERE, null, ex);
}
****************************************/
} private void showPlayList() {
PMusiclist.removeAll();
PMusiclistcontrol.removeAll();
String[] Lists = new String[musicl.length];
for (int i = 0; i < musicl.length; i++) {
Lists[i] = musicl[i].getMusicName();
}
Mmlist = new JList(Lists);
Mmlist.setFixedCellWidth(100);
Mmlist.setFixedCellHeight(20);
PMusiclist.add(Mmlist);
BHide_pl.setText("隐藏播放列表");
} private class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) {
if (e.getSource() == BPlay) {
String playurl = "";
NowPlayingIndex = Mmlist.getSelectedIndex();
playurl = musicl[NowPlayingIndex].getURLString();
if (pl_Thread != null) {
pl_Thread.stop();
}
try {
//System.out.println(musicl[NowPlayingIndex].getMusicName());
Lrc_Thread = new OMB_lrc("大海", LRC_label1, LRC_label2, LRC_label3);
Lrc_Thread.start();
} catch (IOException ex) {
//Logger.getLogger(OMB_GUI.class.getName()).log(Level.SEVERE, null, ex);
}
pl_Thread = new Thread(new OMBPlayer(playurl));
pl_Thread.start();
BPause.setText("暂停");
} else if (e.getSource() == BStop) {
pl_Thread.stop();
} else if (e.getSource() == BPause) {
if (pl_Thread != null) {
if (BPause.getText().compareTo("暂停") == 0) {
pl_Thread.suspend();
BPause.setText("继续");
} else {
pl_Thread.resume();
BPause.setText("暂停");
}
}
}
}
....................
public class OMB_lrc extends Thread { private JPanel container;
private JLabel label1, label2, label3;
String SongName = null;//歌曲名字,供下载歌词文件用
String LRC_tmp = null;//歌词字符串,保存从歌词文件中读取的数据
String LRC_file_URL = null;
String LRC_temp_file = ".lrc_tmp.lrc";
String LRC_final_file = "lrc_file.lrc";
String LRC_tmp_html_file = ".lrc_tmp.html";
String LRC_html_URL = null;
String LRC_tmp_string = null;
Point lrc_point;
// String[] LRC_array_string = new String[100];
float[] LRC_array_time = new float[100];
ArrayList array_string = new ArrayList(); public OMB_lrc(String SName, JLabel l1, JLabel l2, JLabel l3) throws IOException {
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
label1 = l1;
label2 = l2;
label3 = l3;
SongName = SName;
LRC_html_URL = Net.returnLRC_URL(SongName);
Net.saveToFile(LRC_html_URL, LRC_tmp_html_file);
String LRC_html_content = null;
LRC_html_content = this.LRC_load_to_string(LRC_tmp_html_file);
LRC_file_URL = this.LRC_match_html(LRC_html_content);
Net.saveToFile(LRC_file_URL, LRC_temp_file);
Net.transferFile(LRC_temp_file, LRC_final_file);
LRC_tmp_string = this.LRC_load_to_string(LRC_final_file); this.LRC_analyze(LRC_tmp_string); // this.setUndecorated(true);
// this.setSize(800, 80); // this.show_LRC();
}
............ public void show_LRC() {
try { Thread.sleep((int) (LRC_array_time[0] * 100));
} catch (InterruptedException ex) { }
for (int i = 1; i < (array_string.size() - 1); i++) {
try {
label1.setText(array_string.get(i - 1).toString());
label2.setForeground(Color.RED);
label2.setText(array_string.get(i).toString());
label3.setText(array_string.get(i + 1).toString()); Thread.sleep((int) (LRC_array_time[i + 1] * 1000 - LRC_array_time[i] * 1000));
} catch (InterruptedException ex) { }
}
} public void run() {
this.show_LRC();
}
}