Java CardLayout layout example, javacardlayout
I. Design Process
Create a Main Window jf
Create two main editions: mainJP1 and mainJP2
Create subJP1, subJP2, and subJP3
Create three tags, assign a value of 1st pages, 2nd pages, and 3rd pages
Create two buttons, btn1 and btn2, and assign values to the previous and next pages.
Set the layout of the main window to GridLayout (2, 1)
Set the mainJP1 layout of the main version to Cardlayout.
Set the mainJP2 layout of the main version to FlowLayout.
Set different background colors for the three child editions.
Add the three tags to the three child editions respectively.
Add the three subfaces to mainJP1
Add two buttons to mainJP2
Add two main editions to the Main Window
Process two button events
Set the main window to visible
Set the closing mode of the Main Window
Ii. Source Code
Package javaapplication1; import java. awt. cardLayout; import java. awt. color; import java. awt. flowLayout; import java. awt. gridLayout; 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;/***** @ author thinker */public class buJu {/***** @ param args */public static void main (String [] args) {JFrame jf = new JFrame (); JPanel mainjp1 = new JPanel (); JPanel mainjp2 = new JPanel (); JPanel subjp1 = new JPanel (); JPanel subjp2 = new JPanel (); JPanel subjp3 = new JPanel (); JLabel lb1 = new JLabel ("first page "); JLabel lb2 = new JLabel ("second page"); JLabel lb3 = new JLabel ("Third page"); JButton btn1 = new JButton ("Previous Page "); JButton btn2 = new JButton ("next page"); jf. setLayout (new GridLayout (2, 1); mainjp1.setLayout (new CardLayout (); mainjp2.setLayout (new FlowLayout (); subjp1.setBackground (Color. orange); subjp2.setBackground (Color. pink); subjp3.setBackground (Color. lightGray); subjp1.add (lb1, "1"); subjp2.add (lb2, "2"); subjp3.add (lb3, "3"); mainjp1.add (subjp1); mainjp1.add (subjp2 ); mainjp1.add (subjp3); mainjp2.add (btn1); mainjp2.add (btn2); jf. add (mainjp1); jf. add (mainjp2); btn1.addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {(CardLayout) mainjp1.getLayout ()). previous (mainjp1) ;}}); btn2.addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {(CardLayout) mainjp1.getLayout ()). next (mainjp1) ;}}); jf. setVisible (true); jf. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );}}
III,
Iv. References
1. How to Use CardLayout in java
Http://www.cnblogs.com/UUUP/p/3800843.html
2. Fan mingxiang and Chen jinhui-Chapter 3 of java swing Programming