Java学习笔记之卡片式布局CardLayout

CardLayout(构造)方法:

方法说明
public CardLayout()创建一个间距大小为0的新卡片布局
public CardLayout(int hgap,int vgap)创建一个具有指定水平间距和垂直间距的新卡片布局。水平间距置于左右边缘。垂直间距置于上下边缘
public void first(Container parent)翻转到容器的第一张卡片
public void last(Contain parent)翻转到容器的最后一张卡片
public void next(Contain parent)翻转到指定容器的下一张卡片

public void previous(Contain

parent)

翻转到指定容器的前一张卡片
public void show(Contain parent,String name)翻转到使用addLayoutComponent添加到此布局的具有指定name的组件

示例:

package com.lc.awt;

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 arg0) {
				card.previous(pane);
			}
		});
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				card.next(pane);
			}
		});
		b_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				card.show(pane,"p1");
			}
		});
		b_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				card.show(pane,"p2");
			}
		});
		b_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				card.show(pane,"p3");
			}
		});
		this.getContentPane().add(pane);
		this.getContentPane().add(p,BorderLayout.SOUTH);
		this.setSize(300, 200);
		this.setVisible(true);
	}
	
	
	public static void main(String[] args) {
		new CardLayoutDemo();
	}

}

运行结果:


  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Java中使用CardLayout切换卡片,您需要使用以下步骤: 1. 创建一个CardLayout对象。 ``` CardLayout cardLayout = new CardLayout(); ``` 2. 将CardLayout对象设置为容器的布局管理器。 ``` container.setLayout(cardLayout); ``` 3. 添加每个卡片作为一个组件,并使用字符串标识每个卡片。 ``` container.add(component1, "card1"); container.add(component2, "card2"); ``` 4. 要切换到另一个卡片,请调用CardLayout对象的show()方法,并传递该卡片的字符串标识。 ``` cardLayout.show(container, "card2"); ``` 以下是一个完整的示例代码,演示如何使用CardLayout切换卡片: ``` import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CardLayoutDemo extends JFrame implements ActionListener { private JPanel cards; public CardLayoutDemo() { setTitle("CardLayout示例"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建卡片 JPanel card1 = new JPanel(); card1.add(new JLabel("这是第1个卡片")); JPanel card2 = new JPanel(); card2.add(new JLabel("这是第2个卡片")); // 创建容器并设置布局管理器 cards = new JPanel(new CardLayout()); cards.add(card1, "card1"); cards.add(card2, "card2"); // 创建按钮并添加监听器 JButton button1 = new JButton("显示第1个卡片"); button1.addActionListener(this); JButton button2 = new JButton("显示第2个卡片"); button2.addActionListener(this); // 将卡片和按钮添加到窗口中 Container contentPane = getContentPane(); contentPane.add(cards, BorderLayout.CENTER); contentPane.add(button1, BorderLayout.NORTH); contentPane.add(button2, BorderLayout.SOUTH); pack(); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("显示第1个卡片")) { CardLayout cardLayout = (CardLayout) cards.getLayout(); cardLayout.show(cards, "card1"); } else if (e.getActionCommand().equals("显示第2个卡片")) { CardLayout cardLayout = (CardLayout) cards.getLayout(); cardLayout.show(cards, "card2"); } } public static void main(String[] args) { new CardLayoutDemo(); } } ``` 在这个示例中,我们创建了两个卡片,并使用字符串“card1”和“card2”标识它们。我们将这些卡片添加到一个带有CardLayout的JPanel中,并将该JPanel添加到窗口中。我们还创建了两个按钮,分别用于在卡片之间切换。当用户单击按钮时,我们使用CardLayout对象的show()方法切换到另一个卡片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值