java图片浏览器代码 java实现浏览器( 二 )


//MP3播放相关库到:;nbsp;下载
//将下载到的zip文件里的 jl1.0.1.jar 复制到 JDK目录下的 jre/lib/ext/ 目录里即可.
//将 源代码 main 方法里的 playMp3("d:\\bad.mp3");改成自己的地址java图片浏览器代码,换种方法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=https://www.04ip.com/post/e.getSource();
if(src=https://www.04ip.com/post/=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; isuf.length; i++)
if(name.endsWith(suf[i]))return true;
return false;
}
});
if(sf.length0){
index=0;
showPic();
}
}
}
if(sf==null||sf.length==0)return;
if(src=https://www.04ip.com/post/=start)startB();
else if(src=https://www.04ip.com/post/=stop)stopB();
else if(src=https://www.04ip.com/post/=next)next();
else if(src=https://www.04ip.com/post/=prev)prev();
}
void prev(){
index=--index0?sf.length-1:index;
showPic();
}
void next(){
index=++indexsf.length-1?0:index;
showPic();
}
public void run(){
while(true){
if(sf!=nullsf.length0autoFlag){
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() {

推荐阅读