Swing 中卡片布局管理的简单使用

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class CardLay extends JFrame implements ActionListener{
	
	CardLayout cl = null; 
	JPanel jp1,jp2,jp3,jp4,jp5;
	JButton jb1,jb2,jb3;
	public static void main(String[] args) {
		CardLay a1 =new CardLay();
	}
	public CardLay(){
		cl = new CardLayout();
		
		jp1 = new JPanel(cl);
		jp2 = new JPanel();
		jp2.add(new JLabel("a"));
		
		jp3 = new JPanel();
		jp3.add(new JLabel("b"));
		
		jp4 = new JPanel();
		jp4.add(new JLabel("c"));
		
		jp1.add(jp2, "0");//"0"相当于是给这个Jpanel起了一个名字。下面调用的时候用
		jp1.add(jp3, "1");
		jp1.add(jp4, "2");
		
		jp5 = new JPanel();
		
		jb1 = new JButton("上一张");
		jb1.addActionListener(this);
		jb2 = new JButton("中间");
		jb2.addActionListener(this);
		jb3 = new JButton("下一张");
		jb3.addActionListener(this);
		
		jp5.add(jb1);jp5.add(jb2);jp5.add(jb3);
		
		this.add(jp5, BorderLayout.SOUTH);
		this.add(jp1);
		
		this.setSize(400, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==jb1){
			cl.show(jp1, "0");
		}
		else 
			if(e.getSource()==jb2){
				cl.show(jp1, "1");
			}
			else 
				if(e.getSource()==jb3){
					cl.show(jp1, "2");
				}
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值