CardLayout in java AWT with example tutorial: There are various layouts in Java AWT like border layout, grid layout and flow layout and more. This is aslo one of the java layout class. But this simple AWT program in java is used to speak about card layout class With the use of text fields, labels and buttons. This AWT tutorial is easy to understand. A java awt example tutorial is given below.
This java program is used to display the component as a card and changing the component by pressing the buttons and make changes in the views.
[Useful: C Programming Guide] & [IT Companies List]
CardLayout program in Java AWT:
Q: Write a Java code to display card layouts using AWT class.
//Import statements import java.awt.*; import java.awt.event.*; //Class definition class Panel1 extends Panel { //Variable declaration Label la1,la2,la3; TextField te1,te2,te3; Button bu1, bu2, bu3, bu4, bu5; public Panel1() { la1=new Label("Number 1:"); la2=new Label("Number 2:"); la3=new Label("Result :"); te1=new TextField(10); te2=new TextField(10); te3=new TextField(10); te3.setEditable(false); bu1=new Button("Add"); bu2=new Button("Sub"); bu3=new Button("Mul"); bu4=new Button("Div"); bu5=new Button("Exit"); add(la1); add(te1); add(la2); add(te2); add(la3); add(te3); add(bu1); add(bu2); add(bu3); add(bu4); add(bu5); } // © http://students3k.com – Karthikh Venkat } class Panel2 extends Panel { Checkbox ch1, ch2, ch3, ch4; public Panel2() { ch1=new Checkbox("Windows 8"); ch2=new Checkbox("Linux Ubuntu"); ch3=new Checkbox("Android"); ch4=new Checkbox("macOS"); setLayout(new GridLayout(2,2)); add(ch1); add(ch2); add(ch3); add(ch4); } } class Panel3 extends Panel { TextArea tax; TextField te1, te2; Label la1, la2; Button bu1, bu2, bu3, bu4; public Panel3() { tax=new TextArea(10,50); te1=new TextField(10); te2=new TextField(10); la1=new Label("Find What:"); la2=new Label("Replace With:"); bu1=new Button("Find"); bu2=new Button("Replace"); bu3=new Button("Replace All"); bu4=new Button("Cancel"); add(tax); add(la1); add(te1); add(la2); add(te2); add(bu1); add(bu2); add(bu3); add(bu4); } // © http://students3k.com – Karthikh Venkat } class CardLayoutDemo extends Frame implements ActionListener { Button bu1, bu2; Panel cards; CardLayout c; Panel1 pa1; Panel2 pa2; Panel3 pa3; public CardLayoutDemo(String title) { super(title); bu1=new Button("Next");//Object creation bu2=new Button("Previous"); cards=new Panel(); pa1=new Panel1(); pa2=new Panel2(); pa3=new Panel3(); c=new CardLayout(); cards.setLayout(c); cards.add(pa1,"Panel1"); cards.add(pa2,"Panel2"); cards.add(pa3,"Panel3"); // addWindowListener(new MyWindowListener()); bu1.addActionListener(this); bu2.addActionListener(this); Panel p=new Panel(); p.add(b1); p.add(b2); add("North",p); add("Center",cards); setSize(300,300); setVisible(true); } // © http://students3k.com – Karthikh Venkat public void actionPerformed(ActionEvent e) { if(e.getSource()==b1)//Decision making statement c.next(cards); else c.previous(cards); } public static void main(String args[]) { new CardLayoutDemo("Card Layout Demo"); } }