关于用java播放mp3代码的信息( 三 )


import java.awt.BorderLayout;
import javax.swing.JSplitPane;
import java.awt.Component;
import java.util.Vector;
public class JMF_T extends JFrame implements ControllerListener, ActionListener {
MediaPlayer Player;
private String filename = "";
private static final long serialVersionUID = 1L;
private Vector vct = new Vector(); // @jve:decl-index=0:
private JPanel jContentPane = null;
private JSplitPane jSplitPane = null;
private JPanel playPanel = null;
private JList jList = null;
private JSplitPane getJSplitPane() {
if (jSplitPane == null) {
jSplitPane = new JSplitPane();
jSplitPane.setDividerSize(5);
jSplitPane.setResizeWeight(0.8);
jSplitPane.setRightComponent(getJList());
jSplitPane.setLeftComponent(getPlayPanel());
}
return jSplitPane;
}
private JPanel getPlayPanel() {
if (playPanel == null) {
playPanel = new JPanel();
playPanel.setLayout(new BorderLayout());
}
return playPanel;
}
private JList getJList() {
if (jList == null) {
jList = new JList();
jList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
// TODO Auto-generated Event stub mouseClicked()
if (e.getClickCount() == 1) {
String str = (String) jList.getSelectedValue();
if (str == null) {
return;
}
filename = str;
System.out.println(str);
}
if (e.getClickCount() == 2) {
String str = (String) jList.getSelectedValue();
if (str == null) {
return;
}
filename = str;
play();
}
}
});
}
return jList;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JMF_T thisClass = new JMF_T();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public JMF_T() {
initialize();
}
private void OpenFile() {
FileDialog fd = new FileDialog(this, "Choose Video", FileDialog.LOAD);
fd.setVisible(true);
filename = fd.getDirectory() + fd.getFile();
System.out.println(filename);
if (filename.equals("")) {
return;
} else if (filename.equals("nullnull")) {
return;
}
boolean j = false;
for (int i = 0; ivct.size(); i++) {
if (vct.get(i).toString().equals(filename)) {
j = true;
break;
}
}
if (j == false) {
vct.add(filename);
jList.setListData(vct);
}
}
private void stop() {
if (Player != null) {
Player.stop();
Player.deallocate();
}
}
private void play() {
try {
if (filename.equals("")) {
return;
}
if (Player == null) {
Player = new MediaPlayer();
} else {
closePreviosPlayer();
}
Player.setMediaLocator(new MediaLocator("" + filename));
Player.addControllerListener(this);
Player.realize();
Player.start();
} catch (Exception e) {
}
}
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand().toString();
if (action.equals("Open")) {
OpenFile();
}
if (action.equals("Play")) {
play();
}
if (action.equals("Stop")) {
stop();
}
if (action.equals("Exit")) {
dispose();
System.exit(0);
}
}
private void initialize() {
this.setSize(500, 350);
setLocation(300, 100);
this.setContentPane(getJContentPane());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
MenuBar mb = new MenuBar();
setMenuBar(mb);

推荐阅读