1、登录界面
把用户名和密码存储在user.txt文件里,在登录时,查询用户名和密码是否正确,如果正确,则进入到首页购票,如果用户名不存在,则进入注册界面。代码如下:
package 购票系统;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
@SuppressWarnings("unused")
public class denglu {
JFrame frame=new JFrame("登录");
JLabel label1=new JLabel();
JLabel label2=new JLabel("欢迎来到火车票购票系统",JLabel.CENTER);
JLabel label3=new JLabel("用户登录",JLabel.CENTER);
JLabel label4=new JLabel("用户名",JLabel.CENTER);
JLabel label5=new JLabel("密码",JLabel.CENTER);
JLabel label6=new JLabel("按ESC键退出",JLabel.CENTER);
JTextField n1=new JTextField();
JPasswordField n2=new JPasswordField();
JButton button1=new JButton("登录");
JButton button2=new JButton("注册");
JButton button3=new JButton("管理员登录");
static String tel="null";
public denglu() {
label2.setFont(new Font("",Font.BOLD,40));
label3.setFont(new Font("",Font.BOLD,30));
label4.setFont(new Font("",Font.BOLD,20));
label5.setFont(new Font("",Font.BOLD,20));
label5.setFont(new Font("",Font.CENTER_BASELINE,20));
frame.setLayout(null);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
frame.setUndecorated(true);
frame.setSize(screenSize.width, screenSize.height);
ImageIcon i=new ImageIcon("./1.jpg");
i.getImage();
Image temp=i.getImage().getScaledInstance((int)screenSize.getWidth()/2,(int)screenSize.getHeight()-200, Image.SCALE_DEFAULT);
i=new ImageIcon(temp);
label1.setIcon(i);
label1.setBounds(20, 100, screenSize.width/2, screenSize.height-80);
label2.setBounds(0,50,screenSize.width/2, 100);
label3.setBounds(screenSize.width/2,250,screenSize.width/2, 100);
label4.setBounds(screenSize.width/2,350,screenSize.width/4, 30);
label6.setBounds(screenSize.width-100,0,100, 50);
n1.setBounds(screenSize.width/2+screenSize.width/6,350,300, 30);
label5.setBounds(screenSize.width/2,400,screenSize.width/4, 30);
n2.setBounds(screenSize.width/2+screenSize.width/6,400,300, 30);
button1.setBounds(screenSize.width/2+200,500,100, 30);
button2.setBounds(screenSize.width/2+350,500,100, 30);
button3.setBounds(screenSize.width/2+260,550,150, 30);
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.add(label4);
frame.add(label5);
frame.add(label6);
frame.add(n1);
frame.add(n2);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Toolkit.getDefaultToolkit().addAWTEventListener((AWTEventListener) new AWTEventListener() {
public void eventDispatched(AWTEvent event) {
if (((KeyEvent) event).getID() == KeyEvent.KEY_PRESSED) {
switch(((KeyEvent)event).getKeyCode()) {
case KeyEvent.VK_ESCAPE: System.exit(0); break;
}
}
}
}, AWTEvent.KEY_EVENT_MASK);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@SuppressWarnings("deprecation")
String sum=n1.getText()+" "+n2.getText();
boolean cot=false;
String s;
try {
@SuppressWarnings("resource")
BufferedReader r=new BufferedReader(new FileReader("./user.txt"));
while((s=r.readLine())!=null) {
if(s.equals(sum)) {
cot=true;
tel=n1.getText();
}
}
if(cot) {
try {
BufferedWriter w=new BufferedWriter(new FileWriter("./ex.txt",true));
String Tel=tel;
w.write(Tel);
w.flush();
w.close();
} catch (IOException e1) {
e1.printStackTrace();
}
new shouye();
frame.dispose();
}else {
JOptionPane.showMessageDialog(null, "用户名或者密码错误,登录失败!");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// String sum=n1.getText()+" "+n2.getText();
@SuppressWarnings("deprecation")
String sum=n1.getText()+" "+n2.getText();
if(sum.equals("111 111")) {
new guanli();
frame.dispose();
}else {
JOptionPane.showMessageDialog(null, "用户名或者密码错误,登录失败!");
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new zhuce();
}
});
}
public String GetTel() {
return tel;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new denglu();
}
}
运行结果如下:
2、注册界面:如果登录时用户名不存在,则进入注册界面,注册时用户名用的是电话号码,注册时会首先在文件里查询用户名是否存在,如果用户名存在,则无法注册,否则输入用户名和密码可以显示注册成功,之后可以返回登录,代码如下:
package 购票系统;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
@SuppressWarnings("unused")
public class zhuce {
JFrame fm=new JFrame("用户注册");
JLabel label1=new JLabel("用户名",JLabel.CENTER);
JLabel label2=new JLabel("密码",JLabel.CENTER);
JTextField n1=new JTextField();
JTextField n2=new JTextField();
JButton button1=new JButton("确定");
JButton button2=new JButton("返回");
public zhuce() {
fm.setLayout(null);
label1.setBounds(0,20,50, 40);
n1.setBounds(80,30,150, 30);
label2.setBounds(0,70,50, 40);
n2.setBounds(80,80,150, 30);
button1.setBounds(50,170,80, 30);
button2.setBounds(150,170,80, 30);
fm.add(button1);
fm.add(button2);
fm.add(label1);
fm.add(n1);
fm.add(label2);
fm.add(n2);
fm.setSize(300,300);
fm.setResizable(false);
fm.setLocationRelativeTo(null);
fm.setVisible(true);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
BufferedWriter w=new BufferedWriter(new FileWriter("./user.txt",true));
String zhanghao=n1.getText();
String sum=n1.getText()+" "+n2.getText();
@SuppressWarnings("resource")
BufferedReader r=new BufferedReader(new FileReader("./user.txt"));
boolean cot=true;
String s;
while((s=r.readLine())!=null) {
if(sum.equals(s)) {
cot=false;
}
}
if(cot && zhanghao.length()==11) {
w.write(sum);
w.newLine();
w.flush();
w.close();
JOptionPane.showMessageDialog(null, "注册成功!");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./information.txt",true));
String te="null null null "+sum;
bw.write(te);
bw.newLine();
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}else {
JOptionPane.showMessageDialog(null, "账号已经存在");
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new denglu();
fm.dispose();
}
});
}
}
运行结果如下:
3、管理员登录,管理员的账号和密码设定为111和111,输入正确的账号和密码后登录到管理员界面,在管理员界面,可以对车票进行增加,修改和删除。代码如下:
package 购票系统;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
@SuppressWarnings("unused")
public class guanli {
JFrame fram=new JFrame();
JMenuBar bar=new JMenuBar();
JMenu menu1=new JMenu("火车票管理");
JMenuItem item1=new JMenuItem("增加");
JMenuItem item2=new JMenuItem("修改");
JMenuItem item3=new JMenuItem("删除");
JButton but=new JButton("返回");
String[] columnNames = {"检票口","起点站","终点站","出发时间","到达时间","票价","余票"," "};
Object[][] data = {
{"0","0","0","0","0",0,0,"0"},
};
// JTable table = new JTable(data, columnNames);
@SuppressWarnings("serial")
JTable table = new JTable(data, columnNames){
public boolean isCellEditable(int row, int column) {
return false;
}
};
String name="./train/1.txt";
File file=new File(name);
BufferedReader reader=null;
String temp=null;
int line=0;
String []words=new String[1000];
JLabel label1=new JLabel("日期:",JLabel.CENTER);
JLabel label2=new JLabel("车次:",JLabel.CENTER);
JLabel label3=new JLabel("起点站:",JLabel.CENTER);
JLabel label4=new JLabel("终点站:",JLabel.CENTER);
JLabel label5=new JLabel("出发时间:",JLabel.CENTER);
JLabel label6=new JLabel("到达时间:",JLabel.CENTER);
JLabel label7=new JLabel("票价:",JLabel.CENTER);
JLabel label8=new JLabel("余票:",JLabel.CENTER);
JTextField n1=new JTextField();
JTextField n2=new JTextField();
@SuppressWarnings("rawtypes")
JComboBox n3=new JComboBox();
@SuppressWarnings("rawtypes")
JComboBox n4=new JComboBox();
JTextField n5=new JTextField();
JTextField n6=new JTextField();
JTextField n7=new JTextField();
JTextField n8=new JTextField();
JButton button1=new JButton("添加");
JButton button2=new JButton("取消");
JButton button3=new JButton("删除");
@SuppressWarnings("rawtypes")
JComboBox status=new JComboBox();
Calendar c = Calendar.getInstance();
@SuppressWarnings({ "unchecked", "rawtypes" })
public guanli() {
try{
reader=new BufferedReader(new FileReader(file));
while((temp=reader.readLine())!=null){
words[line]=temp;
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
String ss[][]=new String[1000][];
for(int i=0;i<line;i++) {
ss[i]=words[i].split(" ");
}
DefaultTableCellRenderer dc=new DefaultTableCellRenderer();
dc.setHorizontalAlignment(JLabel.CENTER);
table = new JTable(ss, columnNames);
table.setDefaultRenderer(Object.class, dc);
table.getTableHeader().setFont(new Font("宋体",Font.BOLD,14));
table.setFont(new Font("宋体",Font.PLAIN,14));
table.setRowHeight(20);
JTableHeader he=table.getTableHeader();
he.setBounds(0,0,screenSize.width-400,30);
int d=c.get(Calendar.DAY_OF_MONTH);
int m=(c.get(Calendar.MONTH) + 1);
int y=c.get(Calendar.YEAR);
int [] month1= {31,29,31,30,31,30,31,31,30,31,30,31};
int [] month2= {31,28,31,30,31,30,31,31,30,31,30,31};
int [] month=new int[12];
if(y%400==0||(y%4==0&&y%100!=0)) {
month=month1;
}
else month=month2;
String [] cs= new String[20];
for(int ii=0;ii<20;ii++) {
cs[ii]=y+"-"+m+"-"+d;
d+=1;
if(d>month[m-1]) {
d=1;m++;
}
if(m>12) {
m=1;y++;
}
}
String [] cs1= {"北京","上海","杭州","广州","南京","成都","西安","郑州","重庆","合肥","汉口","武汉","长沙","武昌","太原","苏州","厦门","南昌","沈阳","天津","深圳"};
status=new JComboBox(cs);
n3=new JComboBox(cs1);
n4=new JComboBox(cs1);
table.setBounds(0,30,screenSize.width-400,screenSize.height-30);
label1.setBounds(screenSize.width-350,50,50, 40);
status.setBounds(screenSize.width-250,60,150, 30);
n1.setBounds(screenSize.width-250,110,150, 30);
label2.setBounds(screenSize.width-350,100,50, 40);
n2.setBounds(screenSize.width-250,110,150, 30);
label3.setBounds(screenSize.width-350,150,50, 40);
n3.setBounds(screenSize.width-250,160,150, 30);
label4.setBounds(screenSize.width-350,200,50, 40);
n4.setBounds(screenSize.width-250,210,150, 30);
label5.setBounds(screenSize.width-350,250,70, 40);
n5.setBounds(screenSize.width-250,260,150, 30);
label6.setBounds(screenSize.width-350,300,70, 40);
n6.setBounds(screenSize.width-250,310,150, 30);
label7.setBounds(screenSize.width-350,350,50, 40);
n7.setBounds(screenSize.width-250,360,150, 30);
label8.setBounds(screenSize.width-350,400,50, 40);
n8.setBounds(screenSize.width-250,410,150, 30);
button1.setBounds(screenSize.width-300,500,80, 30);
button3.setBounds(screenSize.width-300,500,80, 30);
button2.setBounds(screenSize.width-200,500,80, 30);
fram.add(he);
label1.setVisible(false);
status.setVisible(false);
label2.setVisible(false);
n2.setVisible(false);
label3.setVisible(false);
n3.setVisible(false);
label4.setVisible(false);
n4.setVisible(false);
label5.setVisible(false);
n5.setVisible(false);
label6.setVisible(false);
n6.setVisible(false);
label7.setVisible(false);
n7.setVisible(false);
label8.setVisible(false);
n8.setVisible(false);
button1.setVisible(false);
button2.setVisible(false);
n1.setVisible(false);
button3.setVisible(false);
fram.add(table);
fram.add(label1);
fram.add(status);
fram.add(label2);
fram.add(n2);
fram.add(label3);
fram.add(n3);
fram.add(label4);
fram.add(n4);
fram.add(label5);
fram.add(n5);
fram.add(label6);
fram.add(n6);
fram.add(label7);
fram.add(n7);
fram.add(label8);
fram.add(n8);
fram.add(button1);
fram.add(button2);
fram.add(n1);
fram.add(button3);
fram.setLayout(null);
bar.add(menu1);
menu1.add(item1);
menu1.addSeparator();
menu1.add(item2);
menu1.addSeparator();
menu1.add(item3);
but.setBounds(screenSize.width-100, 0, 100,30);
fram.add(but);
fram.setUndecorated(true);
fram.setSize(screenSize);
fram.setJMenuBar(bar);
fram.setResizable(false);
fram.setLocationRelativeTo(null);
fram.setVisible(true);
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new denglu();
}
});
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(true);
status.setVisible(true);
label2.setVisible(true);
n2.setVisible(true);
label3.setVisible(true);
n3.setVisible(true);
label4.setVisible(true);
n4.setVisible(true);
label5.setVisible(true);
n5.setVisible(true);
label6.setVisible(true);
n6.setVisible(true);
label7.setVisible(true);
n7.setVisible(true);
label8.setVisible(true);
n8.setVisible(true);
button1.setVisible(true);
button2.setVisible(true);
n1.setVisible(false);
button3.setVisible(false);
}});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str1=status.getSelectedItem().toString();
String str2=n2.getText();
String str3=n3.getSelectedItem().toString();
String str4=n4.getSelectedItem().toString();
String str5=n5.getText();
String str6=n6.getText();
String str7=n7.getText();
String str8=n8.getText();
String[]data1={str2,str3,str4,str5,str6,str7,str8,"订购"};
if(ss.length<=1000) {
ss[line]=data1;
line++;
}
TableModel tableModel=new DefaultTableModel(ss,columnNames);
table.setModel(tableModel);
int a=1;
int b=0;
int mo=(c.get(Calendar.MONTH) + 1);
String []tiqu1=str1.split("-");
String d=tiqu1[2];
int dd=Integer.parseInt(d);
if(mo==12) {
b=dd-31;
a+=b;
}
if(mo==1) {
a=0;
a+=dd;
}
String nam="./train/"+a+".txt";
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(nam,true));
String te=str2+" "+str3+" "+str4+" "+str5+" "+str6+" "+str7+" "+str8+" "+"订购";
bw.write(te);
bw.newLine();
bw.flush();
bw.close();
JOptionPane.showMessageDialog(null, "添加成功!");
} catch (IOException e1) {
e1.printStackTrace();
}
}});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(false);
status.setVisible(false);
label2.setVisible(false);
n2.setVisible(false);
label3.setVisible(false);
n3.setVisible(false);
label4.setVisible(false);
n4.setVisible(false);
label5.setVisible(false);
n5.setVisible(false);
label6.setVisible(false);
n6.setVisible(false);
label7.setVisible(false);
n7.setVisible(false);
label8.setVisible(false);
n8.setVisible(false);
button1.setVisible(false);
button2.setVisible(false);
n1.setVisible(false);
button3.setVisible(false);
}});
item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(false);
status.setVisible(false);
label2.setVisible(false);
n2.setVisible(false);
label3.setVisible(false);
n3.setVisible(false);
label4.setVisible(false);
n4.setVisible(false);
label5.setVisible(false);
n5.setVisible(false);
label6.setVisible(false);
n6.setVisible(false);
label7.setVisible(false);
n7.setVisible(false);
label8.setVisible(false);
n8.setVisible(false);
button1.setVisible(false);
button2.setVisible(false);
n1.setVisible(false);
button3.setVisible(false);
}});
item3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(true);
status.setVisible(true);
label2.setVisible(true);
n2.setVisible(true);
label3.setVisible(true);
n3.setVisible(true);
label4.setVisible(true);
n4.setVisible(true);
label5.setVisible(true);
n5.setVisible(true);
label6.setVisible(true);
n6.setVisible(true);
label7.setVisible(true);
n7.setVisible(true);
label8.setVisible(true);
n8.setVisible(true);
button1.setVisible(false);
button2.setVisible(true);
n1.setVisible(false);
button3.setVisible(true);
}});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str1=status.getSelectedItem().toString();
String str2=n2.getText();
String str3=n3.getSelectedItem().toString();
String str4=n4.getSelectedItem().toString();
String str5=n5.getText();
String str6=n6.getText();
String str7=n7.getText();
String str8=n8.getText();
String[]data1={str2,str3,str4,str5,str6,str7,str8,"订购"};
boolean cat=false;
String q=str2+" "+str3+" "+str4+" "+str5+" "+str6+" "+str7+" "+str8+" "+"订购";
// System.out.println(q);
int a=1;
int b=0;
int mo=(c.get(Calendar.MONTH) + 1);
String []tiqu1=str1.split("-");
String d=tiqu1[2];
int dd=Integer.parseInt(d);
if(mo==12) {
b=dd-31;
a+=b;
}
if(mo==1) {
a=0;
a+=dd;
}
String nam="./train/"+a+".txt";
try{
reader=new BufferedReader(new FileReader(nam));
while((temp=reader.readLine())!=null && line<1000){
words[line]=temp;
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
for(int i=0;i<words.length;i++) {
// System.out.println(words[i]);
if(words[i].equals(q)) {
cat=true;
}
}
if(cat) {
for(int j=0;j<words.length;j++) {
if(words[j].equals(q)) {
for(int w=0;w<words.length-1;w++) {
words[w]=words[w+1];
}
words[words.length-1]="";
}
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(nam));
for(int h=0;h<ss.length;h++) {
bw.write(words[h]);
bw.newLine();
}
bw.flush();
bw.close();
JOptionPane.showMessageDialog(null, "删除成功!");
} catch (IOException e1) {
e1.printStackTrace();
}
}
else {
JOptionPane.showMessageDialog(null, "航班不存在");
}
}});
}
}
运行结果如下:
4、首页,可以进行订单查询以及购票,选择车票的日期,终点站和始点站后可以进行查询,点击购票可以进行买票,买票时需要输入正确的身份证号和手机号,限制了身份证号长度为18,手机号长度为11位,购票成功后相关信息会存储到ticket/all.txt 和ticket/wei.txt文件里。可以查询个人所有订单,可以查询个人信息,修改个人信息,解绑手机号以及退出登录,首页代码如下:
package 购票系统;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
@SuppressWarnings("unused")
public class shouye {
JFrame fm=new JFrame();
JLabel label1=new JLabel();
JLabel label6=new JLabel("按ESC键退出",JLabel.CENTER);
JMenuBar bar=new JMenuBar();
JMenu menu1=new JMenu("购票中心");
JMenu menu2=new JMenu("订单中心");
JMenu menu3=new JMenu("我的");
JLabel lab=new JLabel("个人订单",JLabel.CENTER);
JMenuItem item1=new JMenuItem("全部订单");
JMenuItem item2=new JMenuItem("未出行订单");
JMenuItem item3=new JMenuItem("已出行订单");
JMenuItem item4=new JMenuItem("购买火车票");
JMenuItem item5=new JMenuItem("查看个人信息");
JMenuItem item6=new JMenuItem("修改个人信息");
JMenuItem item7=new JMenuItem("解绑手机号 ");
JMenuItem item8=new JMenuItem("退出登录");
JRadioButton rb1=new JRadioButton("男");
JRadioButton rb2=new JRadioButton("女");
ButtonGroup group=new ButtonGroup();
Font font=new Font("宋体",Font.BOLD,20);
JLabel label2=new JLabel("日期");
@SuppressWarnings("rawtypes")
JComboBox status=new JComboBox();
JLabel label3=new JLabel("始点站");
@SuppressWarnings("rawtypes")
JComboBox status1=new JComboBox();
JLabel label4=new JLabel("终点站");
@SuppressWarnings("rawtypes")
JComboBox status2=new JComboBox();
JButton button1=new JButton("查询");
JLabel lab1=new JLabel("昵称:");
JLabel lab2=new JLabel("真实姓名:");
JLabel lab3=new JLabel("性别:");
JLabel lab4=new JLabel("电话号码:");
JLabel f1=new JLabel();
JLabel f2=new JLabel();
JLabel f3=new JLabel();
JLabel f4=new JLabel();
JTextField n1=new JTextField();
JTextField n2=new JTextField();
JTextField n3=new JTextField();
JButton butt1=new JButton("确认");
JButton butt2=new JButton("清空");
String[] columnNames = {"检票口","起点站","终点站","出发时间","到达时间","票价","余票"," "};
Object[][] data = {
{"0","0","0","0","0",0,0,"0"},
};
Calendar c = Calendar.getInstance();
String name="./train/1.txt";
File file=new File(name);
BufferedReader reader=null;
String temp=null;
int line=0;
String []words=new String[1000];
//JTable table = new JTable(data, columnNames);
@SuppressWarnings("serial")
JTable table = new JTable(data, columnNames){
public boolean isCellEditable(int row, int column) {
return false;
}
};
JScrollPane scrollPane = new JScrollPane(table);
String Name="null";
String T="null";
@SuppressWarnings({ "unchecked", "rawtypes" })
public shouye() {
lab1.setFont(new Font("",Font.BOLD,20));
lab2.setFont(new Font("",Font.BOLD,20));
lab3.setFont(new Font("",Font.BOLD,20));
lab4.setFont(new Font("",Font.BOLD,20));
f1.setFont(new Font("",Font.BOLD,20));
f2.setFont(new Font("",Font.BOLD,20));
f3.setFont(new Font("",Font.BOLD,20));
f4.setFont(new Font("",Font.BOLD,20));
try{
reader=new BufferedReader(new FileReader(file));
while((temp=reader.readLine())!=null){
words[line]=temp;
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
String ss[][]=new String[line][];
for(int i=0;i<line;i++) {
ss[i]=words[i].split(" ");
}
try{
reader=new BufferedReader(new FileReader("./ex.txt"));
while((temp=reader.readLine())!=null){
T=temp;
File f=new File("./ex.txt");
FileWriter fw=new FileWriter(f);
fw.write("");
fw.flush();
fw.close();
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
int xx=0;
String [][]NN=new String[1000][];
try{
reader=new BufferedReader(new FileReader("./information.txt"));
while((temp=reader.readLine())!=null &&xx<1000){
NN[xx]=temp.split(" ");
if(NN[xx].length==5 && NN[xx][3].equals(T)) {
Name=NN[xx][1];
}
xx++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
group.add(rb1);
group.add(rb2);
table = new JTable(ss, columnNames);
int [] month1= {31,29,31,30,31,30,31,31,30,31,30,31};
int [] month2= {31,28,31,30,31,30,31,31,30,31,30,31};
int d=c.get(Calendar.DAY_OF_MONTH);
int m=(c.get(Calendar.MONTH) + 1);
int y=c.get(Calendar.YEAR);
lab.setFont(new Font("",Font.BOLD,20));
fm.setLayout(null);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
fm.setUndecorated(true);
fm.setSize(screenSize);
lab.setBounds(0, 0,screenSize.width, 30);
ImageIcon i=new ImageIcon("./2.jpg");
@SuppressWarnings("static-access")
Image temp=i.getImage().getScaledInstance((int)screenSize.getWidth(),(int)screenSize.getHeight(), i.getImage().SCALE_DEFAULT);
i=new ImageIcon(temp);
label1.setIcon(i);
label1.setBounds(0, 0, screenSize.width, screenSize.height);
label1.setVisible(false);
fm.getContentPane().add(scrollPane,BorderLayout.CENTER);
label6.setBounds(screenSize.width-100,0,100, 30);
int [] month=new int[12];
if(y%400==0||(y%4==0&&y%100!=0)) {
month=month1;
}
else month=month2;
String [] cs= new String[20];
String [] cs1= {"北京","上海","杭州","广州","南京","成都","西安","郑州","重庆","合肥","汉口","武汉","长沙","武昌","太原","苏州","厦门","南昌","沈阳","天津","深圳"};
for(int ii=0;ii<20;ii++) {
cs[ii]=y+"-"+m+"-"+d;
d+=1;
if(d>month[m-1]) {
d=1;m++;
}
if(m>12) {
m=1;y++;
}
}
menu1.setFont(font);
menu2.setFont(font);
menu3.setFont(font);
status=new JComboBox(cs);
status1=new JComboBox(cs1);
status2=new JComboBox(cs1);
label2.setBounds(50, 0, 50, 30);
status.setBounds(100,0, 130, 30);
label3.setBounds(300,0, 50, 30);
status1.setBounds(350,0,100, 30);
label4.setBounds(500,0, 50, 30);
status2.setBounds(550,0, 100, 30);
button1.setBounds(700,0, 70, 30);
lab1.setBounds(30,30,50, 30);
lab2.setBounds(30,70,100, 30);
lab3.setBounds(30,110,50, 30);
lab4.setBounds(30,150,100, 30);
f1.setBounds(100,30,400, 30);
f2.setBounds(150,70,200, 30);
f3.setBounds(100,110,200, 30);
f4.setBounds(150,150,400, 30);
n1.setBounds(150,30,200, 30);
n2.setBounds(150,70,200, 30);
n3.setBounds(150,110,200, 30);
rb1.setBounds(150, 110, 100, 30);
rb2.setBounds(250, 110, 100, 30);
butt1.setBounds(100,200,60, 30);
butt2.setBounds(160,200,60, 30);
menu1.add(item4);
menu2.add(item1);
menu2.addSeparator();
menu2.add(item2);
menu2.addSeparator();
menu2.add(item3);
menu3.add(item5);
menu3.addSeparator();
menu3.add(item6);
menu3.addSeparator();
menu3.add(item7);
menu3.addSeparator();
menu3.add(item8);
bar.add(menu1);
bar.add(menu2);
bar.add(menu3);
fm.add(label6);
fm.add(status);
fm.add(label2);
fm.add(label3);
fm.add(label4);
fm.add(lab);
fm.add(lab1);
fm.add(lab2);
fm.add(lab3);
fm.add(lab4);
fm.add(f1);
fm.add(f2);
fm.add(f3);
fm.add(f4);
fm.add(n1);
fm.add(n2);
fm.add(n3);
fm.add(butt1);
fm.add(butt2);
fm.add(rb1);
fm.add(rb2);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
lab4.setVisible(false);
f1.setVisible(false);
f2.setVisible(false);
f3.setVisible(false);
f4.setVisible(false);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
lab.setVisible(false);
fm.add(status1);
fm.add(status2);
fm.add(button1);
fm.add(label1);
DefaultTableCellRenderer dc=new DefaultTableCellRenderer();
dc.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class, dc);
table.getTableHeader().setFont(new Font("宋体",Font.BOLD,14));
table.setFont(new Font("宋体",Font.PLAIN,14));
table.setRowHeight(20);
JTableHeader he=table.getTableHeader();
he.setBounds(0,50,screenSize.width,30);
table.setBounds(0,80,screenSize.width,screenSize.height-200);
fm.add(he);
fm.add(table);
fm.setJMenuBar(bar);
fm.setResizable(false);
fm.setLocationRelativeTo(null);
fm.setVisible(true);
table.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int r= table.getSelectedRow();
int c= table.getSelectedColumn();
String ri1=status.getSelectedItem().toString();
String shi1=status1.getSelectedItem().toString();
String zhong1=status2.getSelectedItem().toString();
if(c==7) {
String info=Name+" "+ri1+" "+shi1+" "+zhong1+" "+table.getValueAt(r,0).toString()+" "+
"null"+" "+ table.getValueAt(r,5).toString()+" "+"未出行"+" "+table.getValueAt(r,3).toString();
try {
BufferedWriter w=new BufferedWriter(new FileWriter("./ex.txt",true));
w.write(info);
w.flush();
w.close();
} catch (IOException e1) {
e1.printStackTrace();
}
new goumai();
// javax.swing.JOptionPane.showMessageDialog(null,info);
}
}
@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
}});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String ri=status.getSelectedItem().toString();
String shi=status1.getSelectedItem().toString();
String zhong=status2.getSelectedItem().toString();
int a=1;
int b=0;
int mo=(c.get(Calendar.MONTH) + 1);
String []tiqu1=ri.split("-");
String d=tiqu1[2];
int dd=Integer.parseInt(d);
if(mo==12) {
b=dd-31;
a+=b;
}
if(mo==1) {
a=0;
a+=dd;
}
String ss1[][]=new String[1000][];
String ssss[][]=new String[1000][];
int mm=0;
String nam="./train/"+a+".txt";
File fil=new File(nam);
BufferedReader reade=null;
String temp=null;
int line=0;
String []words=new String[1000];
try{
reade=new BufferedReader(new FileReader(fil));
while((temp=reade.readLine())!=null &&line<1000){
words[line]=temp;
ss1[line]=words[line].split(" ");
if(ss1[line][1].equals(shi) &&ss1[line][2].equals(zhong)) {
ssss[mm]=ss1[line];
mm++;
}
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
TableModel tableModel=new DefaultTableModel(ssss,columnNames);
table.setModel(tableModel);
}
});
item4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(false);
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
lab4.setVisible(false);
f1.setVisible(false);
f2.setVisible(false);
f3.setVisible(false);
f4.setVisible(false);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
lab.setVisible(false);
he.setVisible(true);
table.setVisible(true);
label2.setVisible(true);
label3.setVisible(true);
label4.setVisible(true);
status.setVisible(true);
status1.setVisible(true);
status2.setVisible(true);
button1.setVisible(true);
TableModel tableModel=new DefaultTableModel(ss,columnNames);
table.setModel(tableModel);
table.setVisible(true);
}});
item8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new denglu();
}});
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(false);
lab.setVisible(true);
he.setVisible(true);
table.setVisible(true);
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
lab4.setVisible(false);
f1.setVisible(false);
f2.setVisible(false);
f3.setVisible(false);
f4.setVisible(false);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
status.setVisible(false);
status1.setVisible(false);
status2.setVisible(false);
button1.setVisible(false);
String[] columnName = {"姓名","出行日期","起点站","终点站","车次","座位号","票价","出行状态"};
File file=new File("./ticket/all.txt");
BufferedReader reader=null;
String temp=null;
int line=0;
String [][]ss1=new String[1000][];
String [][]ff=new String[1000][];
int q=0;
try{
reader=new BufferedReader(new FileReader(file));
while((temp=reader.readLine())!=null){
ff[line]=temp.split(" ");
if(ff[line][0].equals(Name)) {
ss1[q]=ff[line];
q++;
}
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
TableModel tableModel=new DefaultTableModel(ss1,columnName);
table.setModel(tableModel);
}});
item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
lab.setVisible(true);
label1.setVisible(false);
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
lab4.setVisible(false);
f1.setVisible(false);
f2.setVisible(false);
f3.setVisible(false);
f4.setVisible(false);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
status.setVisible(false);
status1.setVisible(false);
status2.setVisible(false);
button1.setVisible(false);
he.setVisible(true);
table.setVisible(true);
String[] columnName = {"姓名","出行日期","起点站","终点站","车次","座位号","票价","出行状态"};
File file=new File("./ticket/wei.txt");
BufferedReader reader=null;
String temp=null;
int line=0;
String [][]ss1=new String[1000][];
String [][]ff=new String[1000][];
int q=0;
try{
reader=new BufferedReader(new FileReader(file));
while((temp=reader.readLine())!=null){
ff[line]=temp.split(" ");
if(ff[line][0].equals(Name)) {
ss1[q]=ff[line];
q++;
}
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
TableModel tableModel=new DefaultTableModel(ss1,columnName);
table.setModel(tableModel);
}});
item3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
lab.setVisible(true);
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
lab4.setVisible(false);
f1.setVisible(false);
f2.setVisible(false);
f3.setVisible(false);
f4.setVisible(false);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
status.setVisible(false);
status1.setVisible(false);
status2.setVisible(false);
button1.setVisible(false);
he.setVisible(true);
table.setVisible(true);
label1.setVisible(false);
String[] columnName = {"姓名","出行日期","起点站","终点站","车次","座位号","票价","出行状态"};
File file=new File("./ticket/yi.txt");
BufferedReader reader=null;
String tem=null;
int lin=0;
String [][]ss1=new String[1000][];
String [][]ff=new String[1000][];
int q=0;
try{
reader=new BufferedReader(new FileReader(file));
while((tem=reader.readLine())!=null){
ff[lin]=tem.split(" ");
if(ff[lin][0].equals(Name)) {
ss1[q]=ff[lin];
q++;
}
lin++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
TableModel tableModel=new DefaultTableModel(ss1,columnName);
table.setModel(tableModel);
}});
item5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
he.setVisible(false);
label1.setVisible(true);
lab1.setVisible(true);
lab2.setVisible(true);
lab3.setVisible(true);
lab4.setVisible(true);
f1.setVisible(true);
f2.setVisible(true);
f3.setVisible(true);
f4.setVisible(true);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
lab.setVisible(false);
table.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
status.setVisible(false);
status1.setVisible(false);
status2.setVisible(false);
button1.setVisible(false);
BufferedReader reader=null;
String temp=null;
int line=0;
String [][]ff=new String[1000][];
int q=0;
try{
reader=new BufferedReader(new FileReader("information.txt"));
while((temp=reader.readLine())!=null &&line<1000){
ff[line]=temp.split(" ");
if(ff[line].length==5 &&ff[line][3].equals(T)) {
f1.setText(ff[line][0]);
f2.setText(ff[line][1]);
f3.setText(ff[line][2]);
f4.setText(ff[line][3]);
}
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
}});
item6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
he.setVisible(false);
label1.setVisible(true);
lab1.setVisible(true);
lab2.setVisible(true);
lab3.setVisible(true);
lab4.setVisible(true);
f1.setVisible(true);
f2.setVisible(true);
f3.setVisible(true);
f4.setVisible(true);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
lab.setVisible(false);
table.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
status.setVisible(false);
status1.setVisible(false);
status2.setVisible(false);
button1.setVisible(false);
try {
BufferedWriter w=new BufferedWriter(new FileWriter("./ex.txt",true));
String Tel=T;
w.write(Tel);
w.flush();
w.close();
} catch (IOException e1) {
e1.printStackTrace();
}
new xiugai();
}});
butt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
n1.setText("");
n2.setText("");
n3.setText("");
}});
item7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
BufferedWriter w=new BufferedWriter(new FileWriter("./ex.txt",true));
String Tel=T;
w.write(Tel);
w.flush();
w.close();
} catch (IOException e1) {
e1.printStackTrace();
}
new tel();
}});
}
}
运行结果如下:
购票代码如下:
package 购票系统;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import javax.swing.*;
@SuppressWarnings("unused")
public class goumai {
JFrame fm=new JFrame("购买火车票");
JLabel label1=new JLabel("姓名:",JLabel.CENTER);
JLabel label2=new JLabel("身份证号:",JLabel.CENTER);
JLabel label11=new JLabel("电话号码",JLabel.CENTER);
JLabel label21=new JLabel("座位",JLabel.CENTER);
JTextField n1=new JTextField();
JTextField n2=new JTextField();
JTextField n3=new JTextField();
@SuppressWarnings("rawtypes")
JComboBox n4=new JComboBox();
JButton button1=new JButton("确定");
JButton button2=new JButton("取消");
String Name="null";
String T="null";
String []xinxi=new String[8];
@SuppressWarnings({ "unchecked", "rawtypes" })
public goumai() {
BufferedReader reader=null;
String temp=null;
try{
reader=new BufferedReader(new FileReader("./ex.txt"));
while((temp=reader.readLine())!=null){
T=temp;
File f=new File("./ex.txt");
FileWriter fw=new FileWriter(f);
fw.write("");
fw.flush();
fw.close();
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
xinxi=T.split(" ");
fm.setLayout(null);
String[] cs= new String[300];
int li=1;
for(int y=0;y<300;y++) {
cs[y]=li+"A";
y++;
cs[y]=li+"B";
y++;
cs[y]=li+"C";
y++;
cs[y]=li+"D";
y++;
cs[y]=li+"E";
li++;
}
n4=new JComboBox(cs);
label1.setBounds(0,20,80, 40);
n1.setBounds(80,30,150, 30);
label2.setBounds(0,70,80, 40);
n2.setBounds(80,80,150, 30);
label11.setBounds(0,120,80, 40);
n3.setBounds(80,130,150, 30);
label21.setBounds(0,170,80, 40);
n4.setBounds(80,180,150, 30);
button1.setBounds(50,250,80, 30);
button2.setBounds(150,250,80, 30);
fm.add(button1);
fm.add(button2);
fm.add(label1);
fm.add(n1);
fm.add(label2);
fm.add(n2);
fm.add(label11);
fm.add(n3);
fm.add(label21);
fm.add(n4);
fm.setSize(300,400);
fm.setResizable(false);
fm.setLocationRelativeTo(null);
fm.setVisible(true);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println(xinxi[0]);
String rn1=n1.getText();//姓名
String rn2=n2.getText();//身份证
String rn3=n3.getText();//电话号码
// System.out.println(rn1);
String rn4=n4.getSelectedItem().toString();//座位号
String sum=n1.getText()+" "+n2.getText();
String s=xinxi[0]+" "+xinxi[1]+" "+xinxi[2]+" "+xinxi[3]+" "+xinxi[4]+" "+rn4+" "+xinxi[6]+" "+xinxi[7];
if(rn3.length()==11 && rn2.length()==18) {
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./ticket/all.txt",true));
bw.write(s);
bw.newLine();
bw.flush();
bw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./ticket/wei.txt",true));
bw.write(s);
bw.newLine();
bw.flush();
bw.close();
JOptionPane.showMessageDialog(null, "购票成功");
} catch (IOException e1) {
e1.printStackTrace();
}
String ri=xinxi[1];
int a=1;
int b=0;
String []tiqu1=ri.split("-");
int mo=(Integer.parseInt(tiqu1[1]));
int dd=Integer.parseInt(tiqu1[2]);
if(mo==12) {
b=dd-31;
a+=b;
}
if(mo==1) {
a=0;
a+=dd;
}
String [][]ss1=new String[1000][];
int mm=0;
String shijian=xinxi[8];
String shi=xinxi[2];
String zhong=xinxi[3];
String nam="./train/"+a+".txt";
File fil=new File(nam);
BufferedReader reade=null;
String temp=null;
int line=0;
String []words=new String[1000];
try{
reade=new BufferedReader(new FileReader(nam));
while((temp=reade.readLine())!=null){
words[line]=temp;
ss1[line]=words[line].split(" ");
if(ss1[line][1].equals(shi) &&ss1[line][2].equals(zhong) &&ss1[line][3].equals(shijian)) {
String zhi=ss1[line][6];
int zh=Integer.parseInt(zhi);
zh-=1;
ss1[line][6]=Integer.toString(zh);
words[line]="";
for(int h=0;h<ss1[line].length;h++) {
words[line]+=ss1[line][h]+" ";
}
}
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reade!=null){
try{
reade.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(nam));
for(int y=0;y<line;y++) {
bw.write(words[y]);
bw.newLine();
}
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
for(int i=0;i<cs.length;i++) {
if(cs[i].equals(rn4)) {
for(int j=i;j<cs.length-1;j++) {
cs[j]=cs[j+1];
}
cs[cs.length-1]="";
}
}
n4.setModel(new DefaultComboBoxModel(cs));
}else {
JOptionPane.showMessageDialog(null, "输入信息有误,请核实");
}
fm.dispose();
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
fm.dispose();
}
});
}
}
订单中心运行结果如下:
我的界面运行结果如下:
使用时,创建文件以及文件夹如下图所示:
其中information.txt里存储的是昵称,姓名,性别,电话号码以及密码,各个信息之间用空格隔开(下同);user.txt里存储的是账号和密码;ticket/all.txt里存储的是姓名,日期,始点站,终点站,车次,座位号,票价以及出行状态 ;train/1.txt里存储的是列车的信息,包括车次,始点站,终点站,出发时间,到达时间,票价,余票,订购
所有代码展示:
1、denglu.java
package 购票系统;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
@SuppressWarnings("unused")
public class denglu {
JFrame frame=new JFrame("登录");
JLabel label1=new JLabel();
JLabel label2=new JLabel("欢迎来到火车票购票系统",JLabel.CENTER);
JLabel label3=new JLabel("用户登录",JLabel.CENTER);
JLabel label4=new JLabel("用户名",JLabel.CENTER);
JLabel label5=new JLabel("密码",JLabel.CENTER);
JLabel label6=new JLabel("按ESC键退出",JLabel.CENTER);
JTextField n1=new JTextField();
JPasswordField n2=new JPasswordField();
JButton button1=new JButton("登录");
JButton button2=new JButton("注册");
JButton button3=new JButton("管理员登录");
static String tel="null";
public denglu() {
label2.setFont(new Font("",Font.BOLD,40));
label3.setFont(new Font("",Font.BOLD,30));
label4.setFont(new Font("",Font.BOLD,20));
label5.setFont(new Font("",Font.BOLD,20));
label5.setFont(new Font("",Font.CENTER_BASELINE,20));
frame.setLayout(null);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
frame.setUndecorated(true);
frame.setSize(screenSize.width, screenSize.height);
ImageIcon i=new ImageIcon("./1.jpg");
i.getImage();
Image temp=i.getImage().getScaledInstance((int)screenSize.getWidth()/2,(int)screenSize.getHeight()-200, Image.SCALE_DEFAULT);
i=new ImageIcon(temp);
label1.setIcon(i);
label1.setBounds(20, 100, screenSize.width/2, screenSize.height-80);
label2.setBounds(0,50,screenSize.width/2, 100);
label3.setBounds(screenSize.width/2,250,screenSize.width/2, 100);
label4.setBounds(screenSize.width/2,350,screenSize.width/4, 30);
label6.setBounds(screenSize.width-100,0,100, 50);
n1.setBounds(screenSize.width/2+screenSize.width/6,350,300, 30);
label5.setBounds(screenSize.width/2,400,screenSize.width/4, 30);
n2.setBounds(screenSize.width/2+screenSize.width/6,400,300, 30);
button1.setBounds(screenSize.width/2+200,500,100, 30);
button2.setBounds(screenSize.width/2+350,500,100, 30);
button3.setBounds(screenSize.width/2+260,550,150, 30);
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.add(label4);
frame.add(label5);
frame.add(label6);
frame.add(n1);
frame.add(n2);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Toolkit.getDefaultToolkit().addAWTEventListener((AWTEventListener) new AWTEventListener() {
public void eventDispatched(AWTEvent event) {
if (((KeyEvent) event).getID() == KeyEvent.KEY_PRESSED) {
switch(((KeyEvent)event).getKeyCode()) {
case KeyEvent.VK_ESCAPE: System.exit(0); break;
}
}
}
}, AWTEvent.KEY_EVENT_MASK);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
@SuppressWarnings("deprecation")
String sum=n1.getText()+" "+n2.getText();
boolean cot=false;
String s;
try {
@SuppressWarnings("resource")
BufferedReader r=new BufferedReader(new FileReader("./user.txt"));
while((s=r.readLine())!=null) {
if(s.equals(sum)) {
cot=true;
tel=n1.getText();
}
}
if(cot) {
try {
BufferedWriter w=new BufferedWriter(new FileWriter("./ex.txt",true));
String Tel=tel;
w.write(Tel);
w.flush();
w.close();
} catch (IOException e1) {
e1.printStackTrace();
}
new shouye();
frame.dispose();
}else {
JOptionPane.showMessageDialog(null, "用户名或者密码错误,登录失败!");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// String sum=n1.getText()+" "+n2.getText();
@SuppressWarnings("deprecation")
String sum=n1.getText()+" "+n2.getText();
if(sum.equals("111 111")) {
new guanli();
frame.dispose();
}else {
JOptionPane.showMessageDialog(null, "用户名或者密码错误,登录失败!");
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new zhuce();
}
});
}
public String GetTel() {
return tel;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new denglu();
}
}
2、goumai.java
package 购票系统;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import javax.swing.*;
@SuppressWarnings("unused")
public class goumai {
JFrame fm=new JFrame("购买火车票");
JLabel label1=new JLabel("姓名:",JLabel.CENTER);
JLabel label2=new JLabel("身份证号:",JLabel.CENTER);
JLabel label11=new JLabel("电话号码",JLabel.CENTER);
JLabel label21=new JLabel("座位",JLabel.CENTER);
JTextField n1=new JTextField();
JTextField n2=new JTextField();
JTextField n3=new JTextField();
@SuppressWarnings("rawtypes")
JComboBox n4=new JComboBox();
JButton button1=new JButton("确定");
JButton button2=new JButton("取消");
String Name="null";
String T="null";
String []xinxi=new String[8];
@SuppressWarnings({ "unchecked", "rawtypes" })
public goumai() {
BufferedReader reader=null;
String temp=null;
try{
reader=new BufferedReader(new FileReader("./ex.txt"));
while((temp=reader.readLine())!=null){
T=temp;
File f=new File("./ex.txt");
FileWriter fw=new FileWriter(f);
fw.write("");
fw.flush();
fw.close();
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
xinxi=T.split(" ");
fm.setLayout(null);
String[] cs= new String[300];
int li=1;
for(int y=0;y<300;y++) {
cs[y]=li+"A";
y++;
cs[y]=li+"B";
y++;
cs[y]=li+"C";
y++;
cs[y]=li+"D";
y++;
cs[y]=li+"E";
li++;
}
n4=new JComboBox(cs);
label1.setBounds(0,20,80, 40);
n1.setBounds(80,30,150, 30);
label2.setBounds(0,70,80, 40);
n2.setBounds(80,80,150, 30);
label11.setBounds(0,120,80, 40);
n3.setBounds(80,130,150, 30);
label21.setBounds(0,170,80, 40);
n4.setBounds(80,180,150, 30);
button1.setBounds(50,250,80, 30);
button2.setBounds(150,250,80, 30);
fm.add(button1);
fm.add(button2);
fm.add(label1);
fm.add(n1);
fm.add(label2);
fm.add(n2);
fm.add(label11);
fm.add(n3);
fm.add(label21);
fm.add(n4);
fm.setSize(300,400);
fm.setResizable(false);
fm.setLocationRelativeTo(null);
fm.setVisible(true);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// System.out.println(xinxi[0]);
String rn1=n1.getText();//姓名
String rn2=n2.getText();//身份证
String rn3=n3.getText();//电话号码
// System.out.println(rn1);
String rn4=n4.getSelectedItem().toString();//座位号
String sum=n1.getText()+" "+n2.getText();
String s=xinxi[0]+" "+xinxi[1]+" "+xinxi[2]+" "+xinxi[3]+" "+xinxi[4]+" "+rn4+" "+xinxi[6]+" "+xinxi[7];
if(rn3.length()==11 && rn2.length()==18) {
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./ticket/all.txt",true));
bw.write(s);
bw.newLine();
bw.flush();
bw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./ticket/wei.txt",true));
bw.write(s);
bw.newLine();
bw.flush();
bw.close();
JOptionPane.showMessageDialog(null, "购票成功");
} catch (IOException e1) {
e1.printStackTrace();
}
String ri=xinxi[1];
int a=1;
int b=0;
String []tiqu1=ri.split("-");
int mo=(Integer.parseInt(tiqu1[1]));
int dd=Integer.parseInt(tiqu1[2]);
if(mo==12) {
b=dd-31;
a+=b;
}
if(mo==1) {
a=0;
a+=dd;
}
String [][]ss1=new String[1000][];
int mm=0;
String shijian=xinxi[8];
String shi=xinxi[2];
String zhong=xinxi[3];
String nam="./train/"+a+".txt";
File fil=new File(nam);
BufferedReader reade=null;
String temp=null;
int line=0;
String []words=new String[1000];
try{
reade=new BufferedReader(new FileReader(nam));
while((temp=reade.readLine())!=null){
words[line]=temp;
ss1[line]=words[line].split(" ");
if(ss1[line][1].equals(shi) &&ss1[line][2].equals(zhong) &&ss1[line][3].equals(shijian)) {
String zhi=ss1[line][6];
int zh=Integer.parseInt(zhi);
zh-=1;
ss1[line][6]=Integer.toString(zh);
words[line]="";
for(int h=0;h<ss1[line].length;h++) {
words[line]+=ss1[line][h]+" ";
}
}
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reade!=null){
try{
reade.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(nam));
for(int y=0;y<line;y++) {
bw.write(words[y]);
bw.newLine();
}
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
for(int i=0;i<cs.length;i++) {
if(cs[i].equals(rn4)) {
for(int j=i;j<cs.length-1;j++) {
cs[j]=cs[j+1];
}
cs[cs.length-1]="";
}
}
n4.setModel(new DefaultComboBoxModel(cs));
}else {
JOptionPane.showMessageDialog(null, "输入信息有误,请核实");
}
fm.dispose();
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
fm.dispose();
}
});
}
}
3、guanli.java
package 购票系统;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
@SuppressWarnings("unused")
public class guanli {
JFrame fram=new JFrame();
JMenuBar bar=new JMenuBar();
JMenu menu1=new JMenu("火车票管理");
JMenuItem item1=new JMenuItem("增加");
JMenuItem item2=new JMenuItem("修改");
JMenuItem item3=new JMenuItem("删除");
JButton but=new JButton("返回");
String[] columnNames = {"检票口","起点站","终点站","出发时间","到达时间","票价","余票"," "};
Object[][] data = {
{"0","0","0","0","0",0,0,"0"},
};
// JTable table = new JTable(data, columnNames);
@SuppressWarnings("serial")
JTable table = new JTable(data, columnNames){
public boolean isCellEditable(int row, int column) {
return false;
}
};
String name="./train/1.txt";
File file=new File(name);
BufferedReader reader=null;
String temp=null;
int line=0;
String []words=new String[1000];
JLabel label1=new JLabel("日期:",JLabel.CENTER);
JLabel label2=new JLabel("车次:",JLabel.CENTER);
JLabel label3=new JLabel("起点站:",JLabel.CENTER);
JLabel label4=new JLabel("终点站:",JLabel.CENTER);
JLabel label5=new JLabel("出发时间:",JLabel.CENTER);
JLabel label6=new JLabel("到达时间:",JLabel.CENTER);
JLabel label7=new JLabel("票价:",JLabel.CENTER);
JLabel label8=new JLabel("余票:",JLabel.CENTER);
JTextField n1=new JTextField();
JTextField n2=new JTextField();
@SuppressWarnings("rawtypes")
JComboBox n3=new JComboBox();
@SuppressWarnings("rawtypes")
JComboBox n4=new JComboBox();
JTextField n5=new JTextField();
JTextField n6=new JTextField();
JTextField n7=new JTextField();
JTextField n8=new JTextField();
JButton button1=new JButton("添加");
JButton button2=new JButton("取消");
JButton button3=new JButton("删除");
@SuppressWarnings("rawtypes")
JComboBox status=new JComboBox();
Calendar c = Calendar.getInstance();
@SuppressWarnings({ "unchecked", "rawtypes" })
public guanli() {
try{
reader=new BufferedReader(new FileReader(file));
while((temp=reader.readLine())!=null){
words[line]=temp;
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
String ss[][]=new String[1000][];
for(int i=0;i<line;i++) {
ss[i]=words[i].split(" ");
}
DefaultTableCellRenderer dc=new DefaultTableCellRenderer();
dc.setHorizontalAlignment(JLabel.CENTER);
table = new JTable(ss, columnNames);
table.setDefaultRenderer(Object.class, dc);
table.getTableHeader().setFont(new Font("宋体",Font.BOLD,14));
table.setFont(new Font("宋体",Font.PLAIN,14));
table.setRowHeight(20);
JTableHeader he=table.getTableHeader();
he.setBounds(0,0,screenSize.width-400,30);
int d=c.get(Calendar.DAY_OF_MONTH);
int m=(c.get(Calendar.MONTH) + 1);
int y=c.get(Calendar.YEAR);
int [] month1= {31,29,31,30,31,30,31,31,30,31,30,31};
int [] month2= {31,28,31,30,31,30,31,31,30,31,30,31};
int [] month=new int[12];
if(y%400==0||(y%4==0&&y%100!=0)) {
month=month1;
}
else month=month2;
String [] cs= new String[20];
for(int ii=0;ii<20;ii++) {
cs[ii]=y+"-"+m+"-"+d;
d+=1;
if(d>month[m-1]) {
d=1;m++;
}
if(m>12) {
m=1;y++;
}
}
String [] cs1= {"北京","上海","杭州","广州","南京","成都","西安","郑州","重庆","合肥","汉口","武汉","长沙","武昌","太原","苏州","厦门","南昌","沈阳","天津","深圳"};
status=new JComboBox(cs);
n3=new JComboBox(cs1);
n4=new JComboBox(cs1);
table.setBounds(0,30,screenSize.width-400,screenSize.height-30);
label1.setBounds(screenSize.width-350,50,50, 40);
status.setBounds(screenSize.width-250,60,150, 30);
n1.setBounds(screenSize.width-250,110,150, 30);
label2.setBounds(screenSize.width-350,100,50, 40);
n2.setBounds(screenSize.width-250,110,150, 30);
label3.setBounds(screenSize.width-350,150,50, 40);
n3.setBounds(screenSize.width-250,160,150, 30);
label4.setBounds(screenSize.width-350,200,50, 40);
n4.setBounds(screenSize.width-250,210,150, 30);
label5.setBounds(screenSize.width-350,250,70, 40);
n5.setBounds(screenSize.width-250,260,150, 30);
label6.setBounds(screenSize.width-350,300,70, 40);
n6.setBounds(screenSize.width-250,310,150, 30);
label7.setBounds(screenSize.width-350,350,50, 40);
n7.setBounds(screenSize.width-250,360,150, 30);
label8.setBounds(screenSize.width-350,400,50, 40);
n8.setBounds(screenSize.width-250,410,150, 30);
button1.setBounds(screenSize.width-300,500,80, 30);
button3.setBounds(screenSize.width-300,500,80, 30);
button2.setBounds(screenSize.width-200,500,80, 30);
fram.add(he);
label1.setVisible(false);
status.setVisible(false);
label2.setVisible(false);
n2.setVisible(false);
label3.setVisible(false);
n3.setVisible(false);
label4.setVisible(false);
n4.setVisible(false);
label5.setVisible(false);
n5.setVisible(false);
label6.setVisible(false);
n6.setVisible(false);
label7.setVisible(false);
n7.setVisible(false);
label8.setVisible(false);
n8.setVisible(false);
button1.setVisible(false);
button2.setVisible(false);
n1.setVisible(false);
button3.setVisible(false);
fram.add(table);
fram.add(label1);
fram.add(status);
fram.add(label2);
fram.add(n2);
fram.add(label3);
fram.add(n3);
fram.add(label4);
fram.add(n4);
fram.add(label5);
fram.add(n5);
fram.add(label6);
fram.add(n6);
fram.add(label7);
fram.add(n7);
fram.add(label8);
fram.add(n8);
fram.add(button1);
fram.add(button2);
fram.add(n1);
fram.add(button3);
fram.setLayout(null);
bar.add(menu1);
menu1.add(item1);
menu1.addSeparator();
menu1.add(item2);
menu1.addSeparator();
menu1.add(item3);
but.setBounds(screenSize.width-100, 0, 100,30);
fram.add(but);
fram.setUndecorated(true);
fram.setSize(screenSize);
fram.setJMenuBar(bar);
fram.setResizable(false);
fram.setLocationRelativeTo(null);
fram.setVisible(true);
but.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new denglu();
}
});
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(true);
status.setVisible(true);
label2.setVisible(true);
n2.setVisible(true);
label3.setVisible(true);
n3.setVisible(true);
label4.setVisible(true);
n4.setVisible(true);
label5.setVisible(true);
n5.setVisible(true);
label6.setVisible(true);
n6.setVisible(true);
label7.setVisible(true);
n7.setVisible(true);
label8.setVisible(true);
n8.setVisible(true);
button1.setVisible(true);
button2.setVisible(true);
n1.setVisible(false);
button3.setVisible(false);
}});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str1=status.getSelectedItem().toString();
String str2=n2.getText();
String str3=n3.getSelectedItem().toString();
String str4=n4.getSelectedItem().toString();
String str5=n5.getText();
String str6=n6.getText();
String str7=n7.getText();
String str8=n8.getText();
String[]data1={str2,str3,str4,str5,str6,str7,str8,"订购"};
if(ss.length<=1000) {
ss[line]=data1;
line++;
}
TableModel tableModel=new DefaultTableModel(ss,columnNames);
table.setModel(tableModel);
int a=1;
int b=0;
int mo=(c.get(Calendar.MONTH) + 1);
String []tiqu1=str1.split("-");
String d=tiqu1[2];
int dd=Integer.parseInt(d);
if(mo==12) {
b=dd-31;
a+=b;
}
if(mo==1) {
a=0;
a+=dd;
}
String nam="./train/"+a+".txt";
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(nam,true));
String te=str2+" "+str3+" "+str4+" "+str5+" "+str6+" "+str7+" "+str8+" "+"订购";
bw.write(te);
bw.newLine();
bw.flush();
bw.close();
JOptionPane.showMessageDialog(null, "添加成功!");
} catch (IOException e1) {
e1.printStackTrace();
}
}});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(false);
status.setVisible(false);
label2.setVisible(false);
n2.setVisible(false);
label3.setVisible(false);
n3.setVisible(false);
label4.setVisible(false);
n4.setVisible(false);
label5.setVisible(false);
n5.setVisible(false);
label6.setVisible(false);
n6.setVisible(false);
label7.setVisible(false);
n7.setVisible(false);
label8.setVisible(false);
n8.setVisible(false);
button1.setVisible(false);
button2.setVisible(false);
n1.setVisible(false);
button3.setVisible(false);
}});
item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(false);
status.setVisible(false);
label2.setVisible(false);
n2.setVisible(false);
label3.setVisible(false);
n3.setVisible(false);
label4.setVisible(false);
n4.setVisible(false);
label5.setVisible(false);
n5.setVisible(false);
label6.setVisible(false);
n6.setVisible(false);
label7.setVisible(false);
n7.setVisible(false);
label8.setVisible(false);
n8.setVisible(false);
button1.setVisible(false);
button2.setVisible(false);
n1.setVisible(false);
button3.setVisible(false);
}});
item3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(true);
status.setVisible(true);
label2.setVisible(true);
n2.setVisible(true);
label3.setVisible(true);
n3.setVisible(true);
label4.setVisible(true);
n4.setVisible(true);
label5.setVisible(true);
n5.setVisible(true);
label6.setVisible(true);
n6.setVisible(true);
label7.setVisible(true);
n7.setVisible(true);
label8.setVisible(true);
n8.setVisible(true);
button1.setVisible(false);
button2.setVisible(true);
n1.setVisible(false);
button3.setVisible(true);
}});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str1=status.getSelectedItem().toString();
String str2=n2.getText();
String str3=n3.getSelectedItem().toString();
String str4=n4.getSelectedItem().toString();
String str5=n5.getText();
String str6=n6.getText();
String str7=n7.getText();
String str8=n8.getText();
String[]data1={str2,str3,str4,str5,str6,str7,str8,"订购"};
boolean cat=false;
String q=str2+" "+str3+" "+str4+" "+str5+" "+str6+" "+str7+" "+str8+" "+"订购";
// System.out.println(q);
int a=1;
int b=0;
int mo=(c.get(Calendar.MONTH) + 1);
String []tiqu1=str1.split("-");
String d=tiqu1[2];
int dd=Integer.parseInt(d);
if(mo==12) {
b=dd-31;
a+=b;
}
if(mo==1) {
a=0;
a+=dd;
}
String nam="./train/"+a+".txt";
try{
reader=new BufferedReader(new FileReader(nam));
while((temp=reader.readLine())!=null && line<1000){
words[line]=temp;
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
for(int i=0;i<words.length;i++) {
// System.out.println(words[i]);
if(words[i].equals(q)) {
cat=true;
}
}
if(cat) {
for(int j=0;j<words.length;j++) {
if(words[j].equals(q)) {
for(int w=0;w<words.length-1;w++) {
words[w]=words[w+1];
}
words[words.length-1]="";
}
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(nam));
for(int h=0;h<ss.length;h++) {
bw.write(words[h]);
bw.newLine();
}
bw.flush();
bw.close();
JOptionPane.showMessageDialog(null, "删除成功!");
} catch (IOException e1) {
e1.printStackTrace();
}
}
else {
JOptionPane.showMessageDialog(null, "航班不存在");
}
}});
}
}
4、shouye.java
package 购票系统;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
@SuppressWarnings("unused")
public class shouye {
JFrame fm=new JFrame();
JLabel label1=new JLabel();
JLabel label6=new JLabel("按ESC键退出",JLabel.CENTER);
JMenuBar bar=new JMenuBar();
JMenu menu1=new JMenu("购票中心");
JMenu menu2=new JMenu("订单中心");
JMenu menu3=new JMenu("我的");
JLabel lab=new JLabel("个人订单",JLabel.CENTER);
JMenuItem item1=new JMenuItem("全部订单");
JMenuItem item2=new JMenuItem("未出行订单");
JMenuItem item3=new JMenuItem("已出行订单");
JMenuItem item4=new JMenuItem("购买火车票");
JMenuItem item5=new JMenuItem("查看个人信息");
JMenuItem item6=new JMenuItem("修改个人信息");
JMenuItem item7=new JMenuItem("解绑手机号 ");
JMenuItem item8=new JMenuItem("退出登录");
JRadioButton rb1=new JRadioButton("男");
JRadioButton rb2=new JRadioButton("女");
ButtonGroup group=new ButtonGroup();
Font font=new Font("宋体",Font.BOLD,20);
JLabel label2=new JLabel("日期");
@SuppressWarnings("rawtypes")
JComboBox status=new JComboBox();
JLabel label3=new JLabel("始点站");
@SuppressWarnings("rawtypes")
JComboBox status1=new JComboBox();
JLabel label4=new JLabel("终点站");
@SuppressWarnings("rawtypes")
JComboBox status2=new JComboBox();
JButton button1=new JButton("查询");
JLabel lab1=new JLabel("昵称:");
JLabel lab2=new JLabel("真实姓名:");
JLabel lab3=new JLabel("性别:");
JLabel lab4=new JLabel("电话号码:");
JLabel f1=new JLabel();
JLabel f2=new JLabel();
JLabel f3=new JLabel();
JLabel f4=new JLabel();
JTextField n1=new JTextField();
JTextField n2=new JTextField();
JTextField n3=new JTextField();
JButton butt1=new JButton("确认");
JButton butt2=new JButton("清空");
String[] columnNames = {"检票口","起点站","终点站","出发时间","到达时间","票价","余票"," "};
Object[][] data = {
{"0","0","0","0","0",0,0,"0"},
};
Calendar c = Calendar.getInstance();
String name="./train/1.txt";
File file=new File(name);
BufferedReader reader=null;
String temp=null;
int line=0;
String []words=new String[1000];
//JTable table = new JTable(data, columnNames);
@SuppressWarnings("serial")
JTable table = new JTable(data, columnNames){
public boolean isCellEditable(int row, int column) {
return false;
}
};
JScrollPane scrollPane = new JScrollPane(table);
String Name="null";
String T="null";
@SuppressWarnings({ "unchecked", "rawtypes" })
public shouye() {
lab1.setFont(new Font("",Font.BOLD,20));
lab2.setFont(new Font("",Font.BOLD,20));
lab3.setFont(new Font("",Font.BOLD,20));
lab4.setFont(new Font("",Font.BOLD,20));
f1.setFont(new Font("",Font.BOLD,20));
f2.setFont(new Font("",Font.BOLD,20));
f3.setFont(new Font("",Font.BOLD,20));
f4.setFont(new Font("",Font.BOLD,20));
try{
reader=new BufferedReader(new FileReader(file));
while((temp=reader.readLine())!=null){
words[line]=temp;
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
String ss[][]=new String[line][];
for(int i=0;i<line;i++) {
ss[i]=words[i].split(" ");
}
try{
reader=new BufferedReader(new FileReader("./ex.txt"));
while((temp=reader.readLine())!=null){
T=temp;
File f=new File("./ex.txt");
FileWriter fw=new FileWriter(f);
fw.write("");
fw.flush();
fw.close();
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
int xx=0;
String [][]NN=new String[1000][];
try{
reader=new BufferedReader(new FileReader("./information.txt"));
while((temp=reader.readLine())!=null &&xx<1000){
NN[xx]=temp.split(" ");
if(NN[xx].length==5 && NN[xx][3].equals(T)) {
Name=NN[xx][1];
}
xx++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
group.add(rb1);
group.add(rb2);
table = new JTable(ss, columnNames);
int [] month1= {31,29,31,30,31,30,31,31,30,31,30,31};
int [] month2= {31,28,31,30,31,30,31,31,30,31,30,31};
int d=c.get(Calendar.DAY_OF_MONTH);
int m=(c.get(Calendar.MONTH) + 1);
int y=c.get(Calendar.YEAR);
lab.setFont(new Font("",Font.BOLD,20));
fm.setLayout(null);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
fm.setUndecorated(true);
fm.setSize(screenSize);
lab.setBounds(0, 0,screenSize.width, 30);
ImageIcon i=new ImageIcon("./2.jpg");
@SuppressWarnings("static-access")
Image temp=i.getImage().getScaledInstance((int)screenSize.getWidth(),(int)screenSize.getHeight(), i.getImage().SCALE_DEFAULT);
i=new ImageIcon(temp);
label1.setIcon(i);
label1.setBounds(0, 0, screenSize.width, screenSize.height);
label1.setVisible(false);
fm.getContentPane().add(scrollPane,BorderLayout.CENTER);
label6.setBounds(screenSize.width-100,0,100, 30);
int [] month=new int[12];
if(y%400==0||(y%4==0&&y%100!=0)) {
month=month1;
}
else month=month2;
String [] cs= new String[20];
String [] cs1= {"北京","上海","杭州","广州","南京","成都","西安","郑州","重庆","合肥","汉口","武汉","长沙","武昌","太原","苏州","厦门","南昌","沈阳","天津","深圳"};
for(int ii=0;ii<20;ii++) {
cs[ii]=y+"-"+m+"-"+d;
d+=1;
if(d>month[m-1]) {
d=1;m++;
}
if(m>12) {
m=1;y++;
}
}
menu1.setFont(font);
menu2.setFont(font);
menu3.setFont(font);
status=new JComboBox(cs);
status1=new JComboBox(cs1);
status2=new JComboBox(cs1);
label2.setBounds(50, 0, 50, 30);
status.setBounds(100,0, 130, 30);
label3.setBounds(300,0, 50, 30);
status1.setBounds(350,0,100, 30);
label4.setBounds(500,0, 50, 30);
status2.setBounds(550,0, 100, 30);
button1.setBounds(700,0, 70, 30);
lab1.setBounds(30,30,50, 30);
lab2.setBounds(30,70,100, 30);
lab3.setBounds(30,110,50, 30);
lab4.setBounds(30,150,100, 30);
f1.setBounds(100,30,400, 30);
f2.setBounds(150,70,200, 30);
f3.setBounds(100,110,200, 30);
f4.setBounds(150,150,400, 30);
n1.setBounds(150,30,200, 30);
n2.setBounds(150,70,200, 30);
n3.setBounds(150,110,200, 30);
rb1.setBounds(150, 110, 100, 30);
rb2.setBounds(250, 110, 100, 30);
butt1.setBounds(100,200,60, 30);
butt2.setBounds(160,200,60, 30);
menu1.add(item4);
menu2.add(item1);
menu2.addSeparator();
menu2.add(item2);
menu2.addSeparator();
menu2.add(item3);
menu3.add(item5);
menu3.addSeparator();
menu3.add(item6);
menu3.addSeparator();
menu3.add(item7);
menu3.addSeparator();
menu3.add(item8);
bar.add(menu1);
bar.add(menu2);
bar.add(menu3);
fm.add(label6);
fm.add(status);
fm.add(label2);
fm.add(label3);
fm.add(label4);
fm.add(lab);
fm.add(lab1);
fm.add(lab2);
fm.add(lab3);
fm.add(lab4);
fm.add(f1);
fm.add(f2);
fm.add(f3);
fm.add(f4);
fm.add(n1);
fm.add(n2);
fm.add(n3);
fm.add(butt1);
fm.add(butt2);
fm.add(rb1);
fm.add(rb2);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
lab4.setVisible(false);
f1.setVisible(false);
f2.setVisible(false);
f3.setVisible(false);
f4.setVisible(false);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
lab.setVisible(false);
fm.add(status1);
fm.add(status2);
fm.add(button1);
fm.add(label1);
DefaultTableCellRenderer dc=new DefaultTableCellRenderer();
dc.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class, dc);
table.getTableHeader().setFont(new Font("宋体",Font.BOLD,14));
table.setFont(new Font("宋体",Font.PLAIN,14));
table.setRowHeight(20);
JTableHeader he=table.getTableHeader();
he.setBounds(0,50,screenSize.width,30);
table.setBounds(0,80,screenSize.width,screenSize.height-200);
fm.add(he);
fm.add(table);
fm.setJMenuBar(bar);
fm.setResizable(false);
fm.setLocationRelativeTo(null);
fm.setVisible(true);
table.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int r= table.getSelectedRow();
int c= table.getSelectedColumn();
String ri1=status.getSelectedItem().toString();
String shi1=status1.getSelectedItem().toString();
String zhong1=status2.getSelectedItem().toString();
if(c==7) {
String info=Name+" "+ri1+" "+shi1+" "+zhong1+" "+table.getValueAt(r,0).toString()+" "+
"null"+" "+ table.getValueAt(r,5).toString()+" "+"未出行"+" "+table.getValueAt(r,3).toString();
try {
BufferedWriter w=new BufferedWriter(new FileWriter("./ex.txt",true));
w.write(info);
w.flush();
w.close();
} catch (IOException e1) {
e1.printStackTrace();
}
new goumai();
// javax.swing.JOptionPane.showMessageDialog(null,info);
}
}
@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
}});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String ri=status.getSelectedItem().toString();
String shi=status1.getSelectedItem().toString();
String zhong=status2.getSelectedItem().toString();
int a=1;
int b=0;
int mo=(c.get(Calendar.MONTH) + 1);
String []tiqu1=ri.split("-");
String d=tiqu1[2];
int dd=Integer.parseInt(d);
if(mo==12) {
b=dd-31;
a+=b;
}
if(mo==1) {
a=0;
a+=dd;
}
String ss1[][]=new String[1000][];
String ssss[][]=new String[1000][];
int mm=0;
String nam="./train/"+a+".txt";
File fil=new File(nam);
BufferedReader reade=null;
String temp=null;
int line=0;
String []words=new String[1000];
try{
reade=new BufferedReader(new FileReader(fil));
while((temp=reade.readLine())!=null &&line<1000){
words[line]=temp;
ss1[line]=words[line].split(" ");
if(ss1[line][1].equals(shi) &&ss1[line][2].equals(zhong)) {
ssss[mm]=ss1[line];
mm++;
}
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
TableModel tableModel=new DefaultTableModel(ssss,columnNames);
table.setModel(tableModel);
}
});
item4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(false);
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
lab4.setVisible(false);
f1.setVisible(false);
f2.setVisible(false);
f3.setVisible(false);
f4.setVisible(false);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
lab.setVisible(false);
he.setVisible(true);
table.setVisible(true);
label2.setVisible(true);
label3.setVisible(true);
label4.setVisible(true);
status.setVisible(true);
status1.setVisible(true);
status2.setVisible(true);
button1.setVisible(true);
TableModel tableModel=new DefaultTableModel(ss,columnNames);
table.setModel(tableModel);
table.setVisible(true);
}});
item8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new denglu();
}});
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
label1.setVisible(false);
lab.setVisible(true);
he.setVisible(true);
table.setVisible(true);
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
lab4.setVisible(false);
f1.setVisible(false);
f2.setVisible(false);
f3.setVisible(false);
f4.setVisible(false);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
status.setVisible(false);
status1.setVisible(false);
status2.setVisible(false);
button1.setVisible(false);
String[] columnName = {"姓名","出行日期","起点站","终点站","车次","座位号","票价","出行状态"};
File file=new File("./ticket/all.txt");
BufferedReader reader=null;
String temp=null;
int line=0;
String [][]ss1=new String[1000][];
String [][]ff=new String[1000][];
int q=0;
try{
reader=new BufferedReader(new FileReader(file));
while((temp=reader.readLine())!=null){
ff[line]=temp.split(" ");
if(ff[line][0].equals(Name)) {
ss1[q]=ff[line];
q++;
}
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
TableModel tableModel=new DefaultTableModel(ss1,columnName);
table.setModel(tableModel);
}});
item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
lab.setVisible(true);
label1.setVisible(false);
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
lab4.setVisible(false);
f1.setVisible(false);
f2.setVisible(false);
f3.setVisible(false);
f4.setVisible(false);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
status.setVisible(false);
status1.setVisible(false);
status2.setVisible(false);
button1.setVisible(false);
he.setVisible(true);
table.setVisible(true);
String[] columnName = {"姓名","出行日期","起点站","终点站","车次","座位号","票价","出行状态"};
File file=new File("./ticket/wei.txt");
BufferedReader reader=null;
String temp=null;
int line=0;
String [][]ss1=new String[1000][];
String [][]ff=new String[1000][];
int q=0;
try{
reader=new BufferedReader(new FileReader(file));
while((temp=reader.readLine())!=null){
ff[line]=temp.split(" ");
if(ff[line][0].equals(Name)) {
ss1[q]=ff[line];
q++;
}
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
TableModel tableModel=new DefaultTableModel(ss1,columnName);
table.setModel(tableModel);
}});
item3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
lab.setVisible(true);
lab1.setVisible(false);
lab2.setVisible(false);
lab3.setVisible(false);
lab4.setVisible(false);
f1.setVisible(false);
f2.setVisible(false);
f3.setVisible(false);
f4.setVisible(false);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
status.setVisible(false);
status1.setVisible(false);
status2.setVisible(false);
button1.setVisible(false);
he.setVisible(true);
table.setVisible(true);
label1.setVisible(false);
String[] columnName = {"姓名","出行日期","起点站","终点站","车次","座位号","票价","出行状态"};
File file=new File("./ticket/yi.txt");
BufferedReader reader=null;
String tem=null;
int lin=0;
String [][]ss1=new String[1000][];
String [][]ff=new String[1000][];
int q=0;
try{
reader=new BufferedReader(new FileReader(file));
while((tem=reader.readLine())!=null){
ff[lin]=tem.split(" ");
if(ff[lin][0].equals(Name)) {
ss1[q]=ff[lin];
q++;
}
lin++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
TableModel tableModel=new DefaultTableModel(ss1,columnName);
table.setModel(tableModel);
}});
item5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
he.setVisible(false);
label1.setVisible(true);
lab1.setVisible(true);
lab2.setVisible(true);
lab3.setVisible(true);
lab4.setVisible(true);
f1.setVisible(true);
f2.setVisible(true);
f3.setVisible(true);
f4.setVisible(true);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
lab.setVisible(false);
table.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
status.setVisible(false);
status1.setVisible(false);
status2.setVisible(false);
button1.setVisible(false);
BufferedReader reader=null;
String temp=null;
int line=0;
String [][]ff=new String[1000][];
int q=0;
try{
reader=new BufferedReader(new FileReader("information.txt"));
while((temp=reader.readLine())!=null &&line<1000){
ff[line]=temp.split(" ");
if(ff[line].length==5 &&ff[line][3].equals(T)) {
f1.setText(ff[line][0]);
f2.setText(ff[line][1]);
f3.setText(ff[line][2]);
f4.setText(ff[line][3]);
}
line++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
}});
item6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
he.setVisible(false);
label1.setVisible(true);
lab1.setVisible(true);
lab2.setVisible(true);
lab3.setVisible(true);
lab4.setVisible(true);
f1.setVisible(true);
f2.setVisible(true);
f3.setVisible(true);
f4.setVisible(true);
n1.setVisible(false);
n2.setVisible(false);
n3.setVisible(false);
rb1.setVisible(false);
rb2.setVisible(false);
butt1.setVisible(false);
butt2.setVisible(false);
lab.setVisible(false);
table.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
status.setVisible(false);
status1.setVisible(false);
status2.setVisible(false);
button1.setVisible(false);
try {
BufferedWriter w=new BufferedWriter(new FileWriter("./ex.txt",true));
String Tel=T;
w.write(Tel);
w.flush();
w.close();
} catch (IOException e1) {
e1.printStackTrace();
}
new xiugai();
}});
butt2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
n1.setText("");
n2.setText("");
n3.setText("");
}});
item7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
BufferedWriter w=new BufferedWriter(new FileWriter("./ex.txt",true));
String Tel=T;
w.write(Tel);
w.flush();
w.close();
} catch (IOException e1) {
e1.printStackTrace();
}
new tel();
}});
}
}
5、tel.java
package 购票系统;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
@SuppressWarnings("unused")
public class tel {
JFrame fm=new JFrame("解绑手机号");
JLabel label1=new JLabel("原手机号",JLabel.CENTER);
JLabel label2=new JLabel("原密码",JLabel.CENTER);
JLabel label11=new JLabel("新手机号",JLabel.CENTER);
JLabel label21=new JLabel("新密码",JLabel.CENTER);
JTextField n1=new JTextField();
JTextField n2=new JTextField();
JTextField n3=new JTextField();
JTextField n4=new JTextField();
JButton button1=new JButton("确定");
JButton button2=new JButton("取消");
String Name="null";
String T="null";
public tel() {
BufferedReader reader=null;
String temp=null;
try{
reader=new BufferedReader(new FileReader("./ex.txt"));
while((temp=reader.readLine())!=null){
T=temp;
File f=new File("./ex.txt");
FileWriter fw=new FileWriter(f);
fw.write("");
fw.flush();
fw.close();
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
fm.setLayout(null);
label1.setBounds(0,20,50, 40);
n1.setBounds(80,30,150, 30);
label2.setBounds(0,70,50, 40);
n2.setBounds(80,80,150, 30);
label11.setBounds(0,120,50, 40);
n3.setBounds(80,130,150, 30);
label21.setBounds(0,170,50, 40);
n4.setBounds(80,180,150, 30);
button1.setBounds(50,250,80, 30);
button2.setBounds(150,250,80, 30);
fm.add(button1);
fm.add(button2);
fm.add(label1);
fm.add(n1);
fm.add(label2);
fm.add(n2);
fm.add(label11);
fm.add(n3);
fm.add(label21);
fm.add(n4);
fm.setSize(300,400);
fm.setResizable(false);
fm.setLocationRelativeTo(null);
fm.setVisible(true);
button1.addActionListener(new ActionListener() {
@SuppressWarnings("resource")
@Override
public void actionPerformed(ActionEvent e) {
String rn1=n1.getText();
String rn2=n2.getText();
String rn3=n3.getText();
String rn4=n4.getText();
String sum=n1.getText()+" "+n2.getText();
boolean cot=false;
int line=0;
String s;
String sa="null";
String []word=new String[1000];
try {
@SuppressWarnings("resource")
BufferedReader r=new BufferedReader(new FileReader("./user.txt"));
while((s=r.readLine())!=null) {
word[line]=s;
if(word[line].equals(sum)) {
cot=true;
sa= rn3+" "+rn4;
word[line]=sa;
}
line++;
}
if(cot) {
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./user.txt"));
for(int y=0;y<line;y++) {
bw.write(word[y]);
bw.newLine();
}
bw.flush();
bw.close();
JOptionPane.showMessageDialog(null, "修改成功");
} catch (IOException e1) {
e1.printStackTrace();
}
int lin=0;
String []words=new String[1000];
String [][]ff=new String[1000][];
try{
BufferedReader reader=null;
String tem=null;
reader=new BufferedReader(new FileReader("./information.txt"));
while((tem=reader.readLine())!=null &&lin<1000){
words[lin]=tem;
ff[lin]=words[lin].split(" ");
if(ff[lin].length==8 && ff[lin][3].equals(rn1)) {
String tt=ff[lin][0]+" "+ff[lin][1]+" "+ff[lin][2]+" "+rn3+" "+rn4;
words[lin]=tt;
}
lin++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./information.txt"));
for(int y=0;y<lin;y++) {
bw.write(words[y]);
bw.newLine();
}
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}else {
JOptionPane.showMessageDialog(null, "用户名不存在或密码错误,修改失败");
}
} catch (Exception e1) {
e1.printStackTrace();
}
fm.dispose();
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
fm.dispose();
}
});
}
}
6、xiugai.java
package 购票系统;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
@SuppressWarnings("unused")
public class xiugai {
JFrame fm=new JFrame("修改个人信息");
JLabel label1=new JLabel("昵称",JLabel.CENTER);
JLabel label2=new JLabel("姓名",JLabel.CENTER);
JLabel label11=new JLabel("性别",JLabel.CENTER);
JTextField n1=new JTextField();
JTextField n2=new JTextField();
JRadioButton rb1=new JRadioButton("男");
JRadioButton rb2=new JRadioButton("女");
ButtonGroup group=new ButtonGroup();
JButton button1=new JButton("确定");
JButton button2=new JButton("取消");
String Name="null";
String T="null";
public xiugai() {
BufferedReader reader=null;
String temp=null;
try{
reader=new BufferedReader(new FileReader("./ex.txt"));
while((temp=reader.readLine())!=null){
T=temp;
File f=new File("./ex.txt");
FileWriter fw=new FileWriter(f);
fw.write("");
fw.flush();
fw.close();
}
}
catch(Exception e2){
e2.printStackTrace();
}
finally{
if(reader!=null){
try{
reader.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
fm.setLayout(null);
label1.setBounds(0,20,50, 40);
n1.setBounds(80,30,150, 30);
label2.setBounds(0,70,50, 40);
n2.setBounds(80,80,150, 30);
label11.setBounds(0,120,50, 40);
rb1.setBounds(80, 130,75, 30);
rb2.setBounds(155, 130,75, 30);
button1.setBounds(50,250,80, 30);
button2.setBounds(150,250,80, 30);
fm.add(button1);
fm.add(button2);
fm.add(label1);
fm.add(n1);
fm.add(label2);
fm.add(n2);
fm.add(label11);
fm.add(rb1);
fm.add(rb2);
fm.setSize(300,400);
fm.setResizable(false);
fm.setLocationRelativeTo(null);
fm.setVisible(true);
button1.addActionListener(new ActionListener() {
@SuppressWarnings("resource")
@Override
public void actionPerformed(ActionEvent e) {
String rn1=n1.getText();
String rn2=n2.getText();
String rn3=rb1.isSelected()?rb1.getText():rb2.getText();
String sum=n1.getText()+" "+n2.getText();
boolean cot=false;
int line=0;
String s;
String sa="null";
String []word=new String[1000];
String [][] a=new String [1000][];
try {
@SuppressWarnings("resource")
BufferedReader r=new BufferedReader(new FileReader("./information.txt"));
while((s=r.readLine())!=null) {
word[line]=s;
a[line]=s.split(" ");
if(a[line].length==5 && a[line][3].equals(T)) {
cot=true;
Name=a[line][1];
sa= rn1+" "+rn2+" "+rn3+" "+a[line][3]+" "+a[line][4];
word[line]=sa;
}
line++;
}
if(cot) {
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./information.txt"));
for(int y=0;y<line;y++) {
bw.write(word[y]);
bw.newLine();
}
bw.flush();
bw.close();
JOptionPane.showMessageDialog(null, "修改成功");
} catch (IOException e1) {
e1.printStackTrace();
}
int lin=0;
String []words=new String[1000];
String [][]ff=new String[1000][];
try{
BufferedReader reader=null;
String tem=null;
reader=new BufferedReader(new FileReader("./ticket/all.txt"));
while((tem=reader.readLine())!=null &&lin<1000){
words[lin]=tem;
ff[lin]=words[lin].split(" ");
if(ff[lin].length==8 && ff[lin][0].equals(Name)) {
String tt=rn2+" "+ff[lin][1]+" "+ff[lin][2]+" "+ff[lin][3]+" "+ff[lin][4]+" "+ff[lin][5]+" "+ff[lin][6]+" "+ff[lin][7];
words[lin]=tt;
}
lin++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./ticket/all.txt"));
for(int y=0;y<lin;y++) {
bw.write(words[y]);
bw.newLine();
}
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
int l=0;
String []wordsss=new String[1000];
String [][]ffff=new String[1000][];
try{
BufferedReader reader=null;
String tem=null;
reader=new BufferedReader(new FileReader("./ticket/wei.txt"));
while((tem=reader.readLine())!=null &&l<1000){
wordsss[l]=tem;
ffff[l]=wordsss[l].split(" ");
if(ffff[l].length==8 && ffff[l][0].equals(Name)) {
String tt=rn2+" "+ffff[l][1]+" "+ffff[l][2]+" "+ffff[l][3]+" "+ffff[l][4]+" "+ffff[l][5]+" "+ffff[l][6]+" "+ffff[l][7];
wordsss[l]=tt;
}
l++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./ticket/wei.txt"));
for(int y=0;y<l;y++) {
bw.write(wordsss[y]);
bw.newLine();
}
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
int li=0;
String []wordss=new String[1000];
String [][]fff=new String[1000][];
try{
BufferedReader reader=null;
String tem=null;
reader=new BufferedReader(new FileReader("./ticket/yi.txt"));
while((tem=reader.readLine())!=null &&li<1000){
wordss[li]=tem;
fff[li]=wordss[li].split(" ");
if(fff[li].length==8 && fff[li][0].equals(Name)) {
String tt=rn2+" "+fff[li][1]+" "+fff[li][2]+" "+fff[li][3]+" "+fff[li][4]+" "+fff[li][5]+" "+fff[li][6]+" "+fff[li][7];
wordss[li]=tt;
}
li++;
}
}
catch(Exception e2){
e2.printStackTrace();
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./ticket/yi.txt"));
for(int y=0;y<li;y++) {
bw.write(wordss[y]);
bw.newLine();
}
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}else {
JOptionPane.showMessageDialog(null, "用户名或者密码错误,修改失败");
}
} catch (Exception e1) {
e1.printStackTrace();
}
fm.dispose();
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
fm.dispose();
}
});
}
}
7、zhuce.java
package 购票系统;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
@SuppressWarnings("unused")
public class zhuce {
JFrame fm=new JFrame("用户注册");
JLabel label1=new JLabel("用户名",JLabel.CENTER);
JLabel label2=new JLabel("密码",JLabel.CENTER);
JTextField n1=new JTextField();
JTextField n2=new JTextField();
JButton button1=new JButton("确定");
JButton button2=new JButton("返回");
public zhuce() {
fm.setLayout(null);
label1.setBounds(0,20,50, 40);
n1.setBounds(80,30,150, 30);
label2.setBounds(0,70,50, 40);
n2.setBounds(80,80,150, 30);
button1.setBounds(50,170,80, 30);
button2.setBounds(150,170,80, 30);
fm.add(button1);
fm.add(button2);
fm.add(label1);
fm.add(n1);
fm.add(label2);
fm.add(n2);
fm.setSize(300,300);
fm.setResizable(false);
fm.setLocationRelativeTo(null);
fm.setVisible(true);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
BufferedWriter w=new BufferedWriter(new FileWriter("./user.txt",true));
String zhanghao=n1.getText();
String sum=n1.getText()+" "+n2.getText();
@SuppressWarnings("resource")
BufferedReader r=new BufferedReader(new FileReader("./user.txt"));
boolean cot=true;
String s;
while((s=r.readLine())!=null) {
if(sum.equals(s)) {
cot=false;
}
}
if(cot && zhanghao.length()==11) {
w.write(sum);
w.newLine();
w.flush();
w.close();
JOptionPane.showMessageDialog(null, "注册成功!");
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("./information.txt",true));
String te="null null null "+sum;
bw.write(te);
bw.newLine();
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}else {
JOptionPane.showMessageDialog(null, "账号已经存在");
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new denglu();
fm.dispose();
}
});
}
}