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

📄 customersdialog.java

📁 用java编写的人事管理系统,很好的 学习下吧
💻 JAVA
字号:
/**
 * 源文件:CustomersDialog.java
 * 作用:顾客信息窗体
 */
package mypro;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;

public class CustomersDialog extends JDialog implements ActionListener,MouseListener
{
	CustomersData cd=new CustomersData();
	Check check=new Check();
	private JLabel labCode,labCustomerName,labSex,labTel,labCustomerID,labEmail,labAddr,labPostalCode;
	private JTextField txtCode,txtCustomerName,txtTel,txtCustomerID,txtEmail,txtAddr,txtPostalCode;
	private JRadioButton raMale,raFemale;
	private ButtonGroup bg;
	private JButton btnInsert,btnUpdate,btnDelete;
	private JScrollPane scop;
	private JTable tabShow;
	private DefaultTableModel dtm;
	private int row=0;
	
	static 
	{
   	 	//设置窗体为豪华框架
   		JDialog.setDefaultLookAndFeelDecorated(true);		
	}

	public CustomersDialog(MainFrame owner,boolean bo)
	{
		super(owner,bo);
  		labCode = new JLabel("*顾客编号");
  		labCustomerName = new JLabel("*顾客姓名");
  		labSex = new JLabel("*性别");
  		labTel = new JLabel("*联系电话");
  		labCustomerID = new JLabel("身份证号码");
  		labEmail = new JLabel("电子邮箱");
  		labAddr = new JLabel("家庭住址");	
  		labPostalCode = new JLabel("邮政编码");
  		txtCode = new JTextField();
  		txtCustomerName = new JTextField();
		raMale = new JRadioButton("男");
		raFemale = new JRadioButton("女");
		bg = new ButtonGroup();
  		txtTel = new JTextField();
  		txtCustomerID = new JTextField();
  		txtEmail = new JTextField();
  		txtAddr = new JTextField();
  		txtPostalCode = new JTextField();
  		btnInsert = new JButton("插入");
  		btnUpdate = new JButton("修改");
  		btnDelete = new JButton("删除");
  		scop = new JScrollPane();
  		tabShow = new JTable();
  		
        labCode.setBounds(new Rectangle(12, 16, 73, 14));
    	txtCode.setBounds(new Rectangle(82, 16, 121, 23));
    	labCustomerName.setBounds(new Rectangle(12, 49, 73, 14));
    	txtCustomerName.setBounds(new Rectangle(82, 49, 121, 23));
    	labSex.setBounds(new Rectangle(12, 82, 73, 14));
    	raMale.setBounds(new Rectangle(82, 82, 55, 23));
    	raMale.setSelected(true);
		raFemale.setBounds(new Rectangle(142, 82, 55, 23));
    	labTel.setBounds(new Rectangle(12, 114, 73, 14));
   		txtTel.setBounds(new Rectangle(82, 114, 121, 23));
   		labCustomerID.setBounds(new Rectangle(254, 18, 73, 14));
    	txtCustomerID.setBounds(new Rectangle(330, 18, 121, 23));
    	labEmail.setBounds(new Rectangle(254, 49, 73, 14));
    	txtEmail.setBounds(new Rectangle(330, 49, 121, 23));
    	labAddr.setBounds(new Rectangle(254, 82, 73, 14));
    	txtAddr.setBounds(new Rectangle(330, 82, 121, 23));
    	labPostalCode.setBounds(new Rectangle(254, 117, 73, 14));
        txtPostalCode.setBounds(new Rectangle(330, 117, 121, 23));
        
  		btnInsert.setBounds(new Rectangle(12, 148, 89, 28));
  		btnInsert.setActionCommand("Insert");
  		btnInsert.setToolTipText("插入新的顾客信息");
      	btnUpdate.setBounds(new Rectangle(187, 148, 89, 28));
      	btnUpdate.setActionCommand("Update");
      	btnUpdate.setToolTipText("修改顾客信息");
    	btnDelete.setBounds(new Rectangle(362, 148, 89, 28));
    	btnDelete.setActionCommand("Delete");
    	btnDelete.setToolTipText("删除顾客信息");
        scop.setBounds(new Rectangle(12, 190, 439, 98));
        
    	btnInsert.addActionListener(this);
    	btnUpdate.addActionListener(this);
    	btnDelete.addActionListener(this);
    	tabShow.addMouseListener(this);
    	
    	Container me=this.getContentPane();
  		me.setLayout(null);
  		me.add(labCode);
        me.add(txtCode);
        me.add(labCustomerName);
        me.add(txtCustomerName);
        me.add(labSex);
   	 	me.add(raMale);
        me.add(raFemale);
        bg.add(raMale);
        bg.add(raFemale);
        me.add(labTel);
        me.add(txtTel);
   	 	me.add(labCustomerID);
    	me.add(txtCustomerID);
    	me.add(labEmail);
    	me.add(txtEmail);
    	me.add(labAddr);
    	me.add(txtAddr);
    	me.add(labPostalCode);
    	me.add(txtPostalCode);
    	me.add(btnInsert);
    	me.add(btnDelete);
    	me.add(btnUpdate);
    	me.add(scop);
        scop.getViewport().add(tabShow);
        DatabaseOperate datao = new DatabaseOperate();
  		dtm = new DefaultTableModel(datao.getDataByTabname("customers"),datao.getColumnNamesByTabname("customers"));
        tabShow.setModel(dtm);//通过dtm模型构造表
        tabShow.setAutoResizeMode(tabShow.AUTO_RESIZE_OFF);//当调整表的大小时,设置表的自动调整模式为不自动调整列的宽度;使用滚动条
    	
	  	this.setTitle("顾客信息");
	  	this.setSize(480,380);
	  	this.setLocationRelativeTo(this);
	  	this.setResizable(false);
	  	this.setVisible(true);
	  }
	  public void actionPerformed(ActionEvent cae)
	  {
		String strbtn=cae.getActionCommand();
			  	
		if(strbtn.equals("Insert"))
		{
			int strcode=0;
			try
			{
			  	strcode=Integer.parseInt(txtCode.getText().trim());
			}
			catch(NumberFormatException ne)
			{
			  	JOptionPane.showMessageDialog(null,"请输入4位以内整数","提示",JOptionPane.INFORMATION_MESSAGE);
			  	txtCode.setText("");
		 		txtCode.requestFocus();
			}
			if(check.checkPrimary("customers","顾客编号",strcode))
			{
			  	JOptionPane.showMessageDialog(null,"该顾客编号已存在","提示",JOptionPane.INFORMATION_MESSAGE);
			  	txtCode.setText("");
		 		txtCode.requestFocus();
			}
			else if(operationBeforeCheck())
			{
				InsertData ind =new InsertData();
				Vector vid = dataVector();
				if(ind.CustomersInsert(cd))
				{
				  	JOptionPane.showMessageDialog(null,"成功插入一行数据","提示",JOptionPane.INFORMATION_MESSAGE);
				  	dtm.addRow(vid);//添加一行到模型的结尾	
				}
				else
				{
				  	JOptionPane.showMessageDialog(null,"数据插入失败","提示",JOptionPane.INFORMATION_MESSAGE);	
				}
			}
		}
		else if(strbtn.equals("Update"))
		{
			int strcode=0;
			Integer n=(Integer)tabShow.getValueAt(row,0);
			int strcode1=n.intValue();
			String strcd=String.valueOf(tabShow.getValueAt(row,0));
			try
			{
			  	strcode=Integer.parseInt(txtCode.getText().trim());
			}
			catch(NumberFormatException ne)
			{
			  	JOptionPane.showMessageDialog(null,"请输入4位以内整数","提示",JOptionPane.INFORMATION_MESSAGE);
			  	txtCode.setText("");
		 		txtCode.requestFocus();
			}
			if(strcode!=strcode1)
			{
				if(check.checkPrimary("sellTel","顾客编号",strcode1))
				{
					JOptionPane.showMessageDialog(null,"该顾客编号被销售信息表引用,禁止更改","提示",JOptionPane.INFORMATION_MESSAGE);
				  	txtCode.setText(strcd);
				}
				else if(check.checkUpdataAfterUnique("customers","顾客编号",strcode,strcode1))
				{
					JOptionPane.showMessageDialog(null,"该顾客编号已存在,无法修改","提示",JOptionPane.INFORMATION_MESSAGE);
				  	txtCode.setText(strcd);
			 		txtCode.requestFocus();
				}
			}
			else if(operationBeforeCheck())
			{
				UpdateData upd=new UpdateData();
					
				if(upd.customersUpdate(cd,strcode1))
				{
					JOptionPane.showMessageDialog(null,"成功修改一行数据","提示",JOptionPane.INFORMATION_MESSAGE);
					changeRowData();//修改表模型中的数据
				}
				else
				{
					JOptionPane.showMessageDialog(null,"数据修改失败","提示",JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}
		else if(strbtn.equals("Delete"))
		{
			Integer n=(Integer)tabShow.getValueAt(row,0);
			int strcoded=n.intValue();
			DeleteData ded=new DeleteData();
			if(check.checkPrimary("sellTel","顾客编号",strcoded))
			{
				JOptionPane.showMessageDialog(null,"该顾客编号被销售信息表引用,请先删除或修改其他表中相关数据","提示",JOptionPane.INFORMATION_MESSAGE);
			}
			else
			{
				if(ded.deleteDate("customers","顾客编号",strcoded))
				{
					JOptionPane.showMessageDialog(null,"成功删除一行数据","提示",JOptionPane.INFORMATION_MESSAGE);
					dtm.removeRow(row);//移除模型中 row 位置的行
				}
				else
				{
					JOptionPane.showMessageDialog(null,"数据删除失败","提示",JOptionPane.INFORMATION_MESSAGE);
				}	
			}
		}
		txtCode.setText("");
		txtCustomerName.setText("");
		txtTel.setText("");
		txtCustomerID.setText("");
		txtEmail.setText("");
		txtAddr.setText("");
		txtPostalCode.setText("");;
	  }
	  /*单击表中的一行将数据返回到组件中*/
	  public void mouseEntered(MouseEvent me){}
	  public void mouseExited(MouseEvent me){}
	  public void mousePressed(MouseEvent me){}
	  public void mouseReleased(MouseEvent me){}
	  public void mouseClicked(MouseEvent me)
	  {
	  	row=tabShow.getSelectedRow();
	  	String strcode=String.valueOf(tabShow.getValueAt(row,0));
	  	String strcn=(String)tabShow.getValueAt(row,1);
	  	String strsex=(String)tabShow.getValueAt(row,2);
	  	String strtel=(String)tabShow.getValueAt(row,3);
	  	String strcID=(String)tabShow.getValueAt(row,4);
	  	String stremail=(String)tabShow.getValueAt(row,5);
	  	String straddr=(String)tabShow.getValueAt(row,6);
	  	String strpc=(String)tabShow.getValueAt(row,7);
	  	
	  	txtCode.setText(strcode.trim());
	  	txtCustomerName.setText(strcn.trim());
	  	if(strsex.equals("男"))
	  	{
	  		raMale.setSelected(true);
	  	}
	  	else if(strsex.equals("女"))
	  	{
	  		raFemale.setSelected(true);
	  	}
	  	txtTel.setText(strtel.trim());
	  	if(strcID==null)
	  	{
	  		txtCustomerID.setText(strcID);	
	  	}
	  	else
	  	{
	  		txtCustomerID.setText(strcID.trim());
	  	}
	  	if(stremail==null)
	  	{
	  		txtEmail.setText(stremail);	
	  	}
	  	else
	  	{
	  		txtEmail.setText(stremail.trim());
	  	}
	  	if(straddr==null)
	  	{
	  		txtAddr.setText(straddr);	
	  	}
	  	else
	  	{
	  		txtAddr.setText(straddr.trim());
	  	}
	  	if(strpc==null)
	  	{
	  		txtPostalCode.setText(strpc);	
	  	}
	  	else
	  	{
	  		txtPostalCode.setText(strpc.trim());
	  	}
	  	
	  }
	  /* 执行操作前对数据进行验证*/
	  public boolean operationBeforeCheck()
	  {
		int strcode=0;
		try
		{
		  	strcode=Integer.parseInt(txtCode.getText().trim());
		}
		catch(NumberFormatException ne)
		{
		  	JOptionPane.showMessageDialog(null,"请输入4位以内整数","提示",JOptionPane.INFORMATION_MESSAGE);
		  	txtCode.setText("");
	 		txtCode.requestFocus();
		  	return false;
		}
		String strcn=txtCustomerName.getText().trim();
		String strsex;
		if(raMale.isSelected())
		{
		  	strsex=raMale.getText().trim();
		}
		else
		{
		  	strsex=raFemale.getText().trim();
		}
		String strtel=txtTel.getText().trim();
		String strcID=txtCustomerID.getText().trim();
		String stremail=txtEmail.getText().trim();
		String straddr=txtAddr.getText().trim();
		String strpc=txtPostalCode.getText().trim();
		  	
		if(strcID.equals(""))
		{
		  	strcID=null;	
		}
		if(stremail.equals(""))
		{
		  	stremail=null;	
		}
		if(straddr.equals(""))
		{
		  	straddr=null;	
		}
		if(strpc.equals(""))
		{
		  	strpc=null;	
		}
		  	
		cd.setCustomerCode(strcode);
		cd.setCustomerName(strcn);
		cd.setSex(strsex);
		cd.setTel(strtel);
		cd.setCustomerID(strcID);
		cd.setEmail(stremail);
		cd.setAddr(straddr);
		cd.setPostalCode(strpc);
		  	
		if(strcode==0||strcn.length()==0||strtel.length()==0)
		{
		  	JOptionPane.showMessageDialog(null,"带*号的必填,请填写完整","提示",JOptionPane.INFORMATION_MESSAGE);
		  	return false;
		}
		else if(strcode>9999&&strcode<0)
		{
		  	JOptionPane.showMessageDialog(null,"请输入4位以内整数","提示",JOptionPane.INFORMATION_MESSAGE);
		  	txtCode.setText("");
	 		txtCode.requestFocus();
	 		return false;
		}
		else if(straddr!=null&&straddr.length()>25)
		{
			JOptionPane.showMessageDialog(null,"字数过长","提示",JOptionPane.INFORMATION_MESSAGE);
			txtAddr.setText("");
		 	txtAddr.requestFocus();
		 	return false;
		}
		else if(!check.checkTel(strtel))
		{
		  	JOptionPane.showMessageDialog(null,"电话号码格式或位数不对","提示",JOptionPane.INFORMATION_MESSAGE);
		  	txtTel.setText("");
	 		txtTel.requestFocus();
	 		return false;	
		}
		else if(strpc!=null&&strpc.length()>6)
		{
			JOptionPane.showMessageDialog(null,"请输入6位以内正确的邮政编码","提示",JOptionPane.INFORMATION_MESSAGE);
			txtPostalCode.setText("");
		 	txtPostalCode.requestFocus();
		 	return false;	
		}
		else if(strcID!=null)
		{
		  	boolean bg=true;
			if(!(strcID.length()==15||strcID.length()==18))
			{
			  	JOptionPane.showMessageDialog(null,"请填写正确的身份证号码","提示",JOptionPane.INFORMATION_MESSAGE);
			  	txtCustomerID.setText("");
		 		txtCustomerID.requestFocus();
		 		bg=false;
			}
			return bg;
		 }
		 else if(stremail!=null)
		 {
		  	boolean bg=true;
			if(!check.checkEmail(stremail))
			{
			  	JOptionPane.showMessageDialog(null,"请输入20位以内正确的Email","提示",JOptionPane.INFORMATION_MESSAGE);
			  	txtEmail.setText("");
		 		txtEmail.requestFocus();
		 		bg=false;	
			}
			return bg;
		  		
		 }
		 else
		 {
		  	return true;
		 }
	  }
	  /*得到一行加入模型中的数据*/
	  public Vector dataVector()
	  {
	  	Vector vd=new Vector();
		vd.add(cd.getCustomerCode());
		vd.add(cd.getCustomerName());
		vd.add(cd.getSex());
		vd.add(cd.getTel());
		vd.add(cd.getCustomerID());
		vd.add(cd.getEmail());
		vd.add(cd.getAddr());
		vd.add(cd.getPostalCode());
		return vd;
	  }
	  /*修改表模型中的数据*/
	  public void changeRowData()
	  {
	  	tabShow.setValueAt(cd.getCustomerCode(),row,0);
	  	tabShow.setValueAt(cd.getCustomerName(),row,1);
	  	tabShow.setValueAt(cd.getSex(),row,2);
	  	tabShow.setValueAt(cd.getTel(),row,3);
	  	tabShow.setValueAt(cd.getCustomerID(),row,4);
	  	tabShow.setValueAt(cd.getEmail(),row,5);
	  	tabShow.setValueAt(cd.getAddr(),row,6);
	  	tabShow.setValueAt(cd.getPostalCode(),row,7);
	  }
}

⌨️ 快捷键说明

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