欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一個(gè)簡單的Java音樂播放器

 更新時(shí)間:2017年06月05日 11:49:12   作者:PettyKoKo  
這篇文章主要為大家分享一個(gè)簡單的Java音樂播放器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java音樂播放器展示的具體代碼,供大家參考,具體內(nèi)容如下

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();//創(chuàng)建一個(gè)文件選擇器
 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);//接受文件選擇器的狀態(tài)
 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());//創(chuàng)建資源定位
 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();//循環(huán)播放
 flag="循環(huán)播放:"+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);
 }
 
}

運(yùn)行結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java從list中取出對象并獲得其屬性值的方法

    java從list中取出對象并獲得其屬性值的方法

    這篇文章主要介紹了java從list中取出對象并獲得其屬性值的方法,大家參考使用
    2013-12-12
  • Springboot啟動后執(zhí)行方法小結(jié)

    Springboot啟動后執(zhí)行方法小結(jié)

    本文主要介紹了Springboot啟動后執(zhí)行方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Java中監(jiān)聽器Listener詳解

    Java中監(jiān)聽器Listener詳解

    Listener是由Java編寫的WEB組件,主要完成對內(nèi)置對象狀態(tài)的變化 (創(chuàng)建、銷毀)和屬性的變化進(jìn)行監(jiān)聽,做進(jìn)一步的處理,主要對session和application內(nèi)置對象監(jiān)聽,這篇文章主要介紹了Java中監(jiān)聽器Listener,需要的朋友可以參考下
    2023-08-08
  • Java中bcrypt算法實(shí)現(xiàn)密碼加密的方法步驟

    Java中bcrypt算法實(shí)現(xiàn)密碼加密的方法步驟

    我們可以在Spring Boot和SSM中實(shí)現(xiàn)密碼加密,使用bcrypt算法可以保障密碼的安全性,并且減少了手動編寫哈希函數(shù)的工作量,本文就來詳細(xì)的介紹一下,感興趣的可以了解一下
    2023-08-08
  • java使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播

    java使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播

    這篇文章主要為大家詳細(xì)介紹了java使用MulticastSocket實(shí)現(xiàn)多點(diǎn)廣播,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Idea2020 無法share項(xiàng)目到svn的解決方法

    Idea2020 無法share項(xiàng)目到svn的解決方法

    這篇文章主要介紹了Idea2020 無法share項(xiàng)目到svn的解決方法,需要的朋友可以參考下
    2020-09-09
  • java實(shí)現(xiàn)簡單的計(jì)算器類實(shí)例

    java實(shí)現(xiàn)簡單的計(jì)算器類實(shí)例

    這篇文章主要介紹了java實(shí)現(xiàn)簡單的計(jì)算器類,涉及java針對鍵盤監(jiān)聽及數(shù)字運(yùn)算的處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • JsonFormat與@DateTimeFormat注解實(shí)例解析

    JsonFormat與@DateTimeFormat注解實(shí)例解析

    這篇文章主要介紹了JsonFormat與@DateTimeFormat注解實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • SpringBoot項(xiàng)目實(shí)戰(zhàn)之?dāng)?shù)據(jù)交互篇

    SpringBoot項(xiàng)目實(shí)戰(zhàn)之?dāng)?shù)據(jù)交互篇

    這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目實(shí)戰(zhàn)之?dāng)?shù)據(jù)交互篇的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-03-03
  • java socket接收保證能讀完數(shù)據(jù)的實(shí)例

    java socket接收保證能讀完數(shù)據(jù)的實(shí)例

    這篇文章主要介紹了java socket接收保證能讀完數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評論