Java 编写航班查询系统 窗体程序 完整源码

开发环境
开发语言为Java,开发环境或者IDEA都可以 。数据库采用:MySQL 。运行主程序,或者执行打开JAR文件即可以运行本程序 。
系统框架
利用JDK自带的SWING框架开发,需要导入的包 。MySQL数据库,纯窗体模式,直接运行Main文件即可以 。同时带有详细得设计文档
系统主要功能
航班查询系统系统用Java语言编写,是一个简单的窗体程序,采用swing框架编写,系统涉及到文件、UI、数据库操作、事件等操作 。通过运行Main.java,或者直接运行解压文件假下的JAR文件
操作说明:
整个航班查询系统采用的图形化界面方式进行设计,最上面是菜单栏,下面是快捷工具栏
1 航班信息维护 。包括航班的新增,航班的修改、航班的删除 。航班信息包括:航班号、是否满仓、起飞时间、落地时间、票价、折扣
2 航班信息查询:包括航班号来查询航班信息,根据起始城市来查询
3 通过快捷图标来执行相关操作
4 更换系统背景图片
实现的主要效果

Java 编写航班查询系统 窗体程序 完整源码

文章插图
关键代码
【Java 编写航班查询系统 窗体程序 完整源码】import java.awt.*;import java.awt.event.*;import java.util.prefs.*;import javax.swing.*;/*** 这个类定义了程序的主界面. 包括菜单, 工具栏, 主功能操作按钮, 显示界面等.* @author wenwen*/public class MainFrame extends JFrame{public MainFrame(){/** 读入用户对程序界面进行配置的参数. */Preferences root = Preferences.userRoot();userPrefs = root.node("/net/sfte/jk88811");setTitle("航班管理信息系统 " + FlightMIS.getVersion());//设置窗口大小与上次系统关闭时一样.Toolkit kit = Toolkit.getDefaultToolkit();Dimension screenSize = kit.getScreenSize();int defaultWidth = screenSize.width;int defaultHeight = screenSize.height;int topLeftX = userPrefs.getInt("topLeftX", 0);int topLeftY = userPrefs.getInt("topLeftY", 0);int width = userPrefs.getInt("width", defaultWidth);int height = userPrefs.getInt("height", defaultHeight);//如果窗口大小与显示屏大小基本相等, 则最大化窗口.if (Math.abs(width - defaultWidth) <= 30 &&Math.abs(height - defaultHeight) <= 30){setBounds(defaultWidth / 10, defaultHeight / 10, (int) (defaultWidth / 1.2), (int) (defaultHeight / 1.2));setExtendedState(MAXIMIZED_BOTH);}else{setBounds(topLeftX, topLeftY, width, height);}//设置窗口小图标Image img = kit.getImage(getClass().getResource("images/icon.png"));setIconImage(img);//中部主面板, 显示图片.String imageName = userPrefs.get("backgroundImage", "images/plane.jpg");mainPanel = new MainPanel(imageName);add(mainPanel, BorderLayout.CENTER);/** 创建事件监听动作Action*/actionExit = new ExitAction();actionAbout = new AboutAction(this);actionInput = new InputAction(mainPanel);actionLoadIn = new LoadInAction(this);actionModify = new ModifyAction(mainPanel);actionDelete = new DeleteAction(mainPanel);actionQueryWithId = new QueryWithIdAction(mainPanel);actionQueryWithCity = new QueryWithCityAction(mainPanel);actionChangeBackground = new ChangeBackgroundAction(this, mainPanel);/** 主菜单设置*/JMenuBar menuBar = new JMenuBar();setJMenuBar(menuBar);//文件菜单JMenu fileMenu = new JMenu("文件(F)");fileMenu.setMnemonic('F');menuBar.add(fileMenu);//unfinishedJMenuItem systemItem = new JMenuItem("系统设置");systemItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));fileMenu.add(systemItem);JMenuItem changeImageItem = new JMenuItem(actionChangeBackground);fileMenu.add(changeImageItem);fileMenu.addSeparator();//退出系统.JMenuItem exitItem = new JMenuItem(actionExit);exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));fileMenu.add(exitItem);//操作菜单JMenu operationMenu = new JMenu("操作(O)");operationMenu.setMnemonic('O');menuBar.add(operationMenu);JMenu addFlightMenu = new JMenu("新增航班信息");operationMenu.add(addFlightMenu);JMenuItem addItem = new JMenuItem(actionInput);addItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK));addFlightMenu.add(addItem);JMenuItem loadInItem = new JMenuItem(actionLoadIn);loadInItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.CTRL_MASK));addFlightMenu.add(loadInItem);operationMenu.addSeparator();JMenu queryMenu = new JMenu("查询航班信息");operationMenu.add(queryMenu);JMenuItem queryWithIdItem = new JMenuItem(actionQueryWithId);queryWithIdItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));queryMenu.add(queryWithIdItem);JMenuItem queryWithCityItem = new JMenuItem(actionQueryWithCity);queryWithCityItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));queryMenu.add(queryWithCityItem);operationMenu.addSeparator();JMenuItem modifyItem = new JMenuItem(actionModify);modifyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, InputEvent.CTRL_MASK));operationMenu.add(modifyItem);JMenuItem deleteItem = new JMenuItem(actionDelete);deleteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK));operationMenu.add(deleteItem);//查看菜单JMenu viewMenu = new JMenu("查看(V)");viewMenu.setMnemonic('V');menuBar.add(viewMenu);JMenu lookAndFeelMenu = new JMenu("外观");viewMenu.add(lookAndFeelMenu);//外观单选菜单项所属的组group = new ButtonGroup();//设置程序外观(Look And Feel)String defaultLookAndFeel = userPrefs.get("lookAndFeel", UIManager.getCrossPlatformLookAndFeelClassName());try{UIManager.setLookAndFeel(defaultLookAndFeel);SwingUtilities.updateComponentTreeUI(MainFrame.this);}catch (Exception e){//do nothing}UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();for (UIManager.LookAndFeelInfo info : infos){addLookAndFeelMenuItem(lookAndFeelMenu, info.getName(),info.getClassName(), defaultLookAndFeel);}viewMenu.addSeparator();//状态栏statusBar = new StatusBar();add(statusBar, BorderLayout.SOUTH);boolean displayStatusBar = userPrefs.getBoolean("displayStatusBar", true);statusBar.setVisible(displayStatusBar);//控制是否显示状态栏的菜单项final JCheckBoxMenuItem statusBarItem = new JCheckBoxMenuItem("状态栏");statusBarItem.setSelected(displayStatusBar);statusBarItem.addActionListener(newActionListener(){public void actionPerformed(ActionEvent e){boolean sign = statusBarItem.isSelected();statusBar.setVisible(sign);validate();userPrefs.putBoolean("displayStatusBar", sign);}});viewMenu.add(statusBarItem);//帮助菜单JMenu helpMenu = new JMenu("帮助(H)");helpMenu.setMnemonic('H');menuBar.add(helpMenu);//unfinishedJMenuItem helpItem = new JMenuItem("系统帮助");helpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));//helpItem.addActionListener();helpMenu.add(helpItem);//unfinished.JMenuItem newVersionItem = new JMenuItem("检查新版本");//newVersionItem.addActionListener();helpMenu.add(newVersionItem);helpMenu.addSeparator();JMenuItem aboutItem = new JMenuItem(actionAbout);aboutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));helpMenu.add(aboutItem);//创建工具栏.toolBar = new JToolBar("工具栏");toolBar.setFloatable(false);toolBar.setRollover(true);add(toolBar, BorderLayout.NORTH);//控制是否显示工具栏.boolean displayToolBar = userPrefs.getBoolean("displayToolBar", true);toolBar.setVisible(displayToolBar);final JCheckBoxMenuItem toolBarItem = new JCheckBoxMenuItem("工具栏");viewMenu.add(toolBarItem);toolBarItem.setSelected(displayToolBar);toolBarItem.addActionListener(newActionListener(){public void actionPerformed(ActionEvent e){boolean sign = toolBarItem.isSelected();toolBar.setVisible(sign);validate();userPrefs.putBoolean("displayToolBar", sign);}});toolBar.add(createButton(actionLoadIn, "images/loadIn.png", "导入航班信息"));toolBar.add(createButton(actionInput, "images/input.png", "录入航班信息"));toolBar.addSeparator();toolBar.add(createButton(actionQueryWithId, "images/queryWithId.png", "按航班号查询"));toolBar.add(createButton(actionQueryWithCity, "images/queryWithCity.png", "按起飞抵达城市查询"));toolBar.addSeparator();toolBar.add(createButton(actionModify, "images/modify.png", "修改航班信息"));toolBar.add(createButton(actionDelete, "images/delete.png", "删除航班信息"));toolBar.addSeparator();toolBar.add(createButton(actionAbout, "images/about.png", "关于"));toolBar.add(createButton(actionExit, "images/exit.png", "退出系统"));//注册窗口关闭监听器addWindowListener(newWindowAdapter(){public void windowClosing(WindowEvent e){exitSystem();}});//设置弹出菜单createPopupMenu();}/*** 为每一种外观创建一个菜单项, 并安装相应的事件监听器* @author wenwen*/public void addLookAndFeelMenuItem(JMenu menu, String name, final String plafName, String defaultLookAndFeel){final JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(name);if (plafName.equals(defaultLookAndFeel))menuItem.setSelected(true);menuItem.addActionListener(newActionListener(){public void actionPerformed(ActionEvent e){if (menuItem.isSelected()){//将用户选择的外观保存到用户参数中, 以便下次启动时使用默认外观.userPrefs.put("lookAndFeel", plafName);//动态设置与菜单项对应的外观try{UIManager.setLookAndFeel(plafName);SwingUtilities.updateComponentTreeUI(MainFrame.this);MainFrame.this.remove(popupMenu);createPopupMenu();}catch (Exception ex){// do nothing.}}}});group.add(menuItem);menu.add(menuItem);}/*** 系统关闭时调用该方法, 保存系统参数以及尚未保存的航班信息资料.* @author wenwen*/public void exitSystem(){int result = JOptionPane.showConfirmDialog(MainFrame.this,"您确定要退出本航班信息管理系统吗?", "程序退出确认",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);if (result != JOptionPane.YES_OPTION)return ;//保存窗口位置参数, 以便下次启动时使用.userPrefs.putInt("topLeftX", getX());userPrefs.putInt("topLeftY", getY());userPrefs.putInt("width", getWidth());userPrefs.putInt("height", getHeight());//保存背景图片完整路径, 以便下次启动时载入.String imageName = mainPanel.getBackgroundImageName();userPrefs.put("backgroundImage", imageName);System.exit(0);}/*** 为工具栏创建按键* @author wenwen*/public JButton createButton(Action action, String imageName, String toolTip){JButton b = new JButton(new ImageIcon(getClass().getResource(imageName)));b.setToolTipText(toolTip);b.addActionListener(action);return b;}/*** 创建弹出式菜单* @author wenwen*/private void createPopupMenu(){popupMenu = new JPopupMenu();popupMenu.add(new JMenuItem(actionInput));popupMenu.add(new JMenuItem(actionLoadIn));popupMenu.addSeparator();popupMenu.add(new JMenuItem(actionQueryWithId));popupMenu.add(new JMenuItem(actionQueryWithCity));popupMenu.addSeparator();popupMenu.add(new JMenuItem(actionModify));popupMenu.add(new JMenuItem(actionDelete));popupMenu.addSeparator();popupMenu.add(new JMenuItem(actionChangeBackground));//将右键菜单添加到mainPanel上mainPanel.setComponentPopupMenu(popupMenu);//下面这条语句是必须的, 这是Java本身的一个bug. 请查看Java bug 4966109mainPanel.addMouseListener(new MouseAdapter() {});}/*** 系统退出事件处理.* @author wenwen*/private class ExitAction extends AbstractAction{public ExitAction(){putValue(Action.NAME, "退出");putValue(Action.SHORT_DESCRIPTION, "退出系统");}public void actionPerformed(ActionEvent e){exitSystem();}}//外观菜单所属的单选按钮组.private ButtonGroup group;//程序界面配置的用户参数.private final Preferences userPrefs;private StatusBar statusBar;private JToolBar toolBar;private MainPanel mainPanel;private JPopupMenu popupMenu;//事件监听动作Action.private ExitAction actionExit;private AboutAction actionAbout;private InputAction actionInput;private LoadInAction actionLoadIn;private ModifyAction actionModify;private DeleteAction actionDelete;private QueryWithIdAction actionQueryWithId;private QueryWithCityAction actionQueryWithCity;private ChangeBackgroundAction actionChangeBackground;}