📜  Java AWT |卡片布局类

📅  最后修改于: 2022-05-13 01:55:35.462000             🧑  作者: Mango

Java AWT |卡片布局类

CardLayout 类以这样一种方式管理组件,即一次只能看到一个组件。它将每个组件视为容器中的一张卡片。一次只能看到一张卡片,并且容器充当一叠卡片。添加到CardLayout对象的第一个组件是容器首次显示时的可见组件。

构造函数:

  1. CardLayout():它用于创建一个新的卡片布局,其间隙大小为零。
  2. CardLayout(int Horizontalgap, int verticalgap):用于创建具有指定水平和垂直间隙的新 CardLayout 类。

常用方法:

  • getLayoutAlignmentX(Container parent):返回沿 x 轴的对齐方式。
  • getLayoutAlignmentY(Container parent):返回沿 y 轴的对齐方式。
  • getVgap():用于获取组件之间的垂直间隙。
  • addLayoutComponent(Component cm, Object cn):用于将指定的组件添加到此卡片布局的内部名称表中。
  • getHgap():用于获取组件之间的水平间隙。
  • toString():返回此卡片布局状态的字符串表示形式。
  • removeLayoutComponent(Component cm):用于从布局中移除指定的组件。

下面的程序说明了 CardLayout 类:

  • 程序1:在下面的程序中,我们在一个JFrame中排列了几个JLabel组件,其实例类为“ Cardlayout ”。我们创建了 3 个名为“ bt1 ”、“ bt2 ”、“ bt3 ”的JButton组件,然后使用add()方法将它们添加到JFrame 。我们通过setSize()setVisible()方法设置框架的大小和可见性。布局由方法setLayout()方法设置。
Java
// Java program to illustrate the CardLayout Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.*;
 
// class extends JFrame and implements actionlistener
public class Cardlayout extends JFrame implements ActionListener {
 
    // Declaration of objects of CardLayout class.
    CardLayout card;
 
    // Declaration of objects of JButton class.
    JButton b1, b2, b3;
 
    // Declaration of objects
    // of Container class.
    Container c;
 
    Cardlayout()
    {
 
        // to get the content
        c = getContentPane();
 
        // Initialization of object "card"
        // of CardLayout class with 40
        // horizontal space and 30 vertical space .
        card = new CardLayout(40, 30);
 
        // set the layout
        c.setLayout(card);
 
        // Initialization of object "b1" of JButton class.
        b1 = new JButton("GEEKS");
 
        // Initialization of object "b2" of JButton class.
        b2 = new JButton("FOR");
 
        // Initialization of object "b3" of JButton class.
        b3 = new JButton("GEEKS");
 
        // this Keyword refers to current object.
        // Adding Jbutton "b1" on JFrame using ActionListener.
        b1.addActionListener(this);
 
        // Adding Jbutton "b2" on JFrame using ActionListener.
        b2.addActionListener(this);
 
        // Adding Jbutton "b3" on JFrame using ActionListener.
        b3.addActionListener(this);
 
        // Adding the JButton "b1"
        c.add("a", b1);
 
        // Adding the JButton "b2"
        c.add("b", b2);
 
        // Adding the JButton "b1"
        c.add("c", b3);
    }
     
    public void actionPerformed(ActionEvent e)
    {
         
        // call the next card
        card.next(c);
    }
 
    // Main Method
    public static void main(String[] args)
    {
         
        // Creating Object of CardLayout class.
        Cardlayout cl = new Cardlayout();
 
        // Function to set size of JFrame.
        cl.setSize(400, 400);
 
        // Function to set visibility of JFrame.
        cl.setVisible(true);
 
        // Function to set default operation of JFrame.
        cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}


Java
// Java program to show Example of CardLayout.
// in java. Importing different Package.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
 
// class extends JFrame
public class CardLayoutDemo extends JFrame {
 
    // Initialization the value of
    // current card is 1 .
    private int currentCard = 1;
 
    // Declaration of objects
    // of CardLayout class.
    private CardLayout cl;
 
    public CardLayoutDemo()
    {
 
        // Function to set visibility of JFrame
        setTitle("Card Layout Example");
 
        // Function to set visibility of JFrame
        setSize(300, 150);
 
        // Creating Object of "Jpanel" class
        JPanel cardPanel = new JPanel();
 
        // Initialization of object "c1"
        // of CardLayout class.
        cl = new CardLayout();
 
        // set the layout
        cardPanel.setLayout(cl);
 
        // Initialization of object
        // "jp1" of JPanel class.
        JPanel jp1 = new JPanel();
 
        // Initialization of object
        // "jp2" of CardLayout class.
        JPanel jp2 = new JPanel();
 
        // Initialization of object
        // "jp3" of CardLayout class.
        JPanel jp3 = new JPanel();
 
        // Initialization of object
        // "jp4" of CardLayout class.
        JPanel jp4 = new JPanel();
 
        // Initialization of object
        // "jl1" of JLabel class.
        JLabel jl1 = new JLabel("Card1");
 
        // Initialization of object
        // "jl2" of JLabel class.
        JLabel jl2 = new JLabel("Card2");
 
        // Initialization of object
        // "jl3" of JLabel class.
        JLabel jl3 = new JLabel("Card3");
 
        // Initialization of object
        // "jl4" of JLabel class.
        JLabel jl4 = new JLabel("Card4");
 
        // Adding JPanel "jp1" on JFrame.
        jp1.add(jl1);
 
        // Adding JPanel "jp2" on JFrame.
        jp2.add(jl2);
 
        // Adding JPanel "jp3" on JFrame.
        jp3.add(jl3);
 
        // Adding JPanel "jp4" on JFrame.
        jp4.add(jl4);
 
        // Adding the cardPanel on "jp1"
        cardPanel.add(jp1, "1");
 
        // Adding the cardPanel on "jp2"
        cardPanel.add(jp2, "2");
 
        // Adding the cardPanel on "jp3"
        cardPanel.add(jp3, "3");
 
        // Adding the cardPanel on "jp4"
        cardPanel.add(jp4, "4");
 
        // Creating Object of "JPanel" class
        JPanel buttonPanel = new JPanel();
 
        // Initialization of object
        // "firstbtn" of JButton class.
        JButton firstBtn = new JButton("First");
 
        // Initialization of object
        // "nextbtn" of JButton class.
        JButton nextBtn = new JButton("Next");
 
        // Initialization of object
        // "previousbtn" of JButton class.
        JButton previousBtn = new JButton("Previous");
 
        // Initialization of object
        // "lastbtn" of JButton class.
        JButton lastBtn = new JButton("Last");
 
        // Adding JButton "firstbtn" on JFrame.
        buttonPanel.add(firstBtn);
 
        // Adding JButton "nextbtn" on JFrame.
        buttonPanel.add(nextBtn);
 
        // Adding JButton "previousbtn" on JFrame.
        buttonPanel.add(previousBtn);
 
        // Adding JButton "lastbtn" on JFrame.
        buttonPanel.add(lastBtn);
 
        // add firstbtn in ActionListener
        firstBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                 
                // used first c1 CardLayout
                cl.first(cardPanel);
 
                // value of currentcard is 1
                currentCard = 1;
            }
        });
 
        // add lastbtn in ActionListener
        lastBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
 
                // used last c1 CardLayout
                cl.last(cardPanel);
 
                // value of currentcard is 4
                currentCard = 4;
            }
        });
 
        // add nextbtn in ActionListener
        nextBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
 
                // if condition apply
                if (currentCard < 4)
                {
                     
                    // increment the value of currentcard by 1
                    currentCard += 1;
 
                    // show the value of currentcard
                    cl.show(cardPanel, "" + (currentCard));
                }
            }
        });
 
        // add previousbtn in ActionListener
        previousBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                // if condition apply
                if (currentCard > 1) {
 
                    // decrement the value
                    // of currentcard by 1
                    currentCard -= 1;
 
                    // show the value of currentcard
                    cl.show(cardPanel, "" + (currentCard));
                }
            }
        });
 
        // used to get content pane
        getContentPane().add(cardPanel, BorderLayout.NORTH);
 
        // used to get content pane
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }
 
    // Main Method
    public static void main(String[] args)
    {
 
        // Creating Object of CardLayoutDemo class.
        CardLayoutDemo cl = new CardLayoutDemo();
 
        // Function to set default operation of JFrame.
        cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // Function to set visibility of JFrame.
        cl.setVisible(true);
    }
}


输出:

  • 程序 2:在下面的程序中,我们将 4 个JLabel组件排列在一个JFrame中,其类为“ CardlayoutDemo ”。我们创建了 4 个名为“ firstbtn ”、“ nextbtn ”、“ previousbtn ”、“ lastbtn ”的JButton组件和 4 个名为“ jl1 ”、“ jl2 ”、“ jl3 ”“ jl4 ”的JLabel组件。这里我们还创建了 4 个JPanel组件,分别命名为“ jp1 ”、“ jp2 ”、“ jp3 ”、“ jp4 ”,然后使用add()方法将它们添加到JFrame 。我们将分别使用setSize()setVisible()setTitle()方法设置框架的大小、可见性和标题。使用setLayout()方法设置布局。

Java

// Java program to show Example of CardLayout.
// in java. Importing different Package.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
 
// class extends JFrame
public class CardLayoutDemo extends JFrame {
 
    // Initialization the value of
    // current card is 1 .
    private int currentCard = 1;
 
    // Declaration of objects
    // of CardLayout class.
    private CardLayout cl;
 
    public CardLayoutDemo()
    {
 
        // Function to set visibility of JFrame
        setTitle("Card Layout Example");
 
        // Function to set visibility of JFrame
        setSize(300, 150);
 
        // Creating Object of "Jpanel" class
        JPanel cardPanel = new JPanel();
 
        // Initialization of object "c1"
        // of CardLayout class.
        cl = new CardLayout();
 
        // set the layout
        cardPanel.setLayout(cl);
 
        // Initialization of object
        // "jp1" of JPanel class.
        JPanel jp1 = new JPanel();
 
        // Initialization of object
        // "jp2" of CardLayout class.
        JPanel jp2 = new JPanel();
 
        // Initialization of object
        // "jp3" of CardLayout class.
        JPanel jp3 = new JPanel();
 
        // Initialization of object
        // "jp4" of CardLayout class.
        JPanel jp4 = new JPanel();
 
        // Initialization of object
        // "jl1" of JLabel class.
        JLabel jl1 = new JLabel("Card1");
 
        // Initialization of object
        // "jl2" of JLabel class.
        JLabel jl2 = new JLabel("Card2");
 
        // Initialization of object
        // "jl3" of JLabel class.
        JLabel jl3 = new JLabel("Card3");
 
        // Initialization of object
        // "jl4" of JLabel class.
        JLabel jl4 = new JLabel("Card4");
 
        // Adding JPanel "jp1" on JFrame.
        jp1.add(jl1);
 
        // Adding JPanel "jp2" on JFrame.
        jp2.add(jl2);
 
        // Adding JPanel "jp3" on JFrame.
        jp3.add(jl3);
 
        // Adding JPanel "jp4" on JFrame.
        jp4.add(jl4);
 
        // Adding the cardPanel on "jp1"
        cardPanel.add(jp1, "1");
 
        // Adding the cardPanel on "jp2"
        cardPanel.add(jp2, "2");
 
        // Adding the cardPanel on "jp3"
        cardPanel.add(jp3, "3");
 
        // Adding the cardPanel on "jp4"
        cardPanel.add(jp4, "4");
 
        // Creating Object of "JPanel" class
        JPanel buttonPanel = new JPanel();
 
        // Initialization of object
        // "firstbtn" of JButton class.
        JButton firstBtn = new JButton("First");
 
        // Initialization of object
        // "nextbtn" of JButton class.
        JButton nextBtn = new JButton("Next");
 
        // Initialization of object
        // "previousbtn" of JButton class.
        JButton previousBtn = new JButton("Previous");
 
        // Initialization of object
        // "lastbtn" of JButton class.
        JButton lastBtn = new JButton("Last");
 
        // Adding JButton "firstbtn" on JFrame.
        buttonPanel.add(firstBtn);
 
        // Adding JButton "nextbtn" on JFrame.
        buttonPanel.add(nextBtn);
 
        // Adding JButton "previousbtn" on JFrame.
        buttonPanel.add(previousBtn);
 
        // Adding JButton "lastbtn" on JFrame.
        buttonPanel.add(lastBtn);
 
        // add firstbtn in ActionListener
        firstBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                 
                // used first c1 CardLayout
                cl.first(cardPanel);
 
                // value of currentcard is 1
                currentCard = 1;
            }
        });
 
        // add lastbtn in ActionListener
        lastBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
 
                // used last c1 CardLayout
                cl.last(cardPanel);
 
                // value of currentcard is 4
                currentCard = 4;
            }
        });
 
        // add nextbtn in ActionListener
        nextBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
 
                // if condition apply
                if (currentCard < 4)
                {
                     
                    // increment the value of currentcard by 1
                    currentCard += 1;
 
                    // show the value of currentcard
                    cl.show(cardPanel, "" + (currentCard));
                }
            }
        });
 
        // add previousbtn in ActionListener
        previousBtn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                // if condition apply
                if (currentCard > 1) {
 
                    // decrement the value
                    // of currentcard by 1
                    currentCard -= 1;
 
                    // show the value of currentcard
                    cl.show(cardPanel, "" + (currentCard));
                }
            }
        });
 
        // used to get content pane
        getContentPane().add(cardPanel, BorderLayout.NORTH);
 
        // used to get content pane
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }
 
    // Main Method
    public static void main(String[] args)
    {
 
        // Creating Object of CardLayoutDemo class.
        CardLayoutDemo cl = new CardLayoutDemo();
 
        // Function to set default operation of JFrame.
        cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        // Function to set visibility of JFrame.
        cl.setVisible(true);
    }
}

输出:

注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。

参考: https: Java/awt/CardLayout.html