Discussion of Activity 6.3 - Simulating a control panel in a lift

Sample solution

Code for class LiftButtonFrame:

import java.awt.*;
import javax.swing.*;

public class LiftButtonFrame extends JFrame
{
   private JPanel top;
   private JPanel middle;
   private JPanel bottom;
   private JButton open;
   private JButton close;
   private JButton alarm;
   private JButton ground;
   private JButton first;
   private JButton second;
   private JButton third;
   private JTextField directionIndicator;
   private JTextField floorIndicator;

   public LiftButtonFrame(String title)
   {
      setTitle(title);
      setSize(200,240);
      setLocation(200,200);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Construct the panels.
      top = new JPanel();
      middle = new JPanel();
      bottom = new JPanel();
      middle.setLayout(new GridLayout(2,2));

      //  Construct the buttons and text fields.
      open = new JButton("< >");
      close = new JButton("> <");
      alarm = new JButton("alarm");
      ground = new JButton("G");
      first = new JButton("1");
      second = new JButton("2");
      third = new JButton("3");
      directionIndicator = new JTextField(4);
      floorIndicator = new JTextField(2);

      // Populate the panels.
      top.add(directionIndicator);
      top.add(floorIndicator);
      top.add(alarm);
      middle.add(third);
      middle.add(second);
      middle.add(ground);
      middle.add(first);
      bottom.add(open);
      bottom.add(close);

      // Get the content pane of the frame.
      Container cp = getContentPane();
      // Set its layout, then add the panels.
      cp.setLayout(new GridLayout(3,1));
      cp.add(top);
      cp.add(middle);
      cp.add(bottom);
   }
}

Code for class LiftButtonFrameTest:

public class LiftButtonFrameTest
{
   public LiftButtonFrameTest()
   {
   }

   public static void main(String[] args)
   {
      LiftButtonFrame lbf = new LiftButtonFrame("Lift controls");
      lbf.setVisible(true);
   }
}