当前位置:文档之家› 一个简单的音乐播放器的java代码

一个简单的音乐播放器的java代码

一个简单的音乐播放器的java代码
一个简单的音乐播放器的java代码

import java.awt.*;

import java.awt.eve nt.*;

import java.io.*;

import java.util.*;

import javax.swi ng.*;

import javax.media.bea n.p layerbea n.*;

import java.i o.I OExce pti on;

import javax.swi ng.JO ptio nPane;

import java.awt.*;

p ublic class lyt exte nds JFrame imp leme nts Runn able {

p ublic Media Player soumd1;

p rivate JButt on p laySo und, stopsound, tjia n,loopSound; p rivate JMe nultem jmiOpenj miSavejmiExitjmiAbout;

p ublic JPanel jp 1, j p2, j p3, j p4;

p ublic JList jl;

p rivate JButt on last, n ext, stop;

p ublic Vector vector, min gche ng;

boolea n fo = false, geshi = false;

JLabel jl1, jl2, sj1, sj2;

JTextField jt1, jt2;

int a = 0, b = 0, you = 1, mm = 0;

p ublic lyt() {

super("200842018java 音乐播放器”); soumd1 = new Media Player();

Container c = getC ontentPan e();

c.setLayout (new FlowLayout());

min gche ng = new Vector();

jp1 = new JPan el();

c.add(j p1);

JMe nuBar mb=new JMe nuBar();

setJMe nuBar(mb);

JMenu fileMenu=new JMenu(” 文件");

mb.add(fileMe nu);

JMenu helpMenu=new JMenu(” 帮助"); mb.add(hel pMenu); fileMenu.add(jmiOpen=new JMenultem(” 打开"));

fileMenu.add(jmiSave=new JMenultem(” 播放"));

helpMenu.add(jmiAbout=new JMenultem(” 关于"));

JPanel jp2 = new JPan el();

jp 2.setLayout (new FlowLayout());

jp2.add(last = new JButton(” 后退"));

jp2.add(playSound = new JButton(” 播放"));

jp2.add(tjian = new JButton(” 添加"));

jp2.add(next = new JButton("前进"));

jp2.add(stopsound = new JButton(” 停止"));

jp2.add(loopSound= new JButton(” 循环")); getCo ntentPan e().add(j p2, BorderLayout.SOUTH);

jp2 = new JPan el(); jp 2.add( playSo un d); jp 2.add(st opsoun d); c.add(j p2);

jp4 = new JPan el(); sj1 = new JLabel(); sj2 = new JLabel(); jp 4.add(sj1);

jp 4.add(sj2);

c.add(j p4);

vector = new Vector(); jl = new JList( min gche ng); jl.setVisibleRowCou

nt(4); jl.setFixedCellHeight(30); jl.setFixedCellWidth(265);

jl.setSelectio nM ode(ListSelectio nM odel.SINGLE_SELECTION)

c.add( new JScroll Pan e(jl));

jp3 = new JPan el();

jp 3.add(tjia n);

c.add(j p3);

try {

Stri ng s = "d:\\music.txt";

Object Inpu tStream input = new Object Inpu tStream( new FileI np

utStream(s));

lujin a1 = (lujin) inpu t.readObject();

min gche ng = a1.b;

vector = a1.a;

jl.setListData( min gche ng);

}

catch (Exce pti on e) {

System.out.printin("D盘没有保存音乐文件");

}

jmiOpen .addActio nListe ner(new Acti on Liste ner() { p ublic void actionP erformed(Acti onEvent eve nt) { JFileChooser fileChooser = new

JFileChooser(); fileChooser

?setFileSelectio nMode(JFileChooser.FILES_AND_DIRECTORIES

);

fileChooser.setCurre ntDirectory( new File(".")); fileChooser

.setFileFilter( new

javax.swi ng.filechooser.FileFilter() {

p ublic boolea n acce pt(File file) {

String n ame = file.getName().toLowerCase(); retur n n ame.e

ndsWith(".m p3")

II file.isDirectory();

p ublic String getDescri pti on() {

return "音乐文件(*.mp3)";

}

});

if (fileChooser.show Open Dialog(lyt.this)== JFileChooser.A PP ROVE_O PTION) {

Stri ng fileName = fileChooser.getSelectedFile()

.getAbsolute Path(); vector.add(fileName); Strin gBuffer buffer = daoxu(fileName); min gche ng.add(buffer);

}

}

});

tjia n.addActio nListe ner(new Acti on Liste ner() {

p ublic void actionP erformed(Act ionEvent event) {

JFileChooser fileChooser = new JFileChooser(); fileChooser

.setFileSelectio nMode(JFileChooser.FILES_AND_DIRECTORIES );

fileChooser.setCurre ntDirectory( new File("."));

fileChooser

.setFileFilter( new javax.swi ng.filechooser.FileFilter() { p ublic boolea n acce pt(File file) { Stri ng n ame =

file.getName().toLowerCase();

retur n n ame.e ndsWith(".m p3")

II file.isDirectory();

P ublic String getDescri pti on() {

return "音乐文件(*.mp3)";

}

});

if (fileChooser.show Open Dialog(lyt.this)== JFileChooser.A PP ROVE_O PTION) {

Stri ng fileName = fileChooser.getSelectedFile()

.getAbsolute Path(); vector.add(fileName); Strin gBuffer buffer = daoxu(fileName); min gche ng.add(buffer);

}

}

});

jmiSave.addActio nListe ner(new Acti on Liste ner() {

p ublic void actionP erformed(Act ionEvent e) {

if (jl.getSelectedI ndex() >= 0) {

String yy = (Stri ng) vector.get(jl.getSelected In dex());

File ff = new File(yy);

if (ff.exists()) {

if (yy.matches("[\\S\\s]*.m p3") ||

yy.matches("[\\S\\s]*.m p3")) {

if (soumd1 != n ull) {

a = 0;

b = 0; you = 0; soumd1.st op();

}

soumd1.setMediaLocati on ("file:/" +

yy);

fo = true;

soumd1.start();

geshi = true;

try {

Thread.slee p(500);

}

catch (Interrup tedExce pti on eee) { }

}

else

JOp tio nPan e.showMessageDialog( null,

"无法播放");

}

else

JOp ti onPan e.showMessageDialog( null,

"此歌曲已经存在");

}

else

JOptionPane.showMessageDialog(null,"请选择音乐文件"); }

});

loopSoun d.addAct ion Liste ner(new Action Liste ner() {

p ublic void actionP erformed(Acti onEvent e) {

if (jl.getSelectedI ndex() >= 0) {

String yy = (Stri ng) vector.get(jl.getSelectedI ndex());

File ff = new File(yy);

if (ff.exists()) {

if (yy.matches("[\\S\\s]*.m p3")|| yy.matches("[\\S\\s]*.M P3")) {

if (soumdl != n ull) {

a = 0;

b = 0;

you = 0;

soumdi.st op();

} soumdI.setMediaLocati on ("file:/" + yy);

fo = true;

soumd1.start();

geshi=true;

try {

Thread.slee p(500);

} catch (Interrup tedExce ptio n we) {

}

} else

JOp tio nPan e.showMessageDialog( null,

"不支持此格式,无法播放");

} else

JOp ti onPan e.showMessageDialog( null,

"此歌曲文件已经存在");

}

else

JOptionPane.showMessageDialog(null,"请选择音乐文件");

}

});

p laySo un d.addActi on Liste ner(new Action Liste ner() { p

ublic void actionP erformed(Act ionEvent e) { if

(jl.getSelectedI ndex() >= 0) {

String yy = (Stri ng) vector.get(jl.getSelectedI ndex());

File ff = new File(yy);

if (ff.exists()) {

if (yy.matches("[\\S\\s]*.m p3") || yy.matches("[\\S\\s]*.m p3")) { if (soumd1 != n ull) {

a = 0;

b = 0;

you = 0;

soumd1.st op();

}

soumd1.setMediaLocati on ("file:/" + yy); fo = true;

soumd1.start(); geshi = true;

try {

Thread.slee p(500);

}

catch (Interrup tedExce pti on eee) {

}

} else

JOp tio nPan e.showMessageDialog( null,

"无法播放");

}

else

JOp tio nPan e.showMessageDialog( nu II,

"此歌曲已经存在");

}

else

JOptionPane.showMessageDialog(null,"请选择音乐文件");

}

});

stopsoun d.addActi on Liste ner(new Action Liste ner() { p ublic void actionP erformed(Act ionEvent e) { a = 0;

b = 0;

you = 0;

mm = 0;

geshi = false;

fo = false;

sj1.setText( null);

sj2.setText( null);

if (jl.getSelectedI ndex() >= 0)

soumd1.st op();

}

});

jl.addMouseListe ner(new MouseAda pter() {

p ublic void mouse Pressed(MouseEve nt eve nt) { if (eve

nt.getClickCou nt() == 2) {

if (jl.getSelectedI ndex() >= 0) {

String yy = (Stri ng)

vector.get(jl.getSelectedI ndex());

File ff = new File(yy);

if (ff.exists()) {

if (yy.matches("[\\S\\s]*.m p3") ||

yy.matches("[\\S\\s]*.m p3")) {

if (soumdl != n ull) {

a = 0;

b = 0;

you = 0;

soumdi.st op();

}

soumdI.setMediaLocati on ("file:/" + yy); fo = true;

soumd1.start();

geshi = true;

try {

Thread.slee p(500);

}

catch (Interrup tedExce pti on e) {

}

}

else

相关主题
文本预览
相关文档 最新文档