界面卡片布局小程序

public class One extends JFrame{
 private Two t;
 public One() {
  this.setTitle("图片管理器");
  
  this.setSize(500, 400);
  
  this.setLocationRelativeTo(null);
  
  this.setResizable(false);
  
  run();
  
  
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  this.setVisible(true);
 }
 private void run() {
  t=new Two(this);
  this.add(t);
  
 }
 public static void main(String[] args) {
  new One();
 }
 public Two getT() {
  return t;
 }
 public void setT(Two t) {
  this.t = t;
 }
}

//两个JPanel
public class Two extends JPanel{
 private There there;
 private Four four;
 
 public Two(final One o) {
  this.setLayout(new CardLayout());
  
  there=new There(o);
  four=new Four(o);
  
  this.add(there,"退出");
  this.add(four, "返回");
  
 }
 public There getThere() {
  return there;
 }
 public void setThere(There there) {
  this.there = there;
 }
 public Four getFour() {
  return four;
 }
 public void setFour(Four four) {
  this.four = four;
 }
 
}


//第一个
public class There extends JPanel{
 private Image image;
 
 public There(final One o) {
  this.setLayout(null);
  Five five=new Five("一",70, 80, 110, 30,this);
  Five five1=new Five("二",200, 80, 110, 30,this);
  Five five2=new Five("三",330, 80, 110, 30,this);
  Five five3=new Five("四",70, 150, 110, 30,this);
  Five five4=new Five("五",200, 150, 110, 30,this);
  Five five5=new Five("六",330, 150, 110, 30,this);
  Five five6=new Five("退出",195, 240, 110, 30,this);
  
  image= new ImageIcon("").getImage();
  image= image.getScaledInstance(390, 210, 0);
  
  
  five.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    CardLayout car=(CardLayout) o.getT().getLayout();
    car.show(o.getT(), "返回");
    o.getT().getFour().getJlabel().setIcon(new ImageIcon("picture/1.jpg"));
   }
  });
  
  five1.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    CardLayout car1=(CardLayout) o.getT().getLayout();
    car1.show(o.getT(), "返回");
    o.getT().getFour().getJlabel().setIcon(new ImageIcon("picture/15.jpg"));
    
   }
  });
  
  five2.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    CardLayout car2=(CardLayout) o.getT().getLayout();
    car2.show(o.getT(), "返回");
    o.getT().getFour().getJlabel().setIcon(new ImageIcon("picture/16.jpg"));
    
   }
  });
  
  five3.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    CardLayout car3=(CardLayout) o.getT().getLayout();
    car3.show(o.getT(), "返回");
    o.getT().getFour().getJlabel().setIcon(new ImageIcon("picture/4.jpg"));
    
   }
  });
  
  five4.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    CardLayout car4=(CardLayout) o.getT().getLayout();
    car4.show(o.getT(), "返回");
    o.getT().getFour().getJlabel().setIcon(new ImageIcon("picture/21.jpg"));
    
   }
  });
  
  five5.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    CardLayout car5=(CardLayout) o.getT().getLayout();
    car5.show(o.getT(), "返回");
    o.getT().getFour().getJlabel().setIcon(new ImageIcon("picture/23.jpg"));
    
   }
  });
  
  five6.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
    
   }
  });
 }
}


//第二个
public class Four extends JPanel{
 private JLabel jlabel;
 public Four(final One o) {
  this.setLayout(null);
  
  jlabel= new JLabel();
  jlabel.setBounds(100, 30, 280, 200);
  this.add(jlabel);
  
  Five five = new Five("返回主菜单", 180, 270, 120, 40, this);
  this.add(five);
  
  five.addActionListener(new ActionListener() {
   
   @Override
   public void actionPerformed(ActionEvent e) {
    CardLayout car=(CardLayout) o.getT().getLayout();
    car.show(o.getT(), "退出");
    
   }
  });
  
  
 }
 public JLabel getJlabel() {
  return jlabel;
 }
 public void setJlabel(JLabel jlabel) {
  this.jlabel = jlabel;
 }
}


import java.awt.Container;
import javax.swing.JButton;
/**
 * @param name 按钮名称
 * @param x 按钮横坐标
 * @param y 按钮纵坐标
 * @param width 按钮宽度
 * @param height 按钮高度
 * @param container 加入的容器
 */
public class Five extends JButton{
 public Five(String name,int x,int y,int width,int height,Container container) {
  super(name);
  this.setBounds(x, y, width, height);
  container.add(this);
  
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值