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

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
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单Java GUI界面扑克牌游戏开火车的代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PokerGame extends JFrame { private JPanel playerPanel, centerPanel, buttonPanel; private JLabel[] playerLabels; private JButton startButton, addButton, passButton; private JTextArea infoArea; private JScrollPane infoScroll; private Deck deck; private Hand[] hands; private Hand train; private int currentPlayer; private boolean isStarted, isEnded; public PokerGame() { super("Poker Game"); deck = new Deck(); hands = new Hand[4]; for (int i = 0; i < 4; i++) { hands[i] = new Hand(); } train = new Hand(); playerPanel = new JPanel(new GridLayout(1, 4)); playerLabels = new JLabel[4]; for (int i = 0; i < 4; i++) { playerLabels[i] = new JLabel("Player " + (i + 1)); playerPanel.add(playerLabels[i]); } centerPanel = new JPanel(new GridLayout(2, 1)); centerPanel.add(new JLabel("Train:")); centerPanel.add(train); startButton = new JButton("Start"); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { startGame(); } }); addButton = new JButton("Add"); addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addCard(); } }); addButton.setEnabled(false); passButton = new JButton("Pass"); passButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pass(); } }); passButton.setEnabled(false); buttonPanel = new JPanel(); buttonPanel.add(startButton); buttonPanel.add(addButton); buttonPanel.add(passButton); infoArea = new JTextArea(10, 30); infoArea.setEditable(false); infoScroll = new JScrollPane(infoArea); Container c = getContentPane(); c.add(playerPanel, BorderLayout.NORTH); c.add(centerPanel, BorderLayout.CENTER); c.add(buttonPanel, BorderLayout.SOUTH); c.add(infoScroll, BorderLayout.EAST); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(600, 400); setVisible(true); } private void startGame() { if (!isStarted) { deck.shuffle(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 10; j++) { hands[i].addCard(deck.dealCard()); } } currentPlayer = 0; isStarted = true; addButton.setEnabled(true); passButton.setEnabled(true); startButton.setEnabled(false); updateInfo("Game started.\n"); updateHands(); updateTrain(); updatePlayer(); } } private void addCard() { if (!isEnded) { Card card = hands[currentPlayer].playCard(); if (card != null) { train.addCard(card); updateInfo("Player " + (currentPlayer + 1) + " added " + card + " to the train.\n"); updateHands(); updateTrain(); if (hands[currentPlayer].getCardCount() == 0) { isEnded = true; updateInfo("Player " + (currentPlayer + 1) + " wins!\n"); addButton.setEnabled(false); passButton.setEnabled(false); } else { currentPlayer = (currentPlayer + 1) % 4; updatePlayer(); } } } } private void pass() { if (!isEnded) { updateInfo("Player " + (currentPlayer + 1) + " passed.\n"); currentPlayer = (currentPlayer + 1) % 4; updatePlayer(); } } private void updateHands() { for (int i = 0; i < 4; i++) { playerLabels[i].setText("Player " + (i + 1) + " (" + hands[i].getCardCount() + ")"); } } private void updateTrain() { centerPanel.remove(train); centerPanel.add(train = new Hand()); centerPanel.revalidate(); centerPanel.repaint(); } private void updatePlayer() { updateInfo("Player " + (currentPlayer + 1) + "'s turn.\n"); } private void updateInfo(String message) { infoArea.append(message); infoArea.setCaretPosition(infoArea.getDocument().getLength()); } public static void main(String[] args) { new PokerGame(); } } class Deck { private Card[] cards; private int topCard; public Deck() { cards = new Card[52]; topCard = 0; for (int i = 0; i < 52; i++) { cards[i] = new Card(i); } } public void shuffle() { for (int i = 51; i > 0; i--) { int j = (int)(Math.random() * (i + 1)); Card temp = cards[i]; cards[i] = cards[j]; cards[j] = temp; } topCard = 0; } public Card dealCard() { if (topCard < 52) { return cards[topCard++]; } else { return null; } } } class Card { private int rank, suit; public Card(int index) { rank = index % 13; suit = index / 13; } public int getRank() { return rank; } public int getSuit() { return suit; } public String toString() { String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; String[] suits = {"♠", "♥", "♦", "♣"}; return ranks[rank] + suits[suit]; } } class Hand extends JPanel { private Card[] cards; private int cardCount; public Hand() { cards = new Card[10]; cardCount = 0; } public int getCardCount() { return cardCount; } public Card getCard(int index) { if (index >= 0 && index < cardCount) { return cards[index]; } else { return null; } } public void addCard(Card card) { if (cardCount < 10) { cards[cardCount++] = card; add(new JLabel(card.toString())); } } public Card playCard() { if (cardCount > 0) { Card card = cards[0]; for (int i = 1; i < cardCount; i++) { cards[i - 1] = cards[i]; } cards[--cardCount] = null; remove(0); revalidate(); repaint(); return card; } else { return null; } } } ``` 这个程序使用了Java的AWT和Swing库来实现GUI界面,同时使用了面向对象的编程思想来设计游戏逻辑。在游戏开始前,程序会生成一副扑克牌,并将其洗牌;然后程序会给四个玩家发牌,每个玩家有10张牌;接着玩家轮流出牌,每出一张牌就会添加到火车上,如果一名玩家没有牌了,他就赢了游戏。程序还提供了添加牌和放弃出牌的功能,同时会在右侧的信息区域显示当前游戏状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值