六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

java之图片浏览器

[复制链接]

升级  93.8%

309

主题

309

主题

309

主题

进士

Rank: 4

积分
969
 楼主| 发表于 2013-1-26 13:37:59 | 显示全部楼层 |阅读模式
图片在当前工作环境下添加。
import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;public class my extends JFrame  implements ActionListener{JFrame f;JPanel p1,p2;JLabel p;JButton pre= new JButton("前一张");JButton next=new JButton("后一张");ImageIcon []ic={new ImageIcon("img1.gif"),new ImageIcon("img2.gif"),new ImageIcon("img3.gif"),new ImageIcon("img4.gif"),new ImageIcon("img5.gif")};int index=0;public my(){f=new JFrame("图像浏览器");//框架f.setSize(1400,1500);f.setVisible(true);Container c=f.getContentPane();//添加容器c.setLayout(new GridLayout(2,1));p1=new JPanel();//两个面板p2=new JPanel();c.add(p1);c.add(p2);p2.add(pre);p2.add(next);pre.addActionListener(this);next.addActionListener(this);p=new JLabel(ic[index]);p1.add(p);}public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()==pre){if(index==0) index=4;else index--;showPic();}if(e.getSource()==next){index=(index+1)%5;showPic();}}private void showPic() {  p.setIcon(ic[index]);  }public static void main(String args[]){new my();}}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表