BorderLayout


FlowLayout


CardLayout

public class CardLayout  implements LayoutManager2, Serializable { }

一个CardLayout对象是CardLayout的布局管理器。 它将容器中的每个组件视为一张卡。 一次只能看到一张卡片,容器就是一堆卡片。类似于一副扑克牌。 添加到CardLayout对象的第一个组件是首次显示容器时的可见组件。 卡片的顺序由容器自己的组件对象的内部顺序决定。
CardLayout定义了一组允许应用程序顺序翻转这些卡片或显示指定卡片的方法。
addLayoutComponent(java.awt.Component, java.lang.Object)方法可用于将字符串标识符与给定卡相关联,以便快速随机访问。


构造方法

  • CardLayout() 创建一个新的卡片布局,其大小为零。
  • CardLayout(int hgap, int vgap) 创建具有指定的水平和垂直间隙的新卡布局。

常用方法

//卡片增删的方法

  1. 通过布局管理器增删卡片
    public void addLayoutComponent(Component comp, Object constraints) 将指定的组件添加到此卡布局的内部名称表中。 由指定的对象constraints必须是一个字符串。用字符串的名称作为此添加组件的指令 。 通过调用show方法,应用程序可以显示具有指定名称的组件。
    参数
    comp - 要添加的组件。
    constraints - 标识布局中特定卡片的标签。
  • public void removeLayoutComponent(Component comp) 从布局中删除指定的组件。 如果卡在顶部可见,则显示下面的下一张卡。
    参数
    comp - 要删除的组件。
  1. 常规方法通过add或remove增删卡片

//现在要显示卡片相关的方法
注意下面函数的用法是: 调用者是CardLayout的实例,而参数是使用CardLayout布局管理器的组件。所有在使用此种布局管理器时,好的习惯应该时先创建一个CardLayout的实例,在为需要的组件使用其实例。

  • public void first(Container parent) 翻转到容器的第一张卡片。
    参数
    parent - 进行布局的父容器
  • public void next(Container parent) 翻转到指定容器的下一张卡片。 如果当前可见的卡是最后一张卡,则此方法将在布局中翻转到第一张卡。
    参数
    parent - 要进行布局的父容器
  • public void previous(Container parent) 翻转到指定容器的前一张卡片。 如果当前可见的卡是第一个,则该方法将翻转到布局中的最后一张卡。
    参数
    parent - 要进行布局的父容器
  • public void last(Container parent) 翻转到容器的最后一张卡片。
    参数
    parent - 要进行布局的父容器
  • public void show(Container parent, String name) 翻转到已加入到该布局与指定的组件name ,使用addLayoutComponent 。 如果没有这样的组件存在,那么没有任何反应。
    参数
    parent - 要进行布局的父容器
    name - 组件名称

实例

package com.layoutPackage;

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

public class MyCardLayout {
    CardLayout myCard = new CardLayout(20, 20);
    JFrame jFrame = new JFrame();
    Container contentPane;
    
    public static void main(String[] args) {
        MyCardLayout myCardLayout = new MyCardLayout();
        myCardLayout.go();
    }
    public void go(){
        //准备组件
        JPanel jPanel, jPanel1, jPanel2, jPanel3;
        JButton jButton, jButton1, jButton2, jButton3;

        //设置内容窗格的布局管理器为CardLayout
        contentPane = jFrame.getContentPane();
        contentPane.setLayout(myCard);

        //创建面板主键
        jPanel = new JPanel(); //第一张
        jPanel.setBackground(Color.BLACK); //黑色
        jPanel1 = new JPanel(); //第二张
        jPanel1.setBackground(Color.RED); //红色
        jPanel2 = new JPanel(); //第三张
        jPanel2.setBackground(Color.CYAN);  //淡蓝色
        jPanel3 = new JPanel(); //第四张
        jPanel3.setBackground(Color.YELLOW); //黄色

        //创建按钮
        Font font = new Font("华文行楷", Font.BOLD + Font.ITALIC, 30); //用于设置按钮的字体
        jButton = new JButton("第一张");
        jButton.setFont(font);
        jButton.setForeground(Color.BLACK);
        jButton1 = new JButton("第二张");
        jButton1.setFont(font);
        jButton1.setForeground(Color.RED);
        jButton2 = new JButton("第三张");
        jButton2.setFont(font);
        jButton2.setForeground(Color.CYAN);
        jButton3 = new JButton("第四张");
        jButton3.setFont(font);
        jButton3.setForeground(Color.YELLOW);
        
        //将面板置于contentPane中
        contentPane.add(jPanel);
        contentPane.add(jPanel1);
        contentPane.add(jPanel2);
        contentPane.add(jPanel3);
        //将按钮置于面板中
        jPanel.add(jButton);
        jPanel1.add(jButton1);
        jPanel2.add(jButton2);
        jPanel3.add(jButton3);

        //为按钮注册事件


        //显示最后一张卡片
        myCard.last(contentPane);

        //定义一个事件的内部类。以便处理捕获的事件
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //当事件发生时,显示下一张卡片
                myCard.next(contentPane);
            }
        };
        
        //为button注册事件,注意此时只能在listener之后注册,因为listener是累不累,不用在之前使用。
        jButton.addActionListener(listener);
        jButton1.addActionListener(listener);
        jButton2.addActionListener(listener);
        jButton3.addActionListener(listener);

        jFrame.setDefaultCloseOperation(3);
        jFrame.setBounds(500, 50, 700, 550);
        jFrame.setVisible(true);
    }
}

运行结果截图如下:

卡片配置审批流 Java java卡片布局如何加组件_字符串