java实现火车订票系统(简单GUI界面)

这是一个使用Java实现的简单火车订票系统的GUI界面,包括账户登录和注册功能。用户界面采用FlowLayout布局,包含账户和密码输入框,以及确定和注册按钮。登录时,系统会检查账户是否存在及密码是否正确,登录成功后将切换到新的界面。
摘要由CSDN通过智能技术生成

package exercise;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class MainClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WindowBoxLayout w = new WindowBoxLayout();
        w.setBounds(500, 250, 300, 200);
    }

}

class WindowBoxLayout extends JFrame implements ActionListener{
    //JPanel panel;
    Box boxH;
    Box boxV1;
    Box boxV2;
    
    Font font1=new Font("楷体",Font.PLAIN,20);
    Font font2=new Font("楷体",Font.PLAIN,25);
    JTextField j1;
    JPasswordField j2;
    Button b1,b2;
    
    WindowBoxLayoutListener listener;
    public WindowBoxLayout(){

        setLayout(new java.awt.FlowLayout());
        init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    void  init(){
        setTitle("山东省省内火车订票系统");
        //add(new Label("12306订票系统"));
        boxH = Box.createHorizontalBox();
        boxV1 = Box.createVerticalBox();
        boxV2 = Box.createVerticalBox();
        
        JLabel l1=new JLabel("账户");
        l1.setFont(font1);
        boxV1.add(l1);
        
        JLabel l2=new JLabel("密码");
        l2.setFont(font1);
        boxV1.add(l2);
        
        j1=new JTextField(16);
        j2=new JPasswordField(16);
        boxV2.add(j1);
        boxV2.add(j2);
        
        boxH.add(boxV1);
        boxH.add(Box.createHorizontalStrut(10));
        boxH.add(boxV2);
        add(boxH);
        boxH.add(Box.createHorizontalStrut(20));
        
        b1=new Button("确定");
        b1.setFont(font2);
        listener = new WindowBoxLayoutListener();
        listener.setWindowBoxLayout(this);
        b1.addActionListener(listener);
        add(b1);
        boxH.setBackground(Color.pink);
        
        b2=new Button("注册");
        b2.setFont(font2);
        listener = new WindowBoxLayoutListener();
        listener.setWindowBoxLayout(this);
        b2.addActionListener(listener);
        
        add(b2);
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
    }

}
class window2 extends JFrame{
    Font font;
    Box box;
    JTextField textField1;
    JPasswordField textFiled2;
    window2Listener listener=new window2Listener();
    JLabel label1,label2,label3;
    Button button1,button2;
    public window2() {
        setLayout(new java.awt.FlowLayout());
        init();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void init(){
        font = new Font("楷体",Font.PLAIN,20);
        box = Box.createVerticalBox();
        label1=new JLabel("注册账户名:");
        label1.setFont(font);
        box.add(label1);
        box.add(Box.createVerticalStrut(12));
        
        textField1=new JTextField(16);
        box.add(textField1);
        box.add(Box.createVerticalStrut(12));
        
        label2=new JLabel("密码:");
        label2.setFont(font);
        box.add(label2);
        box.add(Box.createVerticalStrut(12));
        textFiled2 = new JPasswordField(16);
        box.add(textFiled2);
        box.add(new JLabel("(密码必须含有大小写字母,数字以及其他特殊字符,且长度大于8位且不超过16位)"));
        
        listener.setwindow2(this);
        //button.addActionListener(listener);
        box.add(Box.createVerticalStrut(30));
        button1 = new Button("确定");
        button1.setFont(font);
        button1.addActionListener(listener);
        box.add(button1);
        
        box.add(Box.createVerticalStrut(30));
        button2=new Button("返回上层");
        button2.setFont(font);
        button2.addActionListener(listener);
        box.add(button2);
        
        add(box);
    }
    
}
class WindowBoxLayoutListener implements ActionListener{
    WindowBoxLayout w;
    JPanel panel=new JPanel();
    JLabel label;
    public void setWindowBoxLayout(WindowBoxLayout w) {
        this.w=w;
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==w.b1) {
            String s1=w.j1.getText();
            String s2=w.j2.getText();
            User u=new User();
            u.setUserName(s1);
            u.setUserPwd(s2);
            Login l=new Login();
            if(l.login(u)==1) {             //返回1:账号名不存在 返回2:密码错误  返回3:登入成功
                label=new JLabel("账户名不存在");
                panel.add(label);
                w.add(panel);
                w.setVisible(true);
                
            }else if(l.login(u)==2) {
                //密码错误;
                label=new JLabel("密码错误!");
                panel.add(label);
                w.add(panel);
                w.setVisible(true);
            }else {
                //登入成功,切换界面
                w.setVisible(false);
                Window3 w3=new Window3();
                w3.setTitle("              你好,"+w.j1.getText());
                w3.setBounds(500,200,600,500);
                w3.setVisible(true);
                w3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
            }
        }
        else if(e.getSource()==w.b2) {
            w.setVisible(false);
            window2 w2=new window2();
            w2.setBounds(500, 250, 700, 500);
        }
    }
}
class window2Listener implements ActionListener{
    window2 w2;
    boolean b1=false,b2=false;
    JLabel label;
    Font font = new Font("楷体",Font.PLAIN,25);
    public void setwindow2(window2 w) {
        this.w2=w;
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==w2.button1) {
            //账户
            if(b1==false) {
                userNameValidation user1=new userNameValidation();
                user1.setWindow(w2);
                b1=user1.test(w2.textField1.getText());
            }
            //密码
            if(b2==false) {
                userPwdValidation user2=new userPwdValidation();
                user2.setWindow2(w2);
                b2=user2.test(w2.textFiled2.getText());
            }
            if(b1==true&&b2==true) {  /注册成功
                User u=null;
                String s1=w2.textField1.getText();
                String s2=w2.textFiled2.getText();
                LocalDate ld=LocalDate.now();
                u=new User(s1,s2,String.format("%ty-&tm-%td",ld,ld,ld));
                Register r=new Register(u);
                if(r.check(u)) {
                    //    有重名
                    w2.box.add(Box.createVerticalStrut(10));
                    label=new JLabel("有重名");
                    label.setFont(font);
                    w2.box.add(label);
                    w2.setVisible(true);
                }
                else {
                    if(r.register(u))
                       //                 注册成功!
                        w2.box.add(Box.createVerticalStrut(10));
                    label=new JLabel("注册成功!");
                    label.setFont(font);
                    w2.box.add(label);
                    w2.setVisible(true);
              
                }
                
            }
        }
        else if(e.getSource()==w2.button2) {
            w2.setVisible(false);//返回上层
            WindowBoxLayout w1 = new WindowBoxLayout();
            w1.setBounds(500, 250, 300, 200);
        }
    }
}


abstract class Validation{
    String str;
    public Pattern pattern;
    Validation(String s){
        pattern =Pattern.compile(s);
    }
    boolean test(String s) {
     
    return false;
    }
}
class userNameValidation extends  Validation{
    Font font=new Font("黑体",Font.BOLD,15);
    Label label;
    window2 w;
    public void setWindow(window2 w) {
 

  • 2
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值