Tuesday, April 1, 2014

Border LAyout and Card Layout - Panel

import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Date;
import java.awt.*;
import java.awt.event.*;


             /*<applet code="L5" width=400 height=400>
             </applet>*/


public class L5 extends Applet implements ActionListener {

   Button N = new Button("NORTH = Previous");
   Button S = new Button("SOUTH = Next");
   Button E = new Button("EAST = Last");
   Button W = new Button("WEST = First");

   Button P1 = new Button("FIRST -> Second");
   Button P2 = new Button("SECOND -> First");
   Button P3 = new Button("THIRD -> First");

   Panel p;
   CardLayout CLayout;

   public void init() {
      setup_layout();
   }

   private void setup_layout() {
      p = new Panel();

      setLayout(new BorderLayout());
      setBackground(Color.red);
      setForeground(Color.black);

      p.setBackground(Color.lightGray);
      p.setLayout(CLayout = new CardLayout());

      add("North", N);
      add("South", S);
      add("Center", p);
      add("West", W);
      add("East", E);

      p.add("Name 1",P1);
      p.add("Name 2",P2);
      p.add("Name 3",P3);

      N.addActionListener(this);
      S.addActionListener(this);
      E.addActionListener(this);
      W.addActionListener(this);
      P1.addActionListener(this);
      P2.addActionListener(this);
      P3.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e) {
      if (e.getSource() == N) {
         CLayout.previous(p);
         showStatus("North button pushed");
      }
      else if (e.getSource() == S) {
         CLayout.next(p);
         showStatus("South button pushed");
      }
      else if (e.getSource() == E) {
         CLayout.last(p);
         showStatus("East button pushed");
      }
      else if (e.getSource() == W) {
         CLayout.first(p);
         showStatus("West button pushed");
      }
      else if (e.getSource() == P1) {
         CLayout.show(p,"Name 2");
         showStatus("Card P1 button pushed");
      }
      else if (e.getSource() == P2) {
         CLayout.show(p,"Name 3");
         showStatus("Card P2 button pushed");
      }
      else if (e.getSource() == P3) {
         CLayout.show(p,"Name 1");
         showStatus("Card P3 button pushed");
      }
   
   }
}

No comments: