CardLayout(显示图片)

来源:互联网 发布:淘宝号怎么用邮箱注册 编辑:程序博客网 时间:2024/05/21 10:08

源代码:

package Test12;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test04 extends JFrame implements ActionListener{
 private CardLayout cardlayout;
 private FlowLayout layout;
 private Icon icon1,icon2,icon3;
 private JLabel label[]=new JLabel[3];
 private JPanel button,image;
 private JButton previous=new JButton("上一张");
 private JButton next=new JButton("下一张");
 //private String name[]={"注意落石","车辆故障","禁止左转","最高限速"};
 private Container c;
 public Test04()
 {
  super("This is test04.java");
  c=getContentPane();
  cardlayout=new CardLayout();
  
  image=new JPanel();
  image.setLayout(cardlayout);
  icon1=new ImageIcon("E:\\MyEclipse9.0_Workspaces\\N012\\src\\Test12\\1.gif");
  icon2=new ImageIcon("E:\\MyEclipse9.0_Workspaces\\N012\\src\\Test12\\2.gif");
  icon3=new ImageIcon("E:\\MyEclipse9.0_Workspaces\\N012\\src\\Test12\\3.gif");
  label[0]=new JLabel(icon1,SwingConstants.LEFT);
  label[1]=new JLabel(icon2,SwingConstants.LEADING);
  label[2]=new JLabel(icon3,SwingConstants.LEADING);
  image.add(label[0]);
  image.add(label[1]);
  image.add(label[2]);
  
  button=new JPanel();
  button.add(previous);
  button.add(next);
  
  layout=new FlowLayout(FlowLayout.CENTER);
  c.setLayout(layout);
  c.add(button);
  c.add(image);

  previous.addActionListener(this);
  next.addActionListener(this);
  setSize(300,150);
  setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
  if(e.getSource()==previous)
   cardlayout.previous(image);
  if(e.getSource()==next)
   cardlayout.next(image);
 }
 public static void main(String args[])
 {
  Test04 app=new Test04();
  app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

}

 

 运行结果1:

 

 运行结果2:

 

 运行结果3: