⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trainframe.java

📁 实现了在线订火车票的功能。 java开发
💻 JAVA
字号:
package trainex;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.ButtonGroup;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.*;
import javax.swing.JOptionPane;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class TrainFrame extends JFrame {
    JPanel contentPane;
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JTextField txtnum = new JTextField();
    JTextField txtcount = new JTextField();
    JRadioButton txtying = new JRadioButton();
    JRadioButton txtwo = new JRadioButton();
    JButton btnok = new JButton();
    ButtonGroup buttonGroup1 = new ButtonGroup();
    public TrainFrame() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(447, 407));
        setTitle("");
        jLabel1.setText("订购火车票");
        jLabel1.setBounds(new Rectangle(150, 39, 100, 15));
        jLabel2.setText("火车车次");
        jLabel2.setBounds(new Rectangle(69, 107, 75, 15));
        jLabel3.setText("乘车方式");
        jLabel3.setBounds(new Rectangle(69, 168, 75, 15));
        jLabel4.setText("订购数量");
        jLabel4.setBounds(new Rectangle(69, 229, 75, 15));
        txtnum.setBounds(new Rectangle(157, 107, 111, 20));
        txtcount.setBounds(new Rectangle(164, 229, 105, 20));
        txtying.setText("硬座");
        txtying.setBounds(new Rectangle(163, 168, 64, 23));
        txtwo.setText("硬卧");
        txtwo.setBounds(new Rectangle(242, 169, 67, 23));
        btnok.setBounds(new Rectangle(149, 316, 81, 23));
        btnok.setText("确定");
        btnok.addActionListener(new TrainFrame_btnok_actionAdapter(this));
        //
        buttonGroup1.add(txtying);
        buttonGroup1.add(txtwo);
        //
        contentPane.add(jLabel3);
        contentPane.add(jLabel4);
        contentPane.add(jLabel1);
        contentPane.add(jLabel2);
        contentPane.add(txtnum);
        contentPane.add(btnok);
        contentPane.add(txtying);
        contentPane.add(txtwo);
        contentPane.add(txtcount);
    }

    public void btnok_actionPerformed(ActionEvent e) {
        String strnum = txtnum.getText().trim();
        int strcount = 0;

        try {
            strcount = Integer.parseInt(txtcount.getText().trim());
        } catch (Exception ex1) {
            JOptionPane.showMessageDialog(this, "请输入整数。", "message",
                                          JOptionPane.ERROR_MESSAGE);
        }
        DBCon db = new DBCon();
        String sql = "select * from TrainTicket where TrainNo='" + strnum + "'";
        ResultSet rs = db.getselect(sql);
        try {
            if (rs.next()) {
                int seatNum = rs.getInt(2);
                int bedNum = rs.getInt(3);

                float seatPrice = rs.getFloat(4);

                float bedPrice = rs.getFloat(5);
                if (txtying.isSelected()) {

                    if (seatNum < strcount) {
                        JOptionPane.showMessageDialog(this, "车票订购失败!原因:车票数量不足。",
                                "message", JOptionPane.ERROR_MESSAGE);
                    } else {
                        float w = seatPrice * strcount;
                        sql = "update trainticket set seatnum=seatnum-" +
                              strcount + " where trainno='" + strnum + "'";
                        db.getUpdate(sql);
                        JOptionPane.showMessageDialog(this,
                                "车票订购成功!请支付" + w + "元!", "message",
                                JOptionPane.ERROR_MESSAGE);

                    }
                } else {
                    if (bedNum < strcount) {
                        JOptionPane.showMessageDialog(this, "车票订购失败!原因:车票数量不足。",
                                "message", JOptionPane.ERROR_MESSAGE);
                    } else {
                        float w = bedPrice * strcount;
                        sql = "update trainticket set bednum =bednum-" +
                              strcount + " where trainno='" + strnum + "'";
                        db.getUpdate(sql);
                        JOptionPane.showMessageDialog(this,
                                "车票订购成功!请支付" + w + "元!", "message",
                                JOptionPane.ERROR_MESSAGE);
                    }

                }
            } else {
                JOptionPane.showMessageDialog(this, "没有你所输入的车次!", "message",
                                              JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}


class TrainFrame_btnok_actionAdapter implements ActionListener {
    private TrainFrame adaptee;
    TrainFrame_btnok_actionAdapter(TrainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnok_actionPerformed(e);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -