?? playlist.java
字號:
package MP3;
/**
*It imports two packages from the Package Function ,So
*that it can put the songs to the list and delete them.
*The other packages are from the Java System that support the appearance and the button events.
*@see www.java.com
*@version since 1.0
*
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.event.*;
import Function.SongString;
import Function.SongType;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;
/**
* This class describes the playlist of Mp3 including the buttons,popupmenu and
* a menu to chage the skill. It extends JFrame ,Created by JFormDesigner on Thu
* Jul 10 21:35:01 CST 2008
*
* @author ChenXiaoling
* @author DuXiaojing
*/
public class Playlist extends JFrame {
SpectrumTimeAnalyzer spectrum = new SpectrumTimeAnalyzer();
/**
* Constructer of the class . Make the JFrame not hava the title bar. Set
* the size and make it unchangable Call the method initComponents() to init
* all the components.
*
*/
public Playlist() {
this.setUndecorated(true);
initComponents();
setSize(295, 271);
setResizable(false);
}
/**
* When you press the Add button ,then invoke this method. Call the method
* to add the songs to the list.
*
*/
private void AddActionPerformed() {
// TODO add your code here
addFileToList();
LoadToFile();
}
/**
* Use a protected hashmap to save the song you want to put into the list
* SongString can save each song after invoke the method loadFiles() Each
* song can not be duplicated.
*
* @see Function.SontString
*
*/
protected void addFileToList() {
SongString[] AllSongs = loadFiles();
if (AllSongs != null) {
for (int i = 0; i < AllSongs.length; i++) {
if (!hashmap.containsKey(AllSongs[i].getName())) {
hashmap.put(AllSongs[i].getName(), AllSongs[i]);
Playlist.add(AllSongs[i].getName());
}
}
}
}
/**
* This method is used to load the files from the computer itself.
*
* @see JFileChooser
* @see SongString
* @return <code>the SongString array</code> if you choose some<code>null</code>
* otherwise.
*/
protected SongString[] loadFiles() {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.setFileFilter(new SongType());
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File[] selectedFiles = chooser.getSelectedFiles();
SongString[] songNames = new SongString[selectedFiles.length];
for (int i = 0; i < songNames.length; i++) {
songNames[i] = new SongString(selectedFiles[i].getName(),
selectedFiles[i].getAbsolutePath());
}
return songNames;
} else {
return null;
}
}
/**
* When pressing the delete button ,invoke Call the method removeFile()
*
*/
private void DeleteActionPerformed() {
// TODO add your code here
removeFile();
LoadToFile();
}
/**
* Use the Hashmap object to remove the selected,and also remove it from the
* list
*
* @see HashMap
*/
private void removeFile() {
String[] selected = Playlist.getSelectedItems();
for (int i = 0; i < selected.length; i++) {
Playlist.remove(selected[i]);
hashmap.remove(selected[i]);
}
}
public void AddListActionPerformed() {
String name = JOptionPane
.showInputDialog("Input the name of the List:");
if (name != null) {
while(model.contains(name))
name = JOptionPane
.showInputDialog("Have one already,Input the name again:");
model.addElement(name);
file = new File(name + ".dat");
while(!file.exists())
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Playlist1.setSelectedIndex(model.getSize()-1);
}
public void DeleteListActionPerformed() {
int n = Playlist1.getSelectedIndex();
if(n==0)
Playlist1.setSelectedIndex(1);
else
Playlist1.setSelectedIndex(0);
model.remove(n);
Playlist.removeAll();
// SwingUtilities.updateComponentTreeUI(this);
}
public void LoadToFile() { // 加載對應(yīng)列表的文件
int count = Playlist.getItemCount();
try {
output = new ObjectOutputStream(new FileOutputStream(model.get(Playlist1.getSelectedIndex())
+ ".dat"));
String order;
SongString song;
for (int i = 0; i < count; i++) {
order = Playlist.getItem(i);
song = (SongString) hashmap.get(order);
record = new Record(order, song.getPath());
output.writeObject(record);
output.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void Reload(String name) {
// =============裝載原有列表==================
try {
input = new ObjectInputStream(new FileInputStream(name + ".dat"));
record = new Record();
record = (Record) input.readObject();
hashmap.clear();
while (record != null) {
hashmap.put(record.getName(), new SongString(record.getName(),
record.getPath()));
Playlist.add(record.getName());
record = (Record) input.readObject();
}
} catch (EOFException end) {
try {
input.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void ListValueChanged() {
Playlist.removeAll();
String name = (String) model.get(Playlist1.getSelectedIndex());
Reload(name);
}
/**
* Add all components into the playlist List object Playlist to show the
* songs ScrollPane object scrollPane1 to add to the Playlist Button object
* Add to add the songs Button object Delete to delete the song you selected
* JMenu object Skin including JMenuItem object
* Green,Challenger,RavenGra,Raven,Autumn to change the skin of the MP3
* PopupMenu object popup to add to the Playlist Add the listeners to each
* components
*
* Use FormLayout to layout all the components.
*
* @see FormLayout
* @author ChenXiaoling
* @author DuXiaojing
*/
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY
// //GEN-BEGIN:initComponents
// Generated using JFormDesigner non-commercial license
// vector = new Vector();
model = new DefaultListModel();
scrollPane1 = new ScrollPane();
Playlist1 = new JList(model);
scrollPane2 = new JScrollPane();
Playlist = new List();
panel2 = new JPanel();
panel1 = new JPanel();
Add = new JButton();
Delete = new JButton();
AddList = new JButton();
DeleteList = new JButton();
Skin = new JMenu("Skin");
Green = new JRadioButtonMenuItem("Default");
Challenger = new JRadioButtonMenuItem("ChallengerDeep");
RavenGra = new JRadioButtonMenuItem("RavenGraphite");
Raven = new JRadioButtonMenuItem("Raven");
Autumn = new JRadioButtonMenuItem("Autumn");
bar = new JMenuBar();
panel = new JPanel();
hashmap = new HashMap();
popup = new PopupMenu();
popup1 = new PopupMenu();
CellConstraints cc = new CellConstraints();
// =========popup=============
String list[] = { "Add songs", "Delete songs" };
MenuItem add = new MenuItem(list[0]);
MenuItem delete = new MenuItem(list[1]);
popup.add(add);
popup.add(delete);
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddActionPerformed();
}
});
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DeleteActionPerformed();
}
});
Playlist.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
check(e);
}
public void mouseReleased(MouseEvent e) {
check(e);
}
private void check(MouseEvent e) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -