- *package seeimage;
- /*
- * 看图软件
- * 主要用CardLayout布局
- *
- *
- *
- *
- * */
- import java.awt.BorderLayout;
- import java.awt.CardLayout;
- 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;
- class ShowImage extends JFrame implements ActionListener
- {
- CardLayout cartlayout = new CardLayout(10, 5);
- // 设置面板cardPanel按CardLayout布局
- JPanel cardPanel = new JPanel(cartlayout);
- JButton jbFirst, jbLast, jbPrevious, jbNext;
- ShowImage()
- {
- //创建表示7张图片标签,加入面板cardPanel
- for (int i = 1; i <= 7; i++)
- {
- JLabel label = new JLabel(new ImageIcon("images/" + i + ".gif"));
- cardPanel.add(label, String.valueOf(i)); //卡片名必须是String型
- }
- JPanel p = new JPanel();
- // 创建按钮对象并加入面板p
- p.add(jbFirst = new JButton("第一张"));
- p.add(jbLast = new JButton("最后张"));
- p.add(jbPrevious = new JButton("前一张"));
- p.add(jbNext = new JButton("后一张"));
- add(cardPanel); //把面板cardPanel加入窗口中区
- add(p,BorderLayout.SOUTH); //把面板p加入窗口南区
- cartlayout.first(cardPanel); //显示第1张卡片
- jbFirst.addActionListener(this);
- jbLast.addActionListener(this);
- jbPrevious.addActionListener(this);
- jbNext.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==jbFirst)
- cartlayout.first(cardPanel);
- else if(e.getSource()==jbLast)
- cartlayout.last(cardPanel);
- else if(e.getSource()==jbPrevious)
- cartlayout.previous(cardPanel);
- else if(e.getSource()==jbNext)
- cartlayout.next(cardPanel);
- }
- }
- public class SeeImage
- {
- public static void main(String[] args)
- {
- ShowImage f = new ShowImage();
- f.setTitle("展示图片");
- f.setSize(350, 300);
- f.setLocation(400, 300);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setVisible(true);
- }
- }
复制代码
|