1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| package com.test.four;
import javax.swing.JFrame; import javax.swing.JComboBox;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;
import javax.swing.DefaultComboBoxModel; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton;
public class Demo { private static JTextField tf_price; private static JTextField tf_sytk; private static JTextField tf_buytk; private static JTextField tf_start; private static JTextField tf_finish; private static JTextField tf_hdtk; private static JComboBox cb; private static JButton bt_buy; private static JButton bt_add; private static JButton bt_del; public static void main(String[] args) { JFrame myFrame = new JFrame("火车订票"); myFrame.setVisible(true); myFrame.setSize(300,200); myFrame.getContentPane().setLayout(null); cb = new JComboBox(); cb.setModel(new DefaultComboBoxModel(new String[] {"宁波 → 杭州", "温州 → 杭州"})); cb.setBounds(10, 10, 130, 21); myFrame.getContentPane().add(cb); JLabel lblNewLabel = new JLabel("\u5355\u4EF7\uFF1A"); lblNewLabel.setBounds(177, 69, 56, 15); myFrame.getContentPane().add(lblNewLabel); tf_price = new JTextField(); tf_price.setBounds(220, 66, 54, 21); myFrame.getContentPane().add(tf_price); tf_price.setColumns(10); JLabel label = new JLabel("\u5269\u4F59\u7968\u6570\uFF1A"); label.setBounds(153, 41, 62, 15); myFrame.getContentPane().add(label); tf_sytk = new JTextField(); tf_sytk.setColumns(10); tf_sytk.setBounds(220, 38, 54, 21); myFrame.getContentPane().add(tf_sytk); bt_buy = new JButton("购买"); bt_buy.setBounds(153, 121, 121, 23); myFrame.getContentPane().add(bt_buy); JLabel label_1 = new JLabel("购买票数:"); label_1.setBounds(153, 96, 62, 15); myFrame.getContentPane().add(label_1); tf_buytk = new JTextField(); tf_buytk.setColumns(10); tf_buytk.setBounds(220, 93, 54, 21); myFrame.getContentPane().add(tf_buytk); bt_add = new JButton("添加新票"); bt_add.setBounds(10, 121, 130, 23); myFrame.getContentPane().add(bt_add); JLabel label_2 = new JLabel("出发地"); label_2.setBounds(10, 41, 62, 15); myFrame.getContentPane().add(label_2); tf_start = new JTextField(); tf_start.setColumns(10); tf_start.setBounds(77, 38, 63, 21); myFrame.getContentPane().add(tf_start); JLabel label_2_1 = new JLabel("目的地"); label_2_1.setBounds(10, 69, 62, 15); myFrame.getContentPane().add(label_2_1); tf_finish = new JTextField(); tf_finish.setColumns(10); tf_finish.setBounds(77, 66, 63, 21); myFrame.getContentPane().add(tf_finish); JLabel label_2_2 = new JLabel("核定票数:"); label_2_2.setBounds(10, 96, 62, 15); myFrame.getContentPane().add(label_2_2); tf_hdtk = new JTextField(); tf_hdtk.setColumns(10); tf_hdtk.setBounds(77, 93, 63, 21); myFrame.getContentPane().add(tf_hdtk); JButton bt_del = new JButton("删除"); bt_del.setBounds(153, 9, 121, 23); myFrame.getContentPane().add(bt_del); myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); myFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent w) { System.exit(0); } }); int[][] tk = new int [100][2]; tk[0][0] = 50; tk[0][1] = 30; tk[1][0] = 30; tk[1][1] = 100; tf_sytk.setText(String.valueOf(tk[0][0])); tf_price.setText(String.valueOf(tk[0][1])); ItemListener a = new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == e.SELECTED) { tf_sytk.setText(String.valueOf(tk[cb.getSelectedIndex()][0])); tf_price.setText(String.valueOf(tk[cb.getSelectedIndex()][1])); } } }; cb.addItemListener(a); ActionListener b = new ActionListener() { public void actionPerformed(ActionEvent e) { boolean eq = true; if (e.getSource() == bt_add) { try { if (tf_start.getText().equals(tf_finish.getText())) { JOptionPane.showMessageDialog(null, "出发地和目的地不能相同!"); tf_start.setText(""); tf_finish.setText(""); } else { String str = tf_start.getText() + " → " + tf_finish.getText(); for (int i = 0; i < cb.getItemCount(); i++) { if (str.equals(cb.getItemAt(i))) { eq = false; break; } } if (eq) { tk[cb.getItemCount()][0] = Integer.parseInt(tf_hdtk.getText()); tk[cb.getItemCount()][1] = Integer.parseInt(tf_price.getText()); cb.addItem(str); cb.setSelectedIndex(cb.getItemCount()-1); tf_hdtk.setText(""); tf_start.setText(""); tf_finish.setText(""); JOptionPane.showMessageDialog(null, "添加成功"); } else { JOptionPane.showMessageDialog(null, "车程已经存在!"); tf_hdtk.setText(""); tf_price.setText(""); tf_start.setText(""); tf_finish.setText(""); } } } catch (Exception e2) { if (tf_start.getText().length() == 0 || tf_finish.getText().length() == 0) { JOptionPane.showMessageDialog(null, "出发地和目的地都不能为空!"); } else { JOptionPane.showMessageDialog(null, "票数和单价都必须为数值且不能为空!"); } } } if (e.getSource() == bt_del) { if (cb.getSelectedIndex() == cb.getItemCount() - 1) { cb.removeItemAt(cb.getSelectedIndex()); } else { for (int i = cb.getSelectedIndex(); i < cb.getItemCount() - 1; i++) { tk[i][0] = tk[i+1][0]; tk[i][1] = tk[i+1][1]; } cb.removeItemAt(cb.getSelectedIndex()); } } if (e.getSource() == bt_buy) { try { int nums = Integer.parseInt(tf_buytk.getText()); int price = tk[cb.getSelectedIndex()][1]; if (nums == 0) { JOptionPane.showMessageDialog(null, "购票数不可为0!"); } else if (nums <= tk[cb.getSelectedIndex()][0] && tk[cb.getSelectedIndex()][0] > 0) { tk[cb.getSelectedIndex()][0] = tk[cb.getSelectedIndex()][0] - nums; JOptionPane.showMessageDialog(null, "购票成功,共" + nums *price + "元!"); } else if (tk[cb.getSelectedIndex()][0] == 0) { JOptionPane.showMessageDialog(null, "抱歉,此车程已经售罄!"); } else { JOptionPane.showMessageDialog(null, "抱歉,您购买的票数超过了库存!"); } tf_sytk.setText(String.valueOf(tk[cb.getSelectedIndex()][0])); } catch (Exception e2) { JOptionPane.showMessageDialog(null, "购买票数不可为空"); } } } }; bt_add.addActionListener(b); bt_del.addActionListener(b); bt_buy.addActionListener(b); } }
|