火车票查询项目计划进度安排
时间 | 任务分解 | 完成内容——代码(类、方法等) | |
界面 | 逻辑功能 | ||
第十三周 | 主界面设计实现 (车次、出发站、目的站、时间) | Train Ticket.java | TrainTime() |
功能:事件监听 | Train Ticket.java |
| |
第十四周 | 功能:车次查询 (站—站查询) | Query.java |
|
功能:车票查询 | Query.java |
| |
第十五周 | 功能:提示功能 (提示用户该控件如何使用) | Train Ticket.java |
|
功能:数据保存读取功能 (文本文件) | AddData.java |
| |
功能:购票 | AddData.java |
| |
第十六周 | 界面优化 |
|
|
整合及程序测试 |
|
|
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.plaf.basic.*;
//启动类(鼠标监听,列表监听,键盘监听)
public class TrainTime extends JFrame implements
ActionListener,MouseListener,ListSelectionListener,KeyListener
{
//创建描述表格标题的vector
private Vector<String> vhead = new Vector<String>();
{
vhead.add("车次");
vhead.add("始发站");
vhead.add("终点站");
vhead.add("列车类型");
vhead.add("发站");
vhead.add("发时");
vhead.add("到站");
vhead.add("到时");
}
//创建包含表格数据的vector
private Vector<Vector> vdata = new Vector<Vector>();
//创建表格型对象
private DefaultTableModel dtm = new DefaultTableModel(vdata,vhead);
//创建表格控件
private JTable jt = new JTable(dtm);
//将表格控件放到滚动窗体中
private JScrollPane jspTable = new JScrollPane(jt);
//创建表格的行排序器
TableRowSorter sorter = new TableRowSorter(dtm);
//创建面板,用来查询
private JPanel jp = new JPanel();
//创建窗体分割
private JSplitPane jsp = new JSplitPane(
JSplitPane.VERTICAL_SPLIT,jspTable,jp);
//创建单选按钮数组
private JRadioButton[] jrbArray = {
new JRadioButton("站站查询",true),
new JRadioButton("车次查询"),
new JRadioButton("车站所有车次查询")};
//创建单选按钮组
private ButtonGroup bgStation = new ButtonGroup();
//创建标签和文本框
private JLabel jlStart = new JLabel("出发站");
private JComboBox jcbStart = new JComboBox();
private JLabel jlEnd = new JLabel("目的站");
private JComboBox jcbEnd = new JComboBox();
private JLabel jlTid = new JLabel("车次");
private JTextField jtfTname = new JTextField();
private JLabel jlSid = new JLabel("车站站名");
private JComboBox jcbSname = new JComboBox();
//创建按钮
private JButton jbQuery = new JButton("查询");
private JButton jbApp = new JButton("附加功能");
//创建可编辑组合框的默认编辑器
private BasicComboBoxEditor bcbeStart = new BasicComboBoxEditor();
//创建组合框的默认类型
private DefaultComboBoxModel dcbmStart = new DefaultComboBoxModel();
private BasicComboBoxEditor bcbeEnd = new BasicComboBoxEditor();
private DefaultComboBoxModel dcbmEnd = new DefaultComboBoxModel();
private BasicComboBoxEditor bcbeMid = new BasicComboBoxEditor();
private DefaultComboBoxModel dcbmMid = new DefaultComboBoxModel();
private BasicComboBoxEditor bcbeSname = new BasicComboBoxEditor();
private DefaultComboBoxModel dcbmSname = new DefaultComboBoxModel();
//创建弹出菜单
PopupMenu popup = new PopupMenu();
//创建菜单
Menu jm = new Menu("添加");
//创建菜单项数组
MenuItem[] item = {new MenuItem("车次添加"),new MenuItem("车站添加"),
new MenuItem("关系添加"),new MenuItem("退出程序")};
//定义SystemTray成员变量
SystemTray tray;
//定义TrayIcon成员变量
TrayIcon trayIcon;
public TrainTime()
{
this.add(jsp);
//设置分隔条的位置
jsp.setDividerLocation(400);
//设置分隔条大小
jsp.setDividerSize(2);
//为表格添加排序器
jt.setRowSorter(sorter);
jt.setRowHeight(20);//设置表格行高
//为表格选择模型注册监听器
jt.getSelectionModel().addListSelectionListener(this);
jt.addMouseListener(this);
//设置表头不可编辑
jt.getTableHeader().setReorderingAllowed(false);
//设置JPanel布局为空
jp.setLayout(null);
//初始化JRadioButton数组
initJRadioButton();
//初始化文本框
initJTextField();
//初始化系统托盘
initTray();
//加载窗体图标
Image icon = Toolkit.getDefaultToolkit().getImage("img/icon.gif");
//设置窗体图标
this.setIconImage(icon);
//设置窗体的名称
this.setTitle("火车时刻表");
//使最大化按钮不可用
this.setResizable(false);
//设置窗体的大小及位置
this.setBounds(120,50,800,600);
//设置窗体的可见性
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initJRadioButton() {
// TODO Auto-generated method stub
for(int i=0;i<jrbArray.length;i++)
{
//设置单选按钮的大小
jrbArray[i].setBounds(200,50+i*30,130,30);
//将单选按钮加到JPanel中
jp.add(jrbArray[i]);
//将单选按钮加到按钮组里面
bgStation.add(jrbArray[i]);
//注册事件监听器
jrbArray[i].addActionListener(this);
}
}
private void initJTextField() {
// TODO Auto-generated method stub
//设置标签大小并将它加入JPanel中
jlStart.setBounds(150,20,50,30);
jp.add(jlStart);
//设置jcbStart的大小及位置
jcbStart.setBounds(210,25,80,20);
//设置jcbStart为可编辑状态
jcbStart.setEditable(true);
//为jcbStart设置编辑器
jcbStart.setEditor(bcbeStart);
//为jcbStart设置Model
jcbStart.setModel(dcbmStart);
jp.add(jcbStart);
//为文本框注册事件监听器
//jcbStart.addActionListener(this);
//注册鼠标事件监听器
jcbStart.getEditor().getEditorComponent().addMouseListener(this);
//注册键盘监听器
jcbStart.getEditor().getEditorComponent().addKeyListener(this);
jlEnd.setBounds(330,20,50,30);
jp.add(jlEnd);
jcbEnd.setBounds(390,25,80,20);
//将jcbEnd设置为可编辑
jcbEnd.setEditable(true);
//将jcbEnd设置editor及model
jcbEnd.setEditor(bcbeEnd);
jcbEnd.setModel(dcbmEnd);
jp.add(jcbEnd);
jlTid.setBounds(400,80,30,30);
jp.add(jlTid);
jtfTname.setBounds(460,85,80,20);
jp.add(jtfTname);
//为jtfTname添加鼠标监听器
jtfTname.addMouseListener(this);
//为jtfTname添加事件监听器
jtfTname.addActionListener(this);
jlSid.setBounds(400,110,60,30);
jp.add(jlSid);
jcbSname.setBounds(460,115,80,20);
//将jcbSname设置为可编辑
jcbSname.setEditable(true);
//为jcbSname设置editor及model
jcbSname.setEditor(bcbeSname);
jcbSname.setModel(dcbmSname);
jp.add(jcbSname);
//为jcbSname添加鼠标监听
jcbSname.getEditor().getEditorComponent().addMouseListener(this);
//为jcbSname添加键盘监听
jcbSname.getEditor().getEditorComponent().addKeyListener(this);
//为jcbSname添加事件监听
jcbSname.addActionListener(this);
//设置按钮位置及大小并加到JPanel
jbQuery.setBounds(580,105,60,30);
jp.add(jbQuery);
//为按钮添加事件监听器和鼠标监听器
jbQuery.addActionListener(this);
jbQuery.addMouseListener(this);
jbApp.setBounds(650,105,100,30);
jp.add(jbApp);
//添加监听器
jbApp.addMouseListener(this);
jbApp.addActionListener(this);
}
private void initTray() {
//添加菜单
for(int i=0;i<item.length;i++)
{
//为菜单注册监听器
item[i].addActionListener(this);
//为菜单项数组中前三个菜单项添加到“弹出菜单”
if(i<3)
{
jm.add(item[i]);
}
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args)
{
new TrainTime();
}
}