当然,比较起我选定的界面而言,还是有不小差距的。
最关键的一点是,人家的歌是可以随便选的。我播放的音乐,是定死的。
不爽,于是继续优化。
首先是手动选歌的问题,写死是自寻死路,用手敲路径也是自寻死路(我自问就没那个耐心一首首的去敲),思来想去,还是要用图形化界面。
于是想到了JFileChooser类。
JFileChooser类自然是要连接到按钮上,一点就是打开文件。然而1个按钮是不够的,还需要至少3个:播放、停止、删除。而且还需要1个状态栏去显示正在播放的文件。
于是动手编写代码。
- package tiantian;
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import javax.media.Manager;
- import javax.media.NoPlayerException;
- import javax.media.Player;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- public class MusicPanel extends JPanel implements ActionListener{
- private File file;//播放文件
- private JLabel jfStatus;//状态栏
- private JButton jbPlay;//播放按钮
- private JButton jbStop;//停止按钮
- private JButton jbOpen;//打开文件按钮
- private JButton jbClose;//关闭文件按钮
- private Music music;//音乐播放类
- public MusicPanel() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//将界面转化成windows风格
- // UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
- // UIManager.setLookAndFeel("com.apple.mrj.swing.MacLookAndFeel");
- jfStatus=new JLabel("未选择文件");//状态栏初始化
- jbPlay=new JButton("播放");//四大按钮初始化,并添加监听
- jbPlay.addActionListener(this);
- jbStop=new JButton("停止");
- jbStop.addActionListener(this);
- jbOpen=new JButton("打开");
- jbOpen.addActionListener(this);
- jbClose=new JButton("关闭");
- jbClose.addActionListener(this);
-
-
- //panel界面初始化
- this.setSize(360, 100);
- this.setVisible(true);
- //布局初始化
- //思来想去我决定先用GridBagLayout,虽然很复杂,但是很精确。
- GridBagLayout gbl=new GridBagLayout();
- gbl.columnWidths=new int[]{7,7,7,7,7,7,7,7};
- gbl.rowHeights=new int[]{7,7,7,7,7,7,7,7};
- this.setLayout(gbl);
- GridBagConstraints gbc0=new GridBagConstraints();
- gbc0.weighty = 0.1;
- gbc0.weightx = 0.1;
- gbc0.gridy = 1;
- gbc0.gridx = 1;
- gbc0.gridwidth=8;
- gbc0.fill = GridBagConstraints.HORIZONTAL;
- gbc0.anchor = GridBagConstraints.CENTER;
- this.add(jfStatus,gbc0);
-
- GridBagConstraints gbc1=new GridBagConstraints();
- gbc1.weighty = 0.1;
- gbc1.weightx = 0.1;
- gbc1.gridy = 2;
- gbc1.gridx = 1;
- gbc1.fill = GridBagConstraints.HORIZONTAL;
- gbc1.anchor = GridBagConstraints.CENTER;
- this.add(jbOpen,gbc1);
-
- GridBagConstraints gbc2=new GridBagConstraints();
- gbc2.weighty = 0.1;
- gbc2.weightx = 0.1;
- gbc2.gridy = 2;
- gbc2.gridx = 3;
- gbc2.fill = GridBagConstraints.HORIZONTAL;
- gbc2.anchor = GridBagConstraints.CENTER;
- this.add(jbClose,gbc2);
- GridBagConstraints gbc3=new GridBagConstraints();
- gbc3.weighty = 0.1;
- gbc3.weightx = 0.1;
- gbc3.gridy = 2;
- gbc3.gridx = 5;
- gbc3.fill = GridBagConstraints.HORIZONTAL;
- gbc3.anchor = GridBagConstraints.CENTER;
- this.add(jbPlay,gbc3);
-
- GridBagConstraints gbc4=new GridBagConstraints();
- gbc4.weighty = 0.1;
- gbc4.weightx = 0.1;
- gbc4.gridy = 2;
- gbc4.gridx = 7;
- gbc4.fill = GridBagConstraints.HORIZONTAL;
- gbc4.anchor = GridBagConstraints.CENTER;
- this.add(jbStop,gbc4);
-
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource()==jbPlay){
- this.play();
- }
- if(e.getSource()==jbStop){
- this.stop();
- }
- if(e.getSource()==jbOpen){
- this.openFile();
- }
- if(e.getSource()==jbClose){
- this.closeFile();
- }
- }
- //关闭文件
- private void closeFile() {
- this.music=null;
- this.jfStatus.setText("未选择文件");
- }
- //打开文件
- private void openFile() {
- JFileChooser jfc=new JFileChooser();
- if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
- this.file=jfc.getSelectedFile();
- jfStatus.setText(file.getName());
- }
- }
- //停止
- private void stop() {
- music.stop();
- }
- //播放
- private void play() {
- try {
- this.music=new Music(file.toURL());
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- this.music.initPlayer();
- this.music.play();
- }
-
- }
复制代码
然后动手编写测试代码。
- package tiantian;
- import java.net.MalformedURLException;
- import javax.swing.JFrame;
- import javax.swing.UnsupportedLookAndFeelException;
- public class Demo {
- public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
- MusicPanel mp=new MusicPanel();
- JFrame jf=new JFrame();
- jf.add(mp);
- jf.pack();
- jf.setVisible(true);
- jf.setLocationRelativeTo(null);
- }
- }
复制代码 运行效果
|