Java——CardLayout卡片式格局

本文介绍了一个使用Java Swing的CardLayout实现卡片式布局的示例程序。通过CardLayout可以轻松地在多个面板之间进行切换,创建类似翻页效果的界面。示例中详细展示了如何创建按钮并为它们添加监听器来控制不同卡片的显示。

/*
 * Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作    者:李晓凯
 * 完成日期:2015年 11 月 24 日
 * 版 本 号:v1.0
 *
 * 问题描述:利用Java编写了一个卡片式的布局程序
 * 输入描述:
 * 程序输出:

 */

CardLayout布局
                                         方法                   说明
public void first(Container parent)
翻转到容器的第一张卡片
public void last(Container parent)翻转到容器的最后一张卡片
public void next(Container parent)翻转到指定容器的下一张卡片
public void previous(Container parent)翻转到指定容器的前一张卡片
public void show(Container parent)翻转到使用addLayoutConmponent 添加到
此布局的具有指定的name的组件
代码:

package Frame;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CardLayoutDemo extends JFrame {
	private JPanel pane = null;
	private JPanel p = null;
	private CardLayout card = null;
	private JButton button_1 = null;
	private JButton button_2 = null;
	private JButton b_1 = null, b_2 = null, b_3 = null;

	private JPanel p_1 = null, p_2 = null, p_3 = null;

	public CardLayoutDemo() {
		card = new CardLayout(5, 5);
		pane = new JPanel(card);
		p = new JPanel();
		button_1 = new JButton("< 上一步");
		button_2 = new JButton("下一步  >");
		b_1 = new JButton("1");
		b_2 = new JButton("2");
		b_3 = new JButton("3");
		b_1.setMargin(new Insets(2, 2, 2, 2));
		b_2.setMargin(new Insets(2, 2, 2, 2));
		b_3.setMargin(new Insets(2, 2, 2, 2));
		p.add(button_1);
		p.add(b_1);
		p.add(b_2);
		p.add(b_3);
		p.add(button_2);
		p_1 = new JPanel();
		p_2 = new JPanel();
		p_3 = new JPanel();
		p_1.setBackground(Color.red);
		p_2.setBackground(Color.blue);
		p_3.setBackground(Color.green);
		p_1.add(new JLabel("JPanel_1"));
		p_2.add(new JLabel("JPanel_2"));
		p_3.add(new JLabel("JPanel_3"));
		pane.add(p_1, "p1");
		pane.add(p_2, "p2");
		pane.add(p_3, "p3");

		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				card.previous(pane);
			}
		});
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				card.next(pane);
			}
		});
		b_1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				card.show(pane, "p1");
			}
		});
		b_2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				card.show(pane, "p2");
			}
		});
		b_3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				card.show(pane, "p3");
			}
		});
		getContentPane().add(pane);
		getContentPane().add(p, BorderLayout.SOUTH);
		setSize(300, 200);
		setVisible(true);
	}

	public static void main(String[] args) {
		new CardLayoutDemo();
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值