1 / 3100%
public class CreatePanel extends JPanel
{
private ArrayList flightList;
private JButton button1;
private SelectPanel selectPanel;
private JLabel nameL, flightNumL, depCityL, depDayL, depTimeL, arrivalCityL,
arrivalDayL, arrivalTimeL, airfareL;
private JTextField nameTF, flightNumTF, depCityTF, depDayTF, depTimeTF,
arrivalCityTF, arrivalDayTF, arrivalTimeTF, airfareTF;
private JLabel message;
private JTextArea displayFlightTA;
private JPanel flightCreatPanel, panelLeft, leftANDright;
private JScrollPane displayFlightSP;
//Constructor initializes components and organize them using certain layouts
public CreatePanel(ArrayList flightList, SelectPanel selectPanel)
{
this.flightList = flightList;
this.selectPanel = selectPanel;
//name of the JLabe to tell user what to input
nameL = new JLabel("Enter a name of Airline");
flightNumL = new JLabel("Enter a flight number");
depCityL = new JLabel("Enter a depature city");
depDayL = new JLabel("Enter a depature day");
depTimeL = new JLabel("Enter a depature time");
arrivalCityL = new JLabel("Enter an arrival city");
arrivalDayL =new JLabel("Enter an arrival day");
arrivalTimeL = new JLabel("Enter an arrival time");
airfareL = new JLabel("Airfare");
message = new JLabel("");
message.setForeground(Color.red);
displayFlightTA = new JTextArea("No Flight");
displayFlightTA.setEditable(false); //Set the textarea not editable
//Set textfield to the user's input
nameTF = new JTextField();
flightNumTF = new JTextField();
depCityTF = new JTextField();
depDayTF = new JTextField();
depTimeTF = new JTextField();
arrivalCityTF = new JTextField();
arrivalDayTF = new JTextField();
arrivalTimeTF = new JTextField();
airfareTF = new JTextField();
// Assignment #: 6
// Name: Zihao Feng
// StudentID: 1209476473
// Lecture: M/W/F 10:45 am
// Description: This class setup the layout
// and ButtonListener for the CreatePanel
// creat new flight using input informations
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
//organize components here
JPanel flightCreatPanel = new JPanel(); //The panel contain user's input and
JLabel
flightCreatPanel.setLayout(new GridLayout(9,2));
flightCreatPanel.add(nameL);
flightCreatPanel.add(nameTF);
flightCreatPanel.add(flightNumL);
flightCreatPanel.add(flightNumTF);
flightCreatPanel.add(depCityL);
flightCreatPanel.add(depCityTF);
flightCreatPanel.add(depDayL);
flightCreatPanel.add(depDayTF);
flightCreatPanel.add(depTimeL);
flightCreatPanel.add(depTimeTF);
flightCreatPanel.add(arrivalCityL);
flightCreatPanel.add(arrivalCityTF);
flightCreatPanel.add(arrivalDayL);
flightCreatPanel.add(arrivalDayTF);
flightCreatPanel.add(arrivalTimeL);
flightCreatPanel.add(arrivalTimeTF);
flightCreatPanel.add(airfareL);
flightCreatPanel.add(airfareTF);
button1 = new JButton("Create a flight");
button1.addActionListener(new ButtonListener());
add(button1);
JPanel panelLeft = new JPanel(); //Panel contain message flightCreatPanel
and button1
panelLeft.setLayout(new BorderLayout());
panelLeft.add(message,BorderLayout.NORTH);
panelLeft.add(flightCreatPanel,BorderLayout.CENTER);
panelLeft.add(button1,BorderLayout.SOUTH);
JScrollPane displayFlightSP = new JScrollPane(displayFlightTA); //ScrollPane
contain Flight information
this.setLayout(new GridLayout(1,2));
this.add(panelLeft);
this.add(displayFlightSP);
}
//ButtonListener is a listener class that listens to
//see if the button "Create a flight" is pushed.
//When the event occurs, it adds a flight using the information
//entered by a user.
//It also performs error checking.
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//TO BE COMPLETED
try
{
//Crate a object Flight using user's input
Flight newFlight = new Flight();
newFlight.setAirlines(nameTF.getText());
newFlight.setFlightNum(Integer.parseInt(flightNumTF.getText()));
newFlight.setDeparture(depCityTF.getText(),
depDayTF.getText(), depTimeTF.getText());
newFlight.setArrival(arrivalCityTF.getText(),
arrivalDayTF.getText(), arrivalTimeTF.getText());
newFlight.setAirfare(Double.parseDouble(airfareTF.getText()));
flightList.add(newFlight);
//check and printout the error if missing information
if(nameTF.getText().equals("") ||
flightNumTF.getText().equals("") || depCityTF.getText().equals("") ||
depDayTF.getText().equals("")
|| depTimeTF.getText().equals("") ||
arrivalCityTF.getText().equals("") || arrivalDayTF.getText().equals("") ||
arrivalTimeTF.getText().equals("") ||
airfareTF.getText().equals(""))
{
message.setText("Please enter all fields");
}
else
{
if(displayFlightTA.getText().equals("No Flight"))
{
displayFlightTA.setText("");
}
displayFlightTA.append(newFlight.toString()); //display
the added flight
message.setText("Flight added.");
selectPanel.addCheckBox(newFlight);
//initialize the textfield for next flight
nameTF.setText("");
nameTF.setText("");
flightNumTF.setText("");
depCityTF.setText("");
depDayTF.setText("");
depTimeTF.setText("");
arrivalCityTF.setText("");
arrivalDayTF.setText("");
arrivalTimeTF.setText("");
airfareTF.setText("");
}
}
catch(NumberFormatException e)
{
//if the user input wrong information display the error
message
message.setText("Please enter a number for its flight number
and its airfare");
}
} //end of actionPerformed method
} //end of ButtonListener class
}
Powered by TCPDF (www.tcpdf.org)
Students also viewed