//改编的,CopyOfImageViewer.java 打开一个有图片的文件夹就可浏览了。
//MP3播放相关库到:http://www.javazoom.net/javalayer/sources.html 下载
//将下载到的zip文件里的 jl1.0.1.jar 复制到 JDK目录下的 jre/lib/ext/ 目录里即可.
//将 源代码 main 方法里的 playMp3("d:\\bad.mp3");改成自己的地址,换种方法BMP是可以支持的,这里不行暂不讨论。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javazoom.jl.player.Player;
public class CopyOfImageViewer implements ActionListener,Runnable {
JPanel bts;
JLabel pl;
JScrollPane jsp;
JButton cf,start,next,prev,stop;
JFrame f;
JFileChooser fc;
File [] sf;
int index;
Thread auto;
boolean autoFlag;
int delay=5*1000;
//这里就是GUI布局
CopyOfImageViewer(){
pl=new JLabel();
pl.setHorizontalAlignment(JLabel.CENTER);
jsp=new JScrollPane(pl);
start=new JButton("start");
next=new JButton(">");
prev=new JButton("<");
stop=new JButton("stop");
bts=new JPanel(new FlowLayout(FlowLayout.CENTER));
bts.add(start);
bts.add(prev);
bts.add(next);
bts.add(stop);
cf=new JButton("Select a picture folder");
fc=new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f=new JFrame();
f.setDefaultCloseOperation(3);
f.getContentPane().add(cf,"North");
f.getContentPane().add(jsp,"Center");
f.getContentPane().add(bts,"South");
f.setSize(400,300);
f.setLocationRelativeTo(null);
f.setVisible(true);
//给按钮加入事件侦听器
start.addActionListener(this);
next.addActionListener(this);
prev.addActionListener(this);
stop.addActionListener(this);
cf.addActionListener(this);
auto=new Thread(this);
auto.start();
}
public static void main(String[] args) {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Exception e){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e2){}
}
new CopyOfImageViewer();
playMp3("d:\\bad.mp3");
}
//简单MP3播放
private static void playMp3(String file){
try{
Player p = new Player(new FileInputStream(file));
p.play();
}catch(Exception e){}
}
//处理各按键事件
public void actionPerformed(ActionEvent e) {
Object src=e.getSource();
if(src==cf){
int o=fc.showOpenDialog(f);
if(o==JFileChooser.APPROVE_OPTION){
sf=fc.getSelectedFile().listFiles(new FilenameFilter(){
//合法的文件后缀
String[] suf={".PNG",".GIF",".JPG",};
public boolean accept(File dir, String name) {
name=name.toUpperCase();
for(int i=0; i if(name.endsWith(suf[i]))return true; return false; } }); if(sf.length>0){ index=0; showPic(); } } } if(sf==null||sf.length==0)return; if(src==start)startB(); else if(src==stop)stopB(); else if(src==next)next(); else if(src==prev)prev(); } void prev(){ index=--index<0?sf.length-1:index; showPic(); } void next(){ index=++index>sf.length-1?0:index; showPic(); } public void run(){ while(true){ if(sf!=null && sf.length>0 && autoFlag){ try {Thread.sleep(delay);} catch (Exception e) {} next(); } try {Thread.sleep(100);} catch (Exception e) {} } } private void stopB() { autoFlag=false; } private void startB() { autoFlag=true; } //显示图片 private void showPic() { if(sf==null || sf.length==0)return; pl.setIcon(new ImageIcon(sf[index].getAbsolutePath())); System.out.println(sf[index].getAbsolutePath()); } }
我搞出来了!效果绝对让你满意!呵呵……(但有一点缺陷:没有背景音乐,因为我暂时还没有弄过有音乐的东西)。
准备工作如下(最好你直接HI我,我把整个项目的代码给你):
在根目录(src)下放置1.jpg 2.jpg 3.jpg图片。
代码如下:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.util.*;
public class LiuLanQi extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private int myCounter=1;
private Timer timer=null;
/**
* This is the default constructor
*/
public LiuLanQi() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(375, 343);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(52, 58, 250, 169));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setIcon(new ImageIcon(getClass().getResource("/1.jpg")));
jLabel1.setText("");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(52, 33, 65, 18));
jLabel.setText("我的爱图:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
jContentPane.add(getJButton3(), null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(9, 247, 80, 28));
jButton.setText("开始");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
timer = new Timer();
timer.schedule(new MyTask(), 1000, 2000);
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(98, 247, 80, 28));
jButton1.setText("停止");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
timer.cancel();
}
});
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(187, 247, 80, 28));
jButton2.setText("上一张");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
myCounter-=1;
if(myCounter==0){
myCounter=3;
}
jLabel1.setIcon(new ImageIcon(getClass().getResource("/"+myCounter+".jpg")));
}
});
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setBounds(new Rectangle(276, 247, 80, 28));
jButton3.setText("下一张");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
myCounter+=1;
if(myCounter==4){
myCounter=1;
}
jLabel1.setIcon(new ImageIcon(getClass().getResource("/"+myCounter+".jpg")));
}
});
}
return jButton3;
}
class MyTask extends java.util.TimerTask{
public void run() {
myCounter+=1;
if(myCounter==4){
myCounter=1;
}
jLabel1.setIcon(new ImageIcon(getClass().getResource("/"+myCounter+".jpg")));
}
}
public static void main(String args[]){
new LiuLanQi();
}
} // @jve:decl-index=0:visual-constraint="10,10"
(0079007的答案我也想运行一下看看效果,但代码是有错误的……不能正确编译)
bmp有难度,其它参考楼上的,自己玩去吧
废物东西..
zhanzuo !