中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在Java項目中使用 swing制作一個音樂播放器

發布時間:2020-11-21 16:54:20 來源:億速云 閱讀:180 作者:Leah 欄目:編程語言

這期內容當中小編將會給大家帶來有關在Java項目中使用 swing制作一個音樂播放器,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

實現代碼: 

package com.baiting;

import java.awt.Dimension;
import java.awt.Toolkit;

import com.baiting.menu.CloseWindow;

/**
 * 窗口
 * @author lmq
 *
 */
public abstract class MusicWindow extends Music {
 
 protected MusicFrame musicFrame;
 
 private String title;
 private int locationX;
 private int locationY;
 
 public MusicWindow() {
 title = getConfigMap().get("title").toString();
 defaultLocation();
 }
 
 public MusicWindow(String title,int width,int height) {
 this.title = title;
 setWidth(width);
 setHeight(height);
 defaultLocation();
 }
 
 public MusicWindow(String title,int width,int height,int locationX,int locationY) {
 this.title = title;
 setWidth(width);
 setHeight(height);
 this.locationX = locationX;
 this.locationY = locationY;
 }
 
 private void defaultLocation() {
 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 locationX = (screenSize.width-getWidth())/2;
 locationY = (screenSize.height-getHeight())/2;
 }
 
 protected MusicFrame createWindow() {
 musicFrame = new MusicFrame();
 musicFrame.setTitle(title);
 musicFrame.setSize(getWidth(), getHeight());
 //musicFrame.setLocation(locationX, locationY);
 musicFrame.setLocationRelativeTo(null);
 musicFrame.addWindowListener(new CloseWindow());
 musicFrame.setMinimumSize(new Dimension(600, 450));
 musicFrame.setVisible(true);
 return musicFrame;
 }

 public String getTitle() {
 return title;
 }

 public void setTitle(String title) {
 this.title = title;
 }

 public int getLocationX() {
 return locationX;
 }

 public void setLocationX(int locationX) {
 this.locationX = locationX;
 }

 public int getLocationY() {
 return locationY;
 }

 public void setLocationY(int locationY) {
 this.locationY = locationY;
 }
 
 

 
}

 看看在線下載歌曲的代碼:

package com.baiting.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.baiting.bean.DownFailSong;
import com.baiting.bean.DownedSong;
import com.baiting.http.DownloadSong;
import com.baiting.util.StringUtil;

public class DownloadSongService extends MusicService {

 private static DownloadSongService instance;
 private static Thread downloadThread;
 
 private DownloadSongService() {
 
 }
 
 public static DownloadSongService getInstance() {
 if(null == instance) {
  instance = new DownloadSongService();
 }
 return instance;
 }
 
 public void startDownload() {
 if(null == downloadThread) {
  downloadThread = new Thread(new DownloadSong());
  downloadThread.start();
 } else {
  if(!downloadThread.isAlive()) {
  downloadThread.interrupt();
  downloadThread = null;
  downloadThread = new Thread(new DownloadSong());
  downloadThread.start();
  }
 }
 }
 
 
 public List<DownedSong> getDownedSongAll() {
 File downedListFile = new File(getBasePath()+DOWNLOAD_PATH+"downed.list");
 if(!downedListFile.exists()) {
  try {
  log.info("downed.list文件不存在,正在創建....");
  downedListFile.createNewFile();
  log.info("downed.list文件創建[成功]....");
  } catch (IOException e) {
  log.info("downed.list文件創建[失敗--異常]....");
  e.printStackTrace();
  downedListFile = null;
  return null;
  }
 }
 try {
  BufferedReader reader = new BufferedReader(new FileReader(downedListFile));
   String line = reader.readLine();
   List<DownedSong> list = new ArrayList<DownedSong>();
   int count = 0;
   if(!StringUtil.isEmpty(line)) {
    while(line != null) {
    count++;
    String content = line.replace("\n", "").trim();
   String[] cols = content.split(SEPARATOR);
   if(cols.length>5) {
   DownedSong downedSong = new DownedSong();
   downedSong.setNo(count);
   downedSong.setFileName(cols[0].trim());
   downedSong.setSongName(cols[1].trim());
   downedSong.setSinger(cols[2].trim());
   downedSong.setFileSize(Double.parseDouble(cols[3].trim()));
   downedSong.setPath(cols[4].trim());
   downedSong.setCreateTime(cols[5].trim());
   list.add(downedSong);
   downedSong = null;
   }
   line = reader.readLine();
    }
   }
   reader.close();
   reader = null;
   if(list.size()>0) {
    return list;
   }
   return null;
 } catch (FileNotFoundException e) {
  log.info("文件不存在...");
  e.printStackTrace();
  return null;
 } catch (IOException e) {
  e.printStackTrace();
  return null;
 } finally {
  downedListFile = null;
 }
 }
 
 
 /**
 * 掃描目錄---未完成
 * @return
 */
 public List<DownedSong> getDownedSongByDir() {
 String downedSongDir = getDownloadSongPath();
 File downedDir = new File(downedSongDir);
 if(downedDir.exists() && downedDir.isDirectory()) {
  String[] fileList = downedDir.list();
  for (int i = 0; i < fileList.length; i++) {
  
  }
 }
 return null;
 }
 
 
 /**
 * 判斷歌曲是否存在(通過歌曲名和歌手)
 * @param songName
 * @param singer
 * @return
 */
 public boolean existSongByInfo(String songName,String singer) {
 List<DownedSong> list = getDownedSongAll();
 if(null == list || list.size()<1) {
  return false;
 } 
 boolean flag = false;
 for(DownedSong downedSong : list) {
  if(downedSong.getSongName().equals(songName) && downedSong.getSinger().equals(singer)) {
  flag = true;
  break;
  }
 }
 list = null;
 return flag;
 }
 
 /**
 * 已下載列表中加入新數據
 * @param downedSong
 * @return
 */
 public int addDownedSong(DownedSong downedSong) {
 File downedListFile = new File(getBasePath()+DOWNLOAD_PATH+"downed.list");
 if(!downedListFile.exists()) {
  try {
  log.info("downed.list文件不存在,正在創建....");
  downedListFile.createNewFile();
  log.info("downed.list文件創建[成功]....");
  } catch (IOException e) {
  log.info("downed.list文件創建[失敗--異常]....");
  e.printStackTrace();
  downedListFile = null;
  return -1;
  }
 }
 if(null != downedSong) {
  String contents = downedSong.getFileName()+SEPARATOR+
  downedSong.getSongName()+SEPARATOR+downedSong.getSinger()+SEPARATOR+
  downedSong.getFileSize()+SEPARATOR+downedSong.getPath()+SEPARATOR+
  downedSong.getCreateTime()+"\n";
  try {
  BufferedWriter writer = new BufferedWriter(new FileWriter(downedListFile,true));
  writer.write(contents);
  writer.flush();
  writer.close();
  writer = null;
  List<DownedSong> lists = getDownedSongAll();
  int count = lists.size();
  lists = null;
  return count;
  } catch (IOException e) {
  log.info(downedListFile.getName()+"文件信息寫入[失敗---異常]");
  e.printStackTrace();
  return -1;
  } finally {
  downedListFile = null;
  downedSong = null; 
  }
 }
 return -1;
 }
 
 
 
 
 /**
 * 獲取所有下載失敗的歌曲
 * @return
 */
 public List<DownFailSong> getDownFailSongAll() {
 File downedListFile = new File(getBasePath()+DOWNLOAD_PATH+"downFail.list");
 if(!downedListFile.exists()) {
  try {
  log.info("downFail.list文件不存在,正在創建....");
  downedListFile.createNewFile();
  log.info("downFail.list文件創建[成功]....");
  } catch (IOException e) {
  log.info("downFail.list文件創建[失敗--異常]....");
  e.printStackTrace();
  downedListFile = null;
  return null;
  }
 }
 try {
  BufferedReader reader = new BufferedReader(new FileReader(downedListFile));
   String line = reader.readLine();
   List<DownFailSong> list = new ArrayList<DownFailSong>();
   int count = 0;
   if(!StringUtil.isEmpty(line)) {
    while(line != null) {
    count++;
    String content = line.replace("\n", "").trim();
   String[] cols = content.split(SEPARATOR);
   if(cols.length>3) {
   DownFailSong failSong = new DownFailSong();
   failSong.setNo(count);
   failSong.setSongName(cols[0].trim());
   failSong.setSinger(cols[1].trim());
   failSong.setFormat(cols[2].trim());
   failSong.setFailTime(cols[3].trim());
   list.add(failSong);
   }
   line = reader.readLine();
    }
   }
   reader.close();
   reader = null;
   if(list.size()>0) {
    return list;
   }
   return null;
 } catch (FileNotFoundException e) {
  log.info("文件不存在...");
  e.printStackTrace();
  return null;
 } catch (IOException e) {
  e.printStackTrace();
  return null;
 } catch (Exception e) {
  e.printStackTrace();
  return null;
 } finally {
  downedListFile = null;
 }
 }
 
 
 /**
 * 已下載列表中加入新數據
 * @param downedSong
 * @return
 */
 public int addDownFailSong(DownFailSong downFailSong) {
 File downFailListFile = new File(getBasePath()+DOWNLOAD_PATH+"downFail.list");
 if(!downFailListFile.exists()) {
  try {
  log.info("downFail.list文件不存在,正在創建....");
  downFailListFile.createNewFile();
  log.info("downFail.list文件創建[成功]....");
  } catch (IOException e) {
  log.info("downFail.list文件創建[失敗--異常]....");
  e.printStackTrace();
  downFailSong = null;
  return -1;
  }
 }
 if(null != downFailSong) {
  String contents = downFailSong.getSongName()+SEPARATOR+downFailSong.getSinger()+SEPARATOR+
  downFailSong.getFormat()+SEPARATOR+downFailSong.getFailTime()+"\n";
  try {
  BufferedWriter writer = new BufferedWriter(new FileWriter(downFailListFile,true));
  writer.write(contents);
  writer.flush();
  writer.close();
  List<DownFailSong> lists = getDownFailSongAll();
  return lists.size();
  } catch (IOException e) {
  log.info(downFailListFile.getName()+"文件信息寫入[失敗---異常]");
  e.printStackTrace();
  return -1;
  } catch (Exception e) {
  e.printStackTrace();
  return -1;
  } finally {
  downFailSong = null;
  contents = null;
  }
 }
 return -1;
 }
 
 
 /**
 * 刪除下載失敗的歌曲列表
 * @param no
 * @return
 */
 public boolean delDownFailSong(int no) {
 List<DownFailSong> lists = getDownFailSongAll();
 if(null != lists && lists.size()>0 && lists.size()>=no && no>0) {
  DownFailSong downFailSong = lists.get(no-1);
  log.info("刪除下載失敗的歌曲《"+downFailSong.getSongName()+"》");
  lists.remove(downFailSong);
  
  StringBuffer strBuff = new StringBuffer();
  if(null != lists && lists.size()>0) {
  for(DownFailSong fs : lists) {
   String contents = fs.getSongName()+SEPARATOR+fs.getSinger()+SEPARATOR+
   fs.getFormat()+SEPARATOR+fs.getFailTime()+"\n";
   strBuff.append(contents);
  }
  } else {
  strBuff.append("");
  }
  File downFailListFile = new File(getBasePath()+DOWNLOAD_PATH+"downFail.list");
  if(!downFailListFile.exists()) {
  try {
   log.info("downFail.list文件不存在,正在創建....");
   downFailListFile.createNewFile();
   log.info("downFail.list文件創建[成功]....");
  } catch (IOException e) {
   log.info("downFail.list文件創建[失敗--異常]....");
   e.printStackTrace();
   return false;
  } finally {
   lists = null;
  }
  }
  try {
  BufferedWriter writer = new BufferedWriter(new FileWriter(downFailListFile,false));
  writer.write(strBuff.toString());
  writer.flush();
  writer.close();
  log.info("刪除下載失敗的歌曲《"+downFailSong.getSongName()+"》--成功---");
  return true;
  } catch (IOException e) {
  log.info(downFailListFile.getName()+"文件信息寫入[失敗---異常]");
  e.printStackTrace();
  return false;
  } finally {
  lists = null;
  downFailListFile = null;
  downFailSong = null;
  }
 }
 return false;
 }
} 

上述就是小編為大家分享的在Java項目中使用 swing制作一個音樂播放器了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

茶陵县| 阜平县| 马龙县| 渝北区| 务川| 闻喜县| 新龙县| 麦盖提县| 耿马| 石首市| 宜丰县| 青河县| 江津市| 阜南县| 琼结县| 蒙城县| 正安县| 扎兰屯市| 通江县| 宝清县| 周口市| 大厂| 利辛县| 丹江口市| 葵青区| 建宁县| 清远市| 阳江市| 乐安县| 石首市| 青浦区| 佛冈县| 高阳县| 和田市| 新田县| 拜泉县| 惠水县| 新泰市| 清原| 中牟县| 疏附县|