?? dingpiaofrm.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 + -