一个简单的Java音乐播放器

 更新时间:2017年06月05日 11:49:12   作者:PettyKoKo  
这篇文章主要为大家分享一个简单的Java音乐播放器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java音乐播放器展示的具体代码,供大家参考,具体内容如下

package KKMusic;

 import java.applet.Applet;
 import java.applet.AudioClip;
 import java.awt.BorderLayout;
 import java.awt.EventQueue;
 

 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.border.EmptyBorder;
 import java.awt.CardLayout;
 import javax.swing.JButton;
 import javax.swing.JFileChooser;
 import javax.sound.sampled.FloatControl;
 import javax.sound.sampled.SourceDataLine;
 import javax.swing.GroupLayout;
 import javax.swing.GroupLayout.Alignment;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import javax.swing.LayoutStyle.ComponentPlacement;
 import javax.swing.JMenuBar;
 import javax.swing.JMenuItem;
 import java.awt.event.ItemListener;
 import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.awt.event.ItemEvent;
 import javax.swing.JMenu;
 import javax.swing.JTextField;
 import javax.swing.SwingConstants;
 import javax.swing.JSlider;
 import java.awt.event.MouseMotionAdapter;
 import java.awt.event.MouseEvent;
 

public class Mp3 extends JFrame {
 
private JPanel contentPane;
 File file;//声明文件对象
 String filename;
 JFileChooser chooser=new JFileChooser();//创建一个文件选择器
 private JTextField xiaoxi;
 boolean loop=false;
 AudioClip adc;//声音音频剪辑对象
 SourceDataLine line;
 private FloatControl volume = null;
 /**
 * Launch the application.
 */
 public static void main(String[] args) {
 EventQueue.invokeLater(new Runnable() {
 public void run() {
 try {
 Mp3 frame = new Mp3();
 frame.setVisible(true);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 });
 }
 

/**
 * Create the frame.
 */
 public Mp3() {
 setTitle("\u97F3\u4E50\u64AD\u653E\u5668");
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setBounds(100, 100, 265, 333);
 
JMenuBar menuBar = new JMenuBar();
 setJMenuBar(menuBar);
 
JMenu mnNewMenu = new JMenu("\u6587\u4EF6");
 menuBar.add(mnNewMenu);
 xiaoxi = new JTextField();
 xiaoxi.setColumns(10);
 xiaoxi.setText("欢迎使用本播放器");
 JMenuItem dakai = new JMenuItem("\u6253\u5F00");
 dakai.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 int value=chooser.showOpenDialog(chooser);//接受文件选择器的状态
 if(value==chooser.APPROVE_OPTION){
 file=chooser.getSelectedFile();//返回选中文件
 filename=file.getName();
 String flag=filename;
 xiaoxi.setText(flag);
 try {
 if(adc!=null)
 adc.stop();
 URL url=new URL("file:"+file.getPath());//创建资源定位
 adc=Applet.newAudioClip(url);
 //adc.play();
 
} catch (MalformedURLException e1) {
 // TODO Auto-generated catch block
 e1.printStackTrace();
 System.out.println("不能播放!");
 }
 }
 }
 });
 mnNewMenu.add(dakai);
 
JMenuItem tuichu = new JMenuItem("\u9000\u51FA");
 tuichu.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {

 dispose();
 if(adc!=null)
 adc.stop();
 return ;
 }
 });
 
mnNewMenu.add(tuichu);
 contentPane = new JPanel();
 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
 setContentPane(contentPane);
 
JButton playbtn = new JButton("\u64AD\u653E");
 playbtn.setHorizontalAlignment(SwingConstants.LEFT);
 playbtn.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 
 String flag="正在播放:"+filename;
 if(adc==null){
 flag="请选择播放的音乐";
 xiaoxi.setText(flag);
 return;
 }
 adc.play();
 xiaoxi.setText(flag);
 }
 });
 
JButton stopbtn = new JButton("\u6682\u505C");
 stopbtn.setHorizontalAlignment(SwingConstants.LEFT);
 stopbtn.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 adc.stop();
 String flag="停止播放音乐:"+filename;
 xiaoxi.setText(flag);
 }
 });
 
JButton againbtn = new JButton("\u5FAA\u73AF");
 againbtn.setHorizontalAlignment(SwingConstants.LEFT);
 againbtn.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 loop =!loop;
 String flag="";
 ; if(loop){
 adc.play();
 adc.loop();//循环播放
 flag="循环播放:"+filename;
 }
  else{
  adc.play();
  flag="顺序播放"+filename;
  }
  xiaoxi.setText(flag);
 }
 });
 



GroupLayout gl_contentPane = new GroupLayout(contentPane);
 gl_contentPane.setHorizontalGroup(
 gl_contentPane.createParallelGroup(Alignment.LEADING)
 .addGroup(gl_contentPane.createSequentialGroup()
 .addContainerGap()
 .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
 .addComponent(xiaoxi, Alignment.LEADING)
 .addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
 .addComponent(playbtn, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
 .addPreferredGap(ComponentPlacement.UNRELATED)
 .addComponent(stopbtn, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
 .addPreferredGap(ComponentPlacement.UNRELATED)
 .addComponent(againbtn, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)))
 .addContainerGap(15, Short.MAX_VALUE))
 );
 gl_contentPane.setVerticalGroup(
 gl_contentPane.createParallelGroup(Alignment.TRAILING)
 .addGroup(gl_contentPane.createSequentialGroup()
 .addComponent(xiaoxi, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
 .addPreferredGap(ComponentPlacement.UNRELATED)
 .addPreferredGap(ComponentPlacement.RELATED, 173, Short.MAX_VALUE)
 .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
 .addComponent(playbtn)
 .addComponent(stopbtn)
 .addComponent(againbtn))
 .addContainerGap())
 );
 contentPane.setLayout(gl_contentPane);
 }
 
}

运行结果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java中将bean放入Spring容器中的几种方式详解

    Java中将bean放入Spring容器中的几种方式详解

    这篇文章主要介绍了Java中将bean放入Spring容器中的几种方式详解,在Spring框架中,有多种方式可以将Bean(即对象)放入Spring容器中,今天我们就来详细说一下这几种方式,需要的朋友可以参考下
    2023-07-07
  • java解析sina视频

    java解析sina视频

    本文介绍了一个java解析sina视频地址的例子,从这个例子中可以学习到java使用sax解析xml的方法,大家可以参考修改成其它功能
    2014-01-01
  • Java中反射的学习笔记分享

    Java中反射的学习笔记分享

    反射是Java编程语言中的一个特性。它允许执行的Java程序检查或 操作 自身,并操作程序的内部属性。本文将通过几个示例带大家详细了解一下Java中反射的使用,需要的可以参考一下
    2022-11-11
  • springboot项目启动后执行方法的三种方式

    springboot项目启动后执行方法的三种方式

    有时项目需求,需要项目启动的时候向数据库中查询一下系统属性,或者需要加载某个特定的方法,下面这篇文章主要给大家介绍了关于springboot项目启动后执行方法的三种方式,需要的朋友可以参考下
    2022-06-06
  • IDEA创建SpringBoot的maven项目的方法步骤

    IDEA创建SpringBoot的maven项目的方法步骤

    这篇文章主要介绍了IDEA创建SpringBoot的maven项目的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 详解Java包装类及自动装箱拆箱

    详解Java包装类及自动装箱拆箱

    这篇文章主要介绍了Java包装类及自动装箱拆箱,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • Struts2实现文件上传功能实例解析

    Struts2实现文件上传功能实例解析

    这篇文章主要介绍了Struts2实现文件上传功能实例解析,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-01-01
  • 详解Java创建线程的五种常见方式

    详解Java创建线程的五种常见方式

    Java中如何进行多线程编程,如何使用多线程?不要担心,本文将为你详细介绍一下Java实现线程创建的五种常见方式,感兴趣的可以跟随小编学习一下
    2022-01-01
  • Java(基于Struts2) 分页实现代码

    Java(基于Struts2) 分页实现代码

    这篇文章介绍了Java(基于Struts2) 分页实现代码,有需要的朋友可以参考一下
    2013-10-10
  • MybatisPlus实现逻辑删除功能

    MybatisPlus实现逻辑删除功能

    这篇文章主要介绍了MybatisPlus实现逻辑删除功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12

最新评论