已有259人关注
为啥这编码无法实现播放按钮播放音乐,点击播放按钮没有反应?
发表在Java答疑区 2017-07-04
是否精华
版块置顶:

 17:15:02
Smile。 2017/7/4 17:15:02

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.net 

.*;

import com.lzw.MusicPlay;
import com.sun.org 

.apache.xpath.internal.functions.FuncBoolean;

import java.applet.*;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.io 

.File;
import java.net 

.MalformedURLException;

public class a2 extends JFrame  {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextField filePath = null;
private JButton openFile = null;
private JButton playButton = null;
private File selectedFile;
private AudioClip audioClip;

private JTextField getJTextField() {
if (filePath == null) {
filePath = new JTextField();
filePath.setPreferredSize(new Dimension(200, 22));
filePath.setEditable(false);
}
return filePath;
}

private JButton getOpenFile(){
if(openFile==null){
openFile=new JButton();//创建“选择文件”按钮
openFile.setText("选择文件");
//添加按钮事件监听器
openFile.addActionListener(new ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
//创建文件选择器对象
JFileChooser fileChooser=new JFileChooser();
//设置文件 过滤
fileChooser.setFileFilter(new FileNameExtensionFilter(
"支持的音频文件(*.mid、*.wav、*.au、*.mp3、*.mp4、*avi", "wav",
"au", "mid","mp3","mp4","avi"));
//显示文件选择对话框
fileChooser.showOpenDialog(a2.this);
//获取选择的文件对象
selectedFile =fileChooser.getSelectedFile();
    //在文本框中显示文件信息
filePath.setText(selectedFile.getAbsolutePath());
}
});
}
 return openFile;
}
//关于“播放”按钮的方法
private JButton getPlayButton(){
if(playButton ==null){
playButton=new JButton();
playButton.setText("播放");
//添加按钮事件监听器
playButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
// TODO 自动生成的方法存根
if(selectedFile != null){
try{
if(audioClip !=null)
audioClip.stop();
//获取音频剪辑对象
                    audioClip=Applet.newAudioClip(selectedFile
                      .toURI().toURL());
                  audioClip.play();//播放音频
       }catch(MalformedURLException e1){
       e1.printStackTrace();
       }
}
}
});
}
return playButton;
}
public static void main(String[] args) {
MusicPlay thisClass = new MusicPlay();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
public a2() {
super();
initialize();
}
private void initialize() {
this.setSize(408, 79);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new FlowLayout());
jContentPane.add(getJTextField(), null);
jContentPane.add(getOpenFile(), null);
jContentPane.add(getPlayButton(), null);
}
return jContentPane;
}


分享到:
精彩评论 3
David
学分:914 LV7
TA的每日心情
最后的冲刺
2017-07-15 10:46:08
2017-07-06
沙发

读者可以预留一下自己的邮箱,我会发给你一个可运行的、播放音乐的实例,然后读者根据自身需求,再修改自己的代码。

Smile。_1499142995
学分:4 LV1
2017-07-06
板凳

David 发表于2017-07-06 13:49

读者可以预留一下自己的邮箱,我会发给你一个可运行的、播放音乐的实例,然后读者根据自身需求,再修改自己的代码。

729973389@qq.com

David
学分:914 LV7
TA的每日心情
最后的冲刺
2017-07-15 10:46:08
2017-07-06
地板

已将实例代码发送至读者的油箱,请读者注意查收。

首页上一页 1 下一页尾页 3 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照