// class ElevatorFrame.java //:Assignment 4: //:Student Name: Duo Zhou //:Studnet Number: 722452 /**FileName: ElevatorFrame.java * Date: July, 27, 2000 * Purpose: Learn how to use JFrame, JPanel, Synchronized method, creating * multithread, the basic idea of creating GUI * Name of Files: This assignment include Building.java, ConnectedWall.java, * Elevator.java, ElevatorFrame.java, ElevatorPanel.java, * Floor.java, MailDeliveryApp.java, MailPiece.java, * Position.java, Robot.java * Brief Discription: This file is added to create a new JFrame of the elevator. */ import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; class ElevatorFrame extends JFrame{ private int userTargetFloorno; //user target floor number; private int userFloorno; //the floor number that user currently stayed; private int robotFloorno; //the robot stayed floor number; private Elevator elevator; //hold a reference to the Elevator; private Building building; //hold a reference to the Building; private static ElevatorFrame elevatorFrameSingletone; //hold a reference of this JFrame; private static ElevatorPanel elevatorPanel; //hold a reference to the ElevatorPanel; private static Container content; //hold a reference to the Container; //********************************Constructor ************************************** ElevatorFrame(int preno, int tarno, Building building) { this.building=building; //get the reference of current building; this.elevator=building.getElevator(); //get a reference of current Elevator; userFloorno=preno; //user's stayed floor number; userTargetFloorno=tarno; //user's target floor number; setTitle("Elevator Frame, seen from side"); //set the title of this frame; content=getContentPane(); //create a ContentPanel elevatorPanel= new ElevatorPanel(userFloorno, userTargetFloorno, building); //create ElevatorPanel; elevatorPanel.setLayout(new BorderLayout()); //set BorderLayout; content.add(elevatorPanel, BorderLayout.CENTER);//place the panel to the center; setSize(150, 700); //set the JFrame size; //*********** Add WindowListener, to let it respond to WindowEvent *********** WindowListener wndCloser = new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); } }; addWindowListener(wndCloser); //add the WindowListener; pack(); //pack; setResizable(true); //Resizable; setVisible(true); //set Visible; }//end of Constructor; //**************Check to see if the frame has already displayed ********************* //If we have already make a reference of the current JFrame, then we don't need to //create another one, instead, we can just remove the old panel, then add a new //panel to the current JFrame. public static ElevatorFrame getElevator(int preno, int tarno, Building building) { if (elevatorFrameSingletone ==null) { //if no reference is made, then create another; elevatorFrameSingletone= new ElevatorFrame(preno, tarno, building); } else { //if we have already made a reference to the JFrame, then content.remove(elevatorPanel); //remove the old panel; elevatorPanel= new ElevatorPanel(preno, tarno, building); //add a new panel; elevatorPanel.setLayout(new BorderLayout()); //set BorderLayout; content.add(elevatorPanel, BorderLayout.CENTER); //add the new panel to the center; elevatorFrameSingletone.setSize(150, 700); //set the size to the original size; elevatorFrameSingletone.pack(); //pack; elevatorFrameSingletone.setVisible(true); //set Visible again; } return elevatorFrameSingletone; //send it back to MailDeliveryApplication; } }//end of ElevatorFrame