Demo entry 6630799

coed

   

Submitted by anonymous on Jul 10, 2017 at 18:42
Language: Java. Code size: 12.5 kB.

package com.majiang.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import com.majiang.model.DataModel;
import com.majiang.tools.MySqlHelper;

public class WorkView extends JPanel implements ActionListener, Runnable {

	private JPanel jpNorth = null;// 放查询框
	private JPanel jpCenter = null;// 放表格和

	private JPanel jpTable = null;// 放表格
	private JPanel jpTextField = null;// 放输入框

	private JTextField jtfQuerry = null;
	private JTextField[] jtfRight = new JTextField[11];
	private JLabel[] jl = new JLabel[12];
	private String[] jlName = { "生成订单", "客户姓名", "客户性别       ", "客户地址", "客户电话", "客户邮箱", "宠物名字", "宠物性别       ", "宠物年龄",
			"售       价", "数       量", "操作人员" };
	private JRadioButton cmale = null;
	private JRadioButton cfemale = null;
	private JRadioButton pmale = null;
	private JRadioButton pfemale = null;

	private String pno = null;
	private int cno = 0;

	private JButton jbQuerry = null;
	private JButton jbCreate = null;

	private JTable jt = null;
	private Vector<String> title = null;
	private DefaultTableModel tableModel = new DefaultTableModel();
	private JScrollPane jsp = null;

	private int i = 0;// 指示表格航标

	public WorkView() {
		intit();
	}

	private void intit() {

		this.setLayout(new BorderLayout());
		this.setBackground(Color.black);

		jpNorth = new JPanel();// 放查询框
		jpCenter = new JPanel();// 放表格和输入框

		jpTable = new JPanel();// 放表格
		jpTextField = new JPanel();// 放输入框

		jtfQuerry = new JTextField();
		jtfQuerry.setColumns(12);
		jtfQuerry.setFont(new Font("微软雅黑", Font.PLAIN, 18));

		jbQuerry = new JButton("查询");// 实例化查询按钮
		jbCreate = new JButton("创建订单");// 实例化创建订单按钮
		jbCreate.setEnabled(false);
		jbQuerry.addActionListener(this);
		jbCreate.addActionListener(this);

		JLabel jlSearch = new JLabel("查询宠物id或名字");
		jlSearch.setFont(new Font("微软雅黑", Font.BOLD, 18));
		jpNorth.add(jlSearch);
		jpNorth.add(jtfQuerry);
		jpNorth.add(jbQuerry);

		jpCenter.setLayout(new BorderLayout());
		jpTextField.setLayout(new GridLayout(15, 1));

		// 右边输入框实例化
		for (int i = 0; i < 13; i++) {
			FlowLayout flow = new FlowLayout();
			JPanel jp = new JPanel(flow);
			if (i < 12) {
				jl[i] = new JLabel(jlName[i]);
				jl[i].setFont(new Font("微软雅黑",Font.PLAIN, 16));
				jp.add(jl[i]);
			}
			if (i == 2) {
				flow.setAlignment(FlowLayout.LEFT);
				ButtonGroup group = new ButtonGroup();
				cmale = new JRadioButton("男", false);
				cmale.addActionListener(this);
				cfemale = new JRadioButton("女", true);
				cfemale.addActionListener(this);
				group.add(cmale);
				group.add(cfemale);
				jp.add(cmale);
				jp.add(cfemale);
			}

			if (i == 7) {

				flow.setAlignment(FlowLayout.LEFT);
				ButtonGroup group = new ButtonGroup();
				pmale = new JRadioButton("雄", false);
				pmale.addActionListener(this);
				pfemale = new JRadioButton("雌", true);
				pfemale.addActionListener(this);
				group.add(pmale);
				group.add(pfemale);
				jp.add(pmale);
				jp.add(pfemale);
			}
			if (i < 12 && i > 0 && i != 2 && i != 7) {
				jtfRight[i - 1] = new JTextField();
				jtfRight[i - 1].setColumns(12);
				jtfRight[i - 1].setFont(new Font("微软雅黑", 0, 18));
				
				jp.add(jtfRight[i - 1]);
			}
			if (i == 12) {
				jp.add(jbCreate);
			}

			jpTextField.add(jp);

		}
		

		cmale.setFont(new Font("微软雅黑", 0, 16));
		cfemale.setFont(new Font("微软雅黑", 0, 16));
		pmale.setFont(new Font("微软雅黑", 0, 16));
		pfemale.setFont(new Font("微软雅黑", 0, 16));

		// 表格创建和显示
		jt = new JTable() {
			@Override
			public boolean isCellEditable(int row, int column) {

				return false;
			}
		};
		jt.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		DataModel workModel = new DataModel();
		Vector<Vector> data = workModel.find(" SELECT * FROM petview;");

		title = new Vector<String>();
		title.add("宠物编号");
		title.add("宠物名字");
		title.add("宠物雌雄");
		title.add("宠物年龄");
		title.add("宠物品种");
		title.add("宠物价格");
		title.add("是否卖出");
		tableModel.setDataVector(data, title);
		jt.setModel(tableModel);

		// 设置表不能拖动不能移动
		jt.getTableHeader().setReorderingAllowed(false);
		jt.getTableHeader().setResizingAllowed(false);
		jt.getTableHeader().setReorderingAllowed(false); // 整列移
		jt.getTableHeader().setResizingAllowed(false);

		// 给表格设置监听事件
		jt.getSelectionModel().addListSelectionListener(new ListSelectionListener() {// 给JTable添加选中行的监听事件
			@Override
			public void valueChanged(ListSelectionEvent e) {
				System.out.println(e);
				System.out.println("选中行");
				DefaultTableModel tablemodel = (DefaultTableModel) jt.getModel();
				Vector<Vector> data = tablemodel.getDataVector();
				i = jt.getSelectedRow();
				if (i != -1) {
					jbCreate.setEnabled(true);
					System.out.println("选中-->>" + data.get(i));
					addTojtfRight(data, i);
				} else {
					clearJtfRight();
					jbCreate.setEnabled(false);
				}
			}
		});

		jsp = new JScrollPane(jt);

		jpTable.setLayout(new BorderLayout());
		jpTable.setBackground(Color.red);
		jpTable.add(jsp);

		jpCenter.add(jpTable, BorderLayout.CENTER);
		jpCenter.add(jpTextField, BorderLayout.EAST);

		this.add(jpNorth, BorderLayout.NORTH);
		this.add(jpCenter, BorderLayout.CENTER);
	}

	protected void clearJtfRight() {
		for (int i = 0; i < 11; i++) {
			if (i != 1 && i != 6) {
				jtfRight[i].setText("");
			}
			pmale.setSelected(false);
			pfemale.setSelected(true);
		}

	}

	protected void addTojtfRight(Vector<Vector> data, int i) {

		jtfRight[5].setText(data.get(i).get(1).toString());
		if ("雄".equals((data.get(i).get(2).toString()))) {
			pmale.setSelected(true);
		}
		if ("雌".equals((data.get(i).get(2).toString()))) {
			pfemale.setSelected(true);
		}
		jtfRight[7].setText(data.get(i).get(3).toString());
		jtfRight[8].setText(data.get(i).get(5).toString());
		pno = data.get(i).get(0).toString();

	}

	// 正则表达式判断手机号码
	public static boolean checkMobileNumber(String mobileNumber) {
		boolean flag = false;
		try {
			Pattern regex = Pattern
					.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
			Matcher matcher = regex.matcher(mobileNumber);
			flag = matcher.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	// 正则表达式判断邮箱
	public static boolean checkEmail(String email) {
		boolean flag = false;
		try {
			Pattern regex = Pattern
					.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == jbQuerry) {
			System.out.print("查询-->");
			String querryWord = jtfQuerry.getText().toString();
			String sql = " SELECT * FROM petview WHERE pno LIKE '%" + querryWord + "%' OR pname LIKE '%" + querryWord
					+ "%';";
			System.out.print(sql + "\t");
			
			DataModel workModel = new DataModel();
			Vector<Vector> data = workModel.find(sql);
			System.out.print("data:" + data);
			tableModel.setDataVector(data, title);
			tableModel.fireTableDataChanged();
			System.out.println("查询1次完毕");
			
		} else if (e.getSource() == jbCreate) {
			int flag = 0;
			for (int i = 0; i < 11; i++) {
				System.out.println(i);
				// System.out.println("jtfRight:" +
				// jtfRight[i].getText().toString());
				if (i != 1 && i != 6) {
					if (jtfRight[i].getText().toString() != null && !("".equals(jtfRight[i].getText().toString()))) {
						flag++;
					} else {
						flag = 1;
						break;
					}
				}
			}
			// System.out.println(flag);
			if (flag == 9) {
				if (!checkMobileNumber(jtfRight[3].getText().toString())) {
					flag = 2;
				}
				if (!checkEmail(jtfRight[4].getText().toString())) {
					flag = 3;
				}
			}

			if (flag == 1) {
				JOptionPane.showMessageDialog(this, "信息填写不完整", "创建订单错误", JOptionPane.ERROR_MESSAGE);
			} else if (flag == 2) {
				JOptionPane.showMessageDialog(this, "客户电话不合法", "创建订单错误", JOptionPane.ERROR_MESSAGE);

			} else if (flag == 3) {
				JOptionPane.showMessageDialog(this, "邮箱不合法", "创建订单错误", JOptionPane.ERROR_MESSAGE);

			} else {
				MySqlHelper msh = new MySqlHelper();
				boolean isOK = false;// 用来指示是否执行成功

				int value = JOptionPane.showConfirmDialog(this, "确定下单吗?", "准备下单", JOptionPane.YES_NO_OPTION);
				if (value == JOptionPane.YES_OPTION) {
					// 宠物卖出,卖出标志改为卖出
					String updatePet = " update pet set psoldflag='是' where pno = '" + pno + "';";
					System.out.println(updatePet);
					try {
						if ((msh.update(updatePet)) == 1) {
							isOK = true;
						} else {
							isOK = false;
						}
					} catch (SQLException e1) {
						JOptionPane.showMessageDialog(this, "操作失败", "创建订单错误", JOptionPane.ERROR_MESSAGE);
						System.out.println("更新宠物失败");
						e1.printStackTrace();
					}

					// 客户增加一位
					String insertCustomer = "insert into customer(cname,csex,caddr,ctel,cmail) values" + "('"
							+ jtfRight[0].getText().toString() + "','" + ((cmale.isSelected()) ? "男" : "女") + "','"
							+ jtfRight[2].getText().toString() + "','" + jtfRight[3].getText().toString() + "','"
							+ jtfRight[4].getText().toString() + "');";

					System.out.println(insertCustomer);// 测试
					if (isOK) {
						try {
							if (msh.update(insertCustomer) == 1) {
								isOK = true;
							} else {
								isOK = false;
							}
						} catch (SQLException e1) {
							isOK = false;
							JOptionPane.showMessageDialog(this, "操作失败", "创建订单错误", JOptionPane.ERROR_MESSAGE);
							e1.printStackTrace();
						}
					}

					double price = Double.parseDouble(jtfRight[8].getText().toString());
					int count = Integer.parseInt(jtfRight[9].getText().toString());

					String oprator = jtfRight[10].getText().toString();

					// 订单增加
					if (isOK) {
						try {
							// Thread.sleep(500);
							String querryCno = "Select cno from customer where cname like '"
									+ jtfRight[0].getText().toString() + "';";
							ResultSet rs = msh.query(querryCno);
							System.out.println(querryCno);
							rs.next();
							cno = rs.getInt("cno");
							System.out.println("Cno" + cno);
							rs.close();
						} catch (SQLException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
						String insertOrderForm = "INSERT INTO orderlist(cno,pno,price,count_,money,oprator) "
								+ "VALUES(" + "'" + cno + "'," + "'" + pno + "'," + price + "," + count + ","
								+ price * count + ",'" + oprator + "');";
						try {
							int insertFlag = msh.update(insertOrderForm);
						} catch (SQLException e1) {
							isOK = false;
							JOptionPane.showMessageDialog(this, "操作失败", "创建订单错误", JOptionPane.ERROR_MESSAGE);
							e1.printStackTrace();
							e1.printStackTrace();
						}
						isOK = true;
					} else {
						isOK = false;
					}

					if (isOK) {
						double d = Double.parseDouble(jtfRight[8].getText().toString())
								* Integer.parseInt(jtfRight[9].getText().toString());
						String mess = "下单成功,一共" + d + "元!";
						JOptionPane.showMessageDialog(this, mess);
						String sql = " SELECT * FROM petview";
						DataModel workModel = new DataModel();
						Vector<Vector> data = workModel.find(sql);
						System.out.print("data:" + data);
						tableModel.setDataVector(data, title);
						tableModel.fireTableDataChanged();
					}
					msh.free();
				}
			}

		}

	}

	@Override
	public void run() {

		while (true) {}

	}
}

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).