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

📄 dingpiaofrm.java

📁 JAVA数据库编程实例随书源码 JAVA数据库编程实例随书源码
💻 JAVA
字号:
package train_table;import java.awt.*;import javax.swing.*;import com.borland.jbcl.layout.*;import javax.swing.border.*;import com.borland.dbswing.*;import java.util.Calendar;import java.awt.event.*;import java.sql.*;public class dingpiaoFrm extends JFrame { errormessage em=new errormessage();          //判断错误信息  Calendar curdate=Calendar.getInstance() ;   //日历  String[] shijian=new String[]{"今天","明天","后天"};  private JComboBox jComboBox1 = new JComboBox(shijian);  int  year=curdate.get(Calendar.YEAR ) ;     //获得年  int  month =curdate.get(Calendar.MONTH)+1 ;  //获得月  int  day =curdate.get(Calendar.DAY_OF_MONTH) ;//获得日  private JPanel jPanel1 = new JPanel();  private XYLayout xYLayout1 = new XYLayout();  private TitledBorder titledBorder1;  private XYLayout xYLayout2 = new XYLayout();  private JPanel jPanel2 = new JPanel();  private TitledBorder titledBorder2;  private XYLayout xYLayout3 = new XYLayout();  private JPanel jPanel3 = new JPanel();  private TitledBorder titledBorder3;  private JLabel jLabel2 = new JLabel();  private JLabel jLabel3 = new JLabel();  private XYLayout xYLayout4 = new XYLayout();  private JdbTextField times_txt = new JdbTextField();  private JdbTextField station_txt = new JdbTextField();  private JLabel jLabel1 = new JLabel();  private JLabel jLabel4 = new JLabel();  private JPanel jPanel5 = new JPanel();  private JPanel jPanel6 = new JPanel();  private JLabel jLabel5 = new JLabel();  private Border border1;  private TitledBorder titledBorder4;  private JLabel jLabel6 = new JLabel();  private XYLayout xYLayout5 = new XYLayout();  private TitledBorder titledBorder5;  private JLabel jLabel7 = new JLabel();  private JLabel jLabel8 = new JLabel();  private JTextField yingzuo_txt = new JTextField();  private JTextField wopu_txt = new JTextField();  private JButton jButton2 = new JButton();  private JButton jButton3 = new JButton();  private JLabel jLabel9 = new JLabel();  private JTextField zhangshu_txt = new JTextField();  private JButton jButton1 = new JButton();  private JRadioButton jRadioButton1 = new JRadioButton();  private JRadioButton jRadioButton2 = new JRadioButton();  private TitledBorder titledBorder6;  private TitledBorder titledBorder7;  private JLabel log_label = new JLabel();  private XYLayout xYLayout6 = new XYLayout();  private JButton jButton4 = new JButton();  private TitledBorder titledBorder8;  public dingpiaoFrm() {    try {      jbInit();      pack();    }    catch(Exception e) {      e.printStackTrace();    }  }  private void jbInit() throws Exception {   String date=Integer.toString(year)+"-"+Integer.toString(month)+"-"+Integer.toString(day);   titledBorder8 = new TitledBorder("");    jLabel6.setText(date);    titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(151, 145, 140)),"");    titledBorder2 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(151, 145, 140)),"数据加载方法");    titledBorder3 = new TitledBorder("");    border1 = BorderFactory.createEtchedBorder(Color.white,new Color(151, 145, 140));    titledBorder4 = new TitledBorder("");    titledBorder5 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(151, 145, 140)),"当前时间");    titledBorder6 = new TitledBorder("");    titledBorder7 = new TitledBorder("");    this.getContentPane().setLayout(xYLayout1);    jPanel1.setLayout(xYLayout2);    this.setTitle("订票系统");    jPanel2.setBorder(titledBorder8);    jPanel2.setLayout(xYLayout3);    jPanel3.setBorder(titledBorder3);    jPanel3.setLayout(xYLayout4);    jLabel2.setText("车次");    jLabel3.setText("车站");    jLabel1.setText("1.定票时间必须为三天以内的票");    jLabel4.setText("2.本系统只属模拟不涉及票额的计算");    jPanel5.setBorder(titledBorder5);    jPanel5.setLayout(xYLayout5);    jPanel6.setBorder(titledBorder1);    jPanel6.setLayout(xYLayout6);    jLabel5.setText("订票时间");    jPanel1.setBorder(border1);    jLabel6.setForeground(new Color(177, 66, 138));    jLabel7.setText("硬座余票");    jLabel8.setText("卧铺余票");    jButton2.setEnabled(false);    jButton2.setText("订票打印");    jButton2.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jButton2_actionPerformed(e);      }    });    jButton3.setEnabled(false);    jButton3.setText("打印欲览");    yingzuo_txt.setEditable(false);    yingzuo_txt.setHorizontalAlignment(SwingConstants.RIGHT);    xYLayout1.setWidth(417);    xYLayout1.setHeight(324);    jLabel9.setText("预定张数");    jButton1.setActionCommand("");    jButton1.setText("查看余票");    jButton1.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jButton1_actionPerformed(e);      }    });    jRadioButton1.setSelected(true);    jRadioButton1.setText("硬座");    jRadioButton1.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jRadioButton1_actionPerformed(e);      }    });    jRadioButton2.setText("卧铺");    jRadioButton2.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jRadioButton2_actionPerformed(e);      }    });    zhangshu_txt.setText("1");    zhangshu_txt.setHorizontalAlignment(SwingConstants.RIGHT);    wopu_txt.setEditable(false);    wopu_txt.setHorizontalAlignment(SwingConstants.RIGHT);    jButton4.setText("清空");    jButton4.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        jButton4_actionPerformed(e);      }    });    log_label.setForeground(SystemColor.activeCaption);    log_label.setHorizontalTextPosition(SwingConstants.CENTER);    this.getContentPane().add(jPanel1,             new XYConstraints(7, 6, 400, 291));    jPanel1.add(jPanel2,           new XYConstraints(3, 12, 207, 83));    jPanel2.add(jLabel1,   new XYConstraints(6, 9, -1, -1));    jPanel2.add(jLabel4,   new XYConstraints(6, 37, -1, -1));    jPanel1.add(jPanel3,                   new XYConstraints(5, 103, 382, 99));    jPanel3.add(jLabel2, new XYConstraints(23, 2, -1, -1));    jPanel3.add(times_txt,   new XYConstraints(78, 2, 79, -1));    jPanel3.add(station_txt,      new XYConstraints(78, 30, 79, -1));    jPanel3.add(jLabel3,       new XYConstraints(23, 30, -1, -1));    jPanel3.add(yingzuo_txt,  new XYConstraints(259, 2, 87, -1));    jPanel3.add(wopu_txt,   new XYConstraints(260, 30, 86, -1));    jPanel3.add(jLabel7,  new XYConstraints(196, 2, -1, -1));    jPanel3.add(jLabel8,      new XYConstraints(197, 34, -1, -1));    jPanel3.add(jButton4,  new XYConstraints(277, 62, 74, 24));    jPanel3.add(jLabel5, new XYConstraints(15, 62, -1, -1));    jPanel3.add(jComboBox1, new XYConstraints(78, 62, 79, -1));    jPanel3.add(jButton1, new XYConstraints(195, 62, 82, 24));    jPanel1.add(jRadioButton1, new XYConstraints(41, 210, -1, -1));    jPanel1.add(jLabel9, new XYConstraints(42, 251, -1, -1));    jPanel1.add(zhangshu_txt,   new XYConstraints(112, 246, 81, -1));    jPanel1.add(jRadioButton2, new XYConstraints(112, 210, -1, -1));    jPanel1.add(jButton3, new XYConstraints(249, 213, -1, 24));    jPanel1.add(jButton2, new XYConstraints(248, 245, -1, 24));    jPanel1.add(jPanel5, new XYConstraints(218, 5, 170, 55));    jPanel5.add(jLabel6, new XYConstraints(42, 0, 98, 20));    jPanel1.add(jPanel6, new XYConstraints(220, 64, 167, 31));    jPanel6.add(log_label,       new XYConstraints(-1, 1, 157, 19));  } void jButton1_actionPerformed(ActionEvent e) {    String times=times_txt.getText();    String station=station_txt.getText();    String date1;     int i=0;     JianSuanShiJian jssj=new JianSuanShiJian();  if(!em.isEmpty(times)&&!em.isEmpty(station))     //输入为空的处理      {     if(jComboBox1.getSelectedItem().equals("今天"))          {          jssj.getTime(year,month,day,i);          //对时间进行计算          date1=String.valueOf(year)+"-"+String.valueOf(month)+"-"+String.valueOf(day);        }        else if(jComboBox1.getSelectedItem().equals("明天"))        {          jssj.getTime(year,month,day,i+1);        //对时间进行计算          date1=String.valueOf(year)+"-"+String.valueOf(month)+"-"+String.valueOf(day);        }        else{          jssj.getTime(year,month,day,i+2);         //对时间进行计算          date1=String.valueOf(year)+"-"+String.valueOf(month)+"-"+String.valueOf(day);        }           dbBean db=new dbBean();            ResultSet RS=null;           String SQL1="select * from checibiao where checibiao.\"车次\"='"+times+"' and checibiao.\"到达车站\"='"+station+"'";         try{           RS=db.executeQuery(SQL1);                  //查询输入的车次和车站           if(RS.next()){                             //判断输入的车次和车站是否存在             dbBean DB=new dbBean();             ResultSet rs=null;             String  sql="select * from dingpiao where times='"+times+"' and riqi='"+date1+"'" ;             try{           rs=  DB.executeQuery(sql);                //查询输入车次在某一天的是否存在             if(!rs.next())                          //若无则向数据库中加入这一天的票数                 {                  String SQL="insert into dingpiao (times,riqi,yingzuo,wopu) values ('"+times+"','"+date1+"',"+500+","+200+")";                  db.executeQuery(SQL);                  yingzuo_txt.setText("500");                  wopu_txt.setText("200");               }               else{                                //否则将数据库中的票数显示出来                yingzuo_txt.setText(rs.getString("yingzuo"));                wopu_txt.setText(rs.getString("wopu"));               }               }               catch(Exception t)               {}               jButton2.setEnabled(true);               jButton3.setEnabled(true);           }else{               log_label.setText("查无此站");           }           }           catch(Exception ee){           }         }      else{     log_label.setText("车次车站不能为空");      }  }  void jRadioButton2_actionPerformed(ActionEvent e) {   //卧铺选择按钮  jRadioButton1.setSelected(false);  zhangshu_txt.setText("1");  }  void jRadioButton1_actionPerformed(ActionEvent e) {   //硬座选择按钮    jRadioButton2.setSelected(false);    zhangshu_txt.setText("1");  }  void jButton4_actionPerformed(ActionEvent e) {        //清空处理   times_txt.setText("");   station_txt.setText("");   yingzuo_txt.setText("");   wopu_txt.setText("");  }  void jButton2_actionPerformed(ActionEvent e) {      //订票处理事件    String times=times_txt.getText();    String station=station_txt.getText();   String zhs=zhangshu_txt.getText();   dbBean db=new dbBean();    ResultSet rs=null;    ResultSet Rs=null;  JianSuanShiJian jssj=new JianSuanShiJian();   String date1;   int i=0;   int yupiao=0;   int yuwopu=0;   int zhangshu=0;   if(jComboBox1.getSelectedItem().equals("今天"))   //预定今天票的处理         {         jssj.getTime(year,month,day,i);            //时间计算         date1=String.valueOf(year)+"-"+String.valueOf(month)+"-"+String.valueOf(day);       }       else if(jComboBox1.getSelectedItem().equals("明天"))  //预定明天票的处理       {         jssj.getTime(year,month,day,i+1);          //时间计算         date1=String.valueOf(year)+"-"+String.valueOf(month)+"-"+String.valueOf(day);       }       else{                                        //预定后天票的处理         jssj.getTime(year,month,day,i+2);          //时间计算         date1=String.valueOf(year)+"-"+String.valueOf(month)+"-"+String.valueOf(day);        }       if(!em.isEmpty(zhs))                //判断输入是否为空       {          if(em.isNum(zhs)){               //判断输入的张数是否是数字        zhangshu=Integer.parseInt(zhs);        yuwopu=Integer.parseInt(wopu_txt.getText())-zhangshu;        yupiao=Integer.parseInt(yingzuo_txt.getText())-zhangshu;      if(jRadioButton1.isSelected())       //预定硬座票的处理         {        if((yupiao)>=0){                   //判断票数是否足够       String sql2="update dingpiao set yingzuo="+yupiao+" where times='"+times+"'and riqi='"+date1+"'";       db.executeQuery(sql2);              //完成订票,并更新数据库        yingzuo_txt.setText(Integer.toString(yupiao)); //显示余票        }           else{             JOptionPane.showMessageDialog(null, "硬座票数量不够!根据余票合理订票");             zhangshu_txt.setText("");           }           }           else{                            //处理预定卧铺的情况             if(yuwopu>=0)                  //判断卧铺票是否足够             {               String sql2="update dingpiao set wopu="+yuwopu+" where times='"+times+"' and riqi='"+date1+"'";               db.executeQuery(sql2);       //完成订票,并更新数据库               wopu_txt.setText(Integer.toString(yuwopu));               log_label.setText("订票成功!");             }             else{                JOptionPane.showMessageDialog(null, "卧铺票数量不够!");             }           }         }         else{           log_label.setText("输入必须为数字");       zhangshu_txt.setText("");         }       }   else{       log_label.setText("输入不能为空!");       zhangshu_txt.setText("");    }}}

⌨️ 快捷键说明

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