EBU4201 Mini Project 完整版

在这里插入图片描述

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;

/**
 * Description : this is the main method and can store the information that
 * entered from the MyFrame
 *
 * @author Yuxiong Wang
 * @version 1.0
 * 
 */
public class CheckIn implements ActionListener
{
	/** The main frame that let user enter information */
	MyFrame myFrame;
	/** String array to store the information */
	String[] info;

	/**
	 * Program entrance. Create variable myProg and use method runProg() to run the
	 * Program
	 * 
	 * @param args no use in this program
	 */
	public static void main(String[] args)
	{
		CheckIn myProg = new CheckIn();
		myProg.runProg();
	}

	/**
	 * Create user interface and add action listener to 'Next step'.<br>
	 * <p>
	 * MyFrame for the first stage user interface<br>
	 * Use the String array 'info' to store user information.<br>
	 * <p>
	 * Add action listener to 'Next step'.
	 */
	public void runProg()
	{
		myFrame = new MyFrame();
		info = new String[7];

		myFrame.button1.addActionListener(this);
	}

	/**
	 * Handle the click event.<br>
	 * <p>
	 * When the user clicks on the "Next step" button, this program will check the
	 * information wether all entered.<br>
	 * If not all information are completed, this program will not proceed to the
	 * check in completion stage and an error dialog box will display prompting the
	 * user to complete the missing information.<br>
	 * At the same time, the incomplete session is highlighted in red so that user
	 * can easily check the missing information to be completed.<br>
	 * If all the information are completed by the user, this program will pop-up a
	 * dialog box to confirm with the user for the final confirmation before the
	 * check in process is completed. And, if user clicks "Yes", this program will
	 * proceed to the check in completion stage which will close the "Workshop Check
	 * In" window and open the "Check in complete!" window, if user clicks "No",
	 * this program will go back and the information entered will still here for
	 * user to perfect.<br>
	 * 
	 * @param e the event happened
	 */
	public void actionPerformed(ActionEvent e)
	{

		int guarantee = 1;// the guarantee = 1 means that all the information have been filled
		/*
		 * if the user didn't fill/select in the First name, then highlight the
		 * textfield and set guarantee =0 if the user have filled/selected in the first
		 * name, store it in the array info and cancel the red border(if it exists)
		 */
		if (myFrame.textField1.getText().equals(""))
		{
			myFrame.textField1.setBorder(
					BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2), "First Name"));
			guarantee = 0;
		} else
		{
			info[0] = myFrame.textField1.getText() + " ";
			myFrame.textField1.setBorder(
					BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "First Name"));
		}

		if (myFrame.textField2.getText().equals(""))
		{
			myFrame.textField2.setBorder(
					BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2), "Surname"));
			guarantee = 0;
		}

		else
		{
			info[0] = info[0] + myFrame.textField2.getText();
			myFrame.textField2
					.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Surname"));
		}

		if (myFrame.textField3.getText().equals(""))
		{
			myFrame.textField3
					.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2), "Email"));
			guarantee = 0;
		}

		else
		{
			info[3] = myFrame.textField3.getText();
			myFrame.textField3.setBorder(null);
		}

		if (myFrame.textField4.getText().equals(""))
		{
			myFrame.textField4.setBorder(
					BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2), "Phone number"));
			guarantee = 0;
		}

		else
		{
			info[4] = myFrame.textField4.getText();
			myFrame.textField4.setBorder(null);
		}

		if (myFrame.textField5.getText().equals(""))
		{
			myFrame.textField5.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2),
					"Booking reference code"));
			guarantee = 0;
		}

		/*
		 * if the user have filled in the Booking reference code, store it in the array
		 * info and cancel the red border(if it exists)
		 */
		else
		{
			myFrame.textField5.setBorder(null);
			info[6] = myFrame.textField4.getText();
		}

		if (myFrame.comboBox1.getSelectedItem().toString() == "")
		{
			myFrame.comboBox1
					.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2), "Month"));
			guarantee = 0;
		}

		else
		{
			info[1] = myFrame.comboBox1.getSelectedItem().toString() + " ";
			myFrame.comboBox1.setBorder(null);
		}
		/*
		 * if the user didn't select Year, then highlight the textfield and set
		 * guarantee =0
		 */
		if (myFrame.comboBox2.getSelectedItem().toString() == "")
		{
			myFrame.comboBox2
					.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2), "Year"));

			guarantee = 0;
		}

		else
		{
			info[1] = info[1] + myFrame.comboBox2.getSelectedItem().toString();

			myFrame.comboBox2.setBorder(null);
		}

		if (myFrame.comboBox3.getSelectedItem().toString() == "")
		{
			myFrame.comboBox3.setBorder(
					BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2), "Programme"));

			guarantee = 0;
		}

		else
		{
			info[2] = myFrame.comboBox3.getSelectedItem().toString() + ", Year  ";

			myFrame.comboBox3.setBorder(null);

		}

		if (myFrame.comboBox4.getSelectedItem().toString() == "")
		{
			myFrame.comboBox4
					.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2), "Year"));

			guarantee = 0;
		}

		else
		{

			info[2] = info[2] + myFrame.comboBox4.getSelectedItem().toString();

			myFrame.comboBox4.setBorder(null);

		}
		if (!myFrame.checkBox1.isSelected())
		{
			myFrame.checkBox1.setBorderPainted(true);
			myFrame.checkBox1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2)));
			guarantee = 0;
		} else
		{
			myFrame.checkBox1.setBorder(null);
		}
		if (!myFrame.checkBox2.isSelected())
		{
			myFrame.checkBox2.setBorderPainted(true);
			myFrame.checkBox2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.RED, 2)));
			guarantee = 0;
		} else
		{
			myFrame.checkBox2.setBorder(null);
		}

		if (guarantee == 0)
		{
			JOptionPane.showMessageDialog(myFrame, "Please fill in the missing information", "Missing information",
					JOptionPane.ERROR_MESSAGE);
		}

		else if (guarantee == 1)// if all the information have been filled
		{
			// Pop the confirmation window lets the user confirm whether to proceed

			int n = JOptionPane.showConfirmDialog(myFrame,
					"Hava you checked all the information is correct? You can't change the information once the information is submitted.?",
					"Confirm check in?", JOptionPane.YES_NO_OPTION);
			// if the user clicked the yes then new the anotherFrame
			if (n == JOptionPane.YES_OPTION)
			{
				AnotherFrame anotherFrame = new AnotherFrame(info);
			}
		}
	}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

/**
 * Description : This class defines the main frame
 * 
 * @author Yuxiong Wang
 * @version 1.0
 *
 */

public class MyFrame extends JFrame implements ActionListener
{
	/**
	 * The panel for the rows.
	 */
	JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8, panel9;
	/**
	 * TextField for user to input
	 */
	JTextField textField1, textField2, textField3, textField4, textField5;
	/** First grid in the bus-panel */
	JLabel label1;
	/** Drop-down list for user to select */
	JComboBox comboBox1, comboBox2, comboBox3, comboBox4;
	/** Check box for user to select */
	JCheckBox checkBox1, checkBox2;
	/** Buttons for user to click */
	JButton button1, button2, button3;

	/**
	 * Class constructor.<br>
	 * 
	 * Create every thing in the "Workshop Check In" window
	 */
	public MyFrame()
	{
		// create the panels
		panel1 = new JPanel(new BorderLayout());
		panel2 = new JPanel();
		panel3 = new JPanel();
		panel4 = new JPanel();
		panel5 = new JPanel();
		panel6 = new JPanel();
		panel7 = new JPanel();
		panel8 = new JPanel();
		panel9 = new JPanel();

		// create the label and set the font
		label1 = new JLabel("Please complete the information below:", JLabel.CENTER);
		Font font = new Font(null, Font.PLAIN, 20);
		label1.setFont(font);

		// create the textfield and set the border
		textField1 = new JTextField(10);
		textField2 = new JTextField(10);
		textField3 = new JTextField(20);
		textField3.setBorder(BorderFactory.createBevelBorder(EtchedBorder.LOWERED));
		textField4 = new JTextField(20);
		textField4.setBorder(BorderFactory.createBevelBorder(EtchedBorder.LOWERED));
		textField5 = new JTextField(6);
		textField5.setBorder(BorderFactory.createBevelBorder(EtchedBorder.LOWERED));

		// add the information in the list to a String respectively
		String s1[] =
		{ "", "January", "Febuary", "March", "April", "May", "June", "Junary", "Auguet", "Septemper", "October",
				"November", "December" };
		String s2[] =
		{ "", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005" };
		String s3[] =
		{ "", "Telecommunications Engineering with Management", "Internet of Things Engineering",
				"E-commerce and Law", };
		String s4[] =
		{ "", "1", "2", "3", "4" };

		// create the comboBox and set the border
		comboBox1 = new JComboBox<String>(s1);
		comboBox2 = new JComboBox<String>(s2);
		comboBox3 = new JComboBox<String>(s3);
		comboBox3.setBorder(BorderFactory.createBevelBorder(EtchedBorder.LOWERED));
		comboBox4 = new JComboBox<String>(s4);
		comboBox4.setBorder(BorderFactory.createBevelBorder(EtchedBorder.LOWERED));

		// create the buttons
		button1 = new JButton("Next step");
		button2 = new JButton("Clear");
		button3 = new JButton("Exit");

		// listening register
		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);

		// set action command
		button1.setActionCommand("Next step");
		button2.setActionCommand("Clear");
		button3.setActionCommand("Exit");

		// create the checkBox
		checkBox1 = new JCheckBox("I have read and agree to the terms and conditions");
		checkBox2 = new JCheckBox("I confirm that the information provided is correct");

		// add border to the textfields:
		textField1
				.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "First Name"));
		textField2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Surname"));

		// add borders to the panels:
		panel2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Name"));
		panel3.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Date of Birth"));
		panel4.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Programme"));
		panel5.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Email"));
		panel6.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Phone number"));
		panel7.setBorder(
				BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Booking reference code"));
		panel8.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY)));
		panel9.setBorder(BorderFactory.createBevelBorder(EtchedBorder.LOWERED));

		// add the components to the panels

		// panel1
		panel1.add(label1, BorderLayout.CENTER);

		// panel2
		panel2.add(textField1);
		panel2.add(textField2);

		// panel3
		panel3.add(comboBox1);
		panel3.add(comboBox2);

		// panel4
		panel4.add(comboBox3);
		panel4.add(comboBox4);

		// panel5
		panel5.add(textField3);
		// panel6
		panel6.add(textField4);
		// panel7
		panel7.add(textField5);

		// panel8
		panel8.add(checkBox1);
		panel8.add(checkBox2);

		// panel9
		panel9.add(button1);
		panel9.add(button2);
		panel9.add(button3);

		// add the panels to the frame:
		this.add(panel1);
		this.add(panel2);
		this.add(panel3);
		this.add(panel4);
		this.add(panel5);
		this.add(panel6);
		this.add(panel7);
		this.add(panel8);
		this.add(panel9);

		// set the layout of the frame
		this.setLayout(new GridLayout(9, 1));
		this.setTitle("Workshop Check In");

		// the settings of frame
		this.setSize(500, 800);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
	}

	/**
	 * Handle the click event.<br>
	 * When the user clicks on the "Clear" button, this program will erase the
	 * entered information and if there have box is highlighted in red, it will
	 * remove the red highlight to go back to original state.<br>
	 * <p>
	 * When the user clicks on the "Exit" button, this program will exit.<br>
	 * 
	 * @param e the event happened
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		// make the programe exit
		if (e.getActionCommand().equals("Exit"))
		{
			System.exit(0);

		}

		if (e.getActionCommand().equals("Clear"))
		{
			// clear the textfield
			textField1.setText("");
			textField2.setText("");
			textField3.setText("");
			textField4.setText("");
			textField5.setText("");

			// set the combobox to null
			comboBox1.setSelectedIndex(0);
			comboBox2.setSelectedIndex(0);
			comboBox3.setSelectedIndex(0);
			comboBox4.setSelectedIndex(0);

			// erase the red border
			textField1.setBorder(
					BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "First Name"));
			textField2
					.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Surname"));
			textField3.setBorder(null);
			textField4.setBorder(null);
			textField5.setBorder(null);
			comboBox1.setBorder(null);
			comboBox2.setBorder(null);
			comboBox3.setBorder(null);
			comboBox4.setBorder(null);
			checkBox1.setBorder(null);
			checkBox2.setBorder(null);

		}

	}

}
/**
 * 
 * Description : This class defines the frame used for showing information. 
 * Copyright(c) 2021
 * 
 * @author Yuxiong Wang
 * @version 1.0
 */
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;

public class AnotherFrame extends JFrame implements ActionListener
{
	/** The panel in the frame */
	JPanel panel1, panel2, panel3;
	/** The label in the first line. */
	JLabel label;
	/** The 'Finish' button. */
	JButton button;
	/** The board to show user's information. */
	JTextArea textArea;

	/**
	 * Class constructor.<br>
	 * To initialize the AnotherFrame class
	 * 
	 * @param info the String array used to store the information get from myFrame
	 */
	public AnotherFrame(String[] info)
	{

		// create the panel in the North,Center,South respectively
		panel1 = new JPanel();
		panel2 = new JPanel();
		panel3 = new JPanel();

		// create the lable in the top and set font of it
		label = new JLabel("You have checked in successfully.");
		Font font = new Font(null, Font.PLAIN, 20);
		label.setFont(font);

		// create the textArea and set font
		textArea = new JTextArea();
		Font font2 = new Font(null, Font.PLAIN, 15);
		textArea.setFont(font2);

		// add the information to the textfield:
		textArea.append("Full name: ");
		textArea.append(info[0] + "\n");
		textArea.append("Date of birth: ");
		textArea.append(info[1] + "\n");
		textArea.append("Programme: ");
		textArea.append(info[2] + "\n");
		textArea.append("Email: ");
		textArea.append(info[3] + "\n");
		textArea.append("Phone number: ");
		textArea.append(info[4] + "\n");
		// set the border and the background
		textArea.setBorder(BorderFactory.createBevelBorder(EtchedBorder.LOWERED));
		textArea.setBackground(null);
		// make the textArea can not be editable
		textArea.setEditable(false); // can editable

		// create the button
		button = new JButton("Finish");

		// add the components to the panels:
		panel1.add(label);
		panel2.add(textArea);
		panel2.setBorder(BorderFactory.createBevelBorder(EtchedBorder.LOWERED));
		panel3.add(button);
		panel3.setBorder(BorderFactory.createBevelBorder(EtchedBorder.LOWERED));

		// add the panel to the frame
		this.add(panel1, BorderLayout.NORTH);
		this.add(panel2, BorderLayout.CENTER);
		this.add(panel3, BorderLayout.SOUTH);

		// the settings of frame
		this.setSize(540, 240);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);

		this.setTitle("Check In complete!");

		// listening registration:
		button.addActionListener(this);

		// set action command:
		button.setActionCommand("Finish");

	}

	@Override
	/**
	 * Handle the click event.<br>
	 * When the user clicks on the "Finish" button, this program will exit.<br>
	 * 
	 * @param e the event happened
	 */
	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equals("Finish"))
		{
			System.exit(0);
		}

	}

}
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值