This article shares the specific code of CardLayout for Java layout management for your reference. The specific content is as follows
import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextArea;public class TestCard implements ActionListener{ private JFrame fm=new JFrame("Card test"); private JPanel cardpan,containbtn; //cardpan is the card display area, containsbtn is the button display area private JButton btn1,btn2,btn3,btn4; //Define the poet's button private JPanel pan1,pan2,pan3,pan4; //The verse private CardLayout card is displayed in the panel defined here; //Define the card layout private JTextArea ta1,ta2,ta3,ta4; //Define the text field for lying in the verse public TestCard(){ //The following is the initialization component cardpan=new JPanel(); containsbtn=new JPanel(); card=new CardLayout(); cardpan.setLayout(card); btn1=new JButton("Li Bai"); btn2=new JButton("Du Fu"); btn3=new JButton("Bai Juyi"); btn4=new JButton("Meng Haoran"); ta1=new JTextArea(30,30); ta2=new JTextArea(30,30); ta3=new JTextArea(30,30); ta4=new JTextArea(30,30); //Put the defined button in the panel of the display button containsbtn.add(btn1); containsbtn.add(btn2); containsbtn.add(btn3); containsbtn.add(btn4); pan1=new JPanel(); //Add the listening event to the button btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); btn4.addActionListener(this); //Add the corresponding poem of the poet to the defined text field ta1.append("The sun shines the incense burner and produces purple smoke, /r/n"); ta1.append("Looking at the waterfall hanging in the front river from a distance. /r/n"); ta1.append("The flying flow goes down three thousand feet, /r/n"); ta1.append("It is suspected that the Milky Way fell in the sky."); ta2.append("Two orioles sing in the green willows, /r/n"); ta2.append("A row of egrets climbing into the blue sky. /r/n"); ta2.append("The window contains the snow of the West Ridge for thousands of miles, /r/n"); ta2.append("The door is moored by the ships of the East Wu thousands of miles."); ta3.append("A sunset spreads in the water, /r/n"); ta3.append("Half of the river whispers half of the river red. /r/n"); ta3.append("Poor night on the third day of the September lunar month, /r/n"); ta3.append("Dew is like pearls and moon is like bows."); ta4.append("Sleeping in spring without feeling the dawn, /r/n"); ta4.append("Speak birds everywhere. /r/n"); ta4.append("The sound of wind and rain comes at night, /r/n"); ta4.append("How many flowers fall."); pan1.add(ta1); pan2=new JPanel(); pan2.add(ta2); pan3=new JPanel(); pan3.add(ta3); pan4=new JPanel(); pan4.add(ta4); //Put the defined panel into the component of the card layout cardpan.add("b1",pan1); cardpan.add("b2",pan2); cardpan.add("b3",pan3); cardpan.add("b4",pan4); fm.add(containbtn,BorderLayout.NORTH);//Put the component of the display button to display fm.add(cardpan,BorderLayout.CENTER);//Put the component of the display text field to the middle to display fm.setSize(400,400); fm.setLocation(200,100); fm.setVisible(true); fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestCard(); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub /** * The result displayed by e.getActionCommand() is the content label on the corresponding component * card.show(cardpan, "b1"); represents the name of the corresponding component and display component * CardLayout.show(Container parent, String name): Definition of the implementation method of the display card */ if("Li Bai".equals(e.getActionCommand())){ card.show(cardpan, "b1"); } if("Du Fu".equals(e.getActionCommand())){ card.show(cardpan, "b2"); } if("Bai Juyi".equals(e.getActionCommand())){ card.show(cardpan, "b3"); } if("Meng Haoran".equals(e.getActionCommand())){ card.show(cardpan, "b4"); }}}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.