航班信息查询与检索(java)

         大二上时用C语言写过一个关于航班信息查询与检索的课程设计,当时是自己抄代码,然后再让学长帮改的,前天晚上在“抄”代码时,突然想用java把那个课设题再写一次,于是昨天晚上就开始了,当然还是先在网上找了不少“成品”做参考,半借鉴,半修改,马马虎虎写了一个功能比较少的版本,以后再写更高版本的。

       这个小程序的代码分为四个模块:主框架(MainWindow),关于飞机航班号、起飞(目的)城市与起飞时间(Plane),对航班信息的添加查询等操作(FlightInfoOperation),以及对文件的处理(IOOperation)。

      主窗口图:

      航班信息查询与检索(java)_第1张图片

    添加窗口图:

  航班信息查询与检索(java)_第2张图片

  查询窗口图:

  (其中可以通过航班号、起飞城市任意一个查询或两者查询)

  航班信息查询与检索(java)_第3张图片

 

下面给出这个代码的部分代码:

FlightInfoOperation里的

	public FlightInfoOperation() {

		mainFrame = new Frame("航班信息查询与检索");
		mainFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		mainFrame.setSize(WIDTH, HEIGHT);
		mainFrame.setVisible(true);

		Dimension frameSize = mainFrame.getSize();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		mainFrame.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);

		buttonPanel = new Panel();
		mainFrame.add(buttonPanel, BorderLayout.NORTH);
		addButton = new Button("Add");
		addButton.addActionListener(this);
		searchButton = new Button("Search");
		searchButton.addActionListener(this);
		exitButton = new Button("Exit");
		exitButton.addActionListener(this);
		aboutButton = new Button("About");
		aboutButton.addActionListener(this);
		planeInfo = new TextArea();
		planeInfo.setFont(new Font("serif", Font.PLAIN, 18));
		buttonPanel.add(addButton);
		buttonPanel.add(searchButton);
		buttonPanel.add(exitButton);
		buttonPanel.add(aboutButton);
		mainFrame.add(planeInfo);

		/**
		 * 添加飞机信息的窗口设置
		 */
		addFrame = new Frame();
		addFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				addFrame.setVisible(false);
			}
		});

		inputLabPanel = new Panel(new GridLayout(4, 2));
		inputBtnPanel = new Panel();
		String[] inputLbName = { "airNum:", "StartCity:", "Destination:",
				"FlightTime" };
		String[] inputBtnName = { "Save", "Delete", "Quit" };
		// Add窗口中各字段名称、按钮设置
		for (int i = 0; i < 4; i++) {
			inputLab[i] = new Label(inputLbName[i]);
			inputTf[i] = new TextField(15);
			inputLabPanel.add(inputLab[i]);
			inputLabPanel.add(inputTf[i]);
		}
		for (int i = 0; i < 3; i++) {
			inputBtn[i] = new Button(inputBtnName[i]);
			inputBtn[i].addActionListener(this);
			inputBtnPanel.add(inputBtn[i]);
		}
		inputBtn[2].setActionCommand("add");// 用来获取对象的标签或事先为这个对象设置的命令名
		addFrame.add(inputLabPanel, BorderLayout.CENTER);
		addFrame.add(inputBtnPanel, BorderLayout.SOUTH);
		addFrame.pack();
		addFrame.setLocationRelativeTo(mainFrame);

		/**
		 * set searchFrame which is used to search plane information
		 */
		searchFrame = new Frame("Search plane");
		searchFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				searchFrame.setVisible(false);
			}
		});
		schLabPanel = new Panel(new GridLayout(2, 2));
		schBtnPanel = new Panel();
		String schLabName[] = { "airNum :", "StartCity :" };
		String schBtnName[] = { "Ok", "Quit" };
		for (int i = 0; i < 2; i++) {
			schLab[i] = new Label(schLabName[i]);
			schTf[i] = new TextField(15);
			schLabPanel.add(schLab[i]);
			schLabPanel.add(schTf[i]);
		}
		for (int i = 0; i < 2; i++) {
			schBtn[i] = new Button(schBtnName[i]);
			schBtn[i].addActionListener(this);
			schBtnPanel.add(schBtn[i]);
		}
		schBtn[1].setActionCommand("search");
		searchFrame.add(schLabPanel, BorderLayout.CENTER);
		searchFrame.add(schBtnPanel, BorderLayout.SOUTH);
		searchFrame.pack();
		searchFrame.setLocationRelativeTo(mainFrame);
		/**
		 * IO operation object
		 */
		ioo = new IOOperation();
		plane = ioo.getAllPlane();

	}


文件操作部分

public class IOOperation {

	private File file = new File("E:\\wenjianyuan\\info");

	public IOOperation() {
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * write to file
	 */
	public void write(Plane[] wPlane) {
		try {
			FileOutputStream fos = new FileOutputStream(file);
			ObjectOutputStream objOut = new ObjectOutputStream(fos);
			objOut.writeObject(wPlane);
			objOut.close();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * read all informations from the file
	 */
	public Plane[] getAllPlane() {
		Plane[] plane = new Plane[100];

		try {
			if (file.length() > 0) {
				FileInputStream fis = new FileInputStream(file);
				ObjectInputStream ois=new ObjectInputStream(fis);
				plane= (Plane[]) ois.readObject();
				ois.close();
				fis.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return plane;
	}
}


感兴趣的朋友可以到:http://download.csdn.net/detail/yanghai0321/4185470 下载完整代码。

你可能感兴趣的:(java,exception,String,File,search,button)