用Java写的图片浏览器

                    用Java写的图片浏览器

package com.wenbo.myprogrem.day16;


import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class MyPicture implements ActionListener{
 CardLayout cl=new CardLayout();
 JPanel jp1=new JPanel();
 Timer timer=new Timer(2000,this);//设置timer延迟时间,注册监听器
 public MyPicture(){
 timer.setActionCommand("下一张");
  JFrame jf=new JFrame("图片浏览窗口");
  jp1.setLayout(cl);
  JPanel jp2=new JPanel();
  for(int i=0;i<30;i++){
   Icon ic=new ImageIcon("C:/Documents and Settings/Administrator/桌面/tupian/tu"+(i+1)+".gif");
   JLabel jl=new JLabel(ic);
   jp1.add(jl,(i+1)+"");
  }
  jf.add(jp1);
  String [] st={"PLAY","第一张","下一张","上一张","最后一张","STOP"};
  for(int i=0;i   JButton jb=new JButton(st[i]);
   jb.addActionListener(this);
   jp2.add(jb);
  }
  
  jf.add(jp2,BorderLayout.SOUTH);

  
  jf.setSize(520,500);//设置窗口大小
  jf.setLocation(300, 400);//设置出现在屏幕的位置
  jf.setVisible(true);//设置可见
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点X关闭
 }
 public static void main(String[] args) {

  new MyPicture();
 }
 public void actionPerformed(ActionEvent e) {
  String comm=e.getActionCommand();
  if("第一张".equals(comm)){
   cl.first(jp1);
  }else if("下一张".equals(comm)){
   cl.next(jp1);
  }else if("上一张".equals(comm)){
   cl.previous(jp1);
  }else if("最后一张".equals(comm)){
   cl.last(jp1);
  }else if("PLAY".equals(comm)){
   timer.setActionCommand("下一张");
   timer.start();
  }else if("STOP".equals(comm)){
   timer.stop();
  }
 }

}

你可能感兴趣的:(用Java写的图片浏览器)