java之图片浏览器
图片在当前工作环境下添加。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 JFrameimplements 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);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);}public static void main(String args[]){new my();}}
页:
[1]