📄 suppliersdialog.java
字号:
/**
* 源文件:SuppliersDialog.java
* 作用:供货商信息窗体
*/
package mypro;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import javax.swing.event.*;
public class SuppliersDialog extends JDialog implements ActionListener,MouseListener
{
SuppliersData sd=new SuppliersData();
Check check=new Check();
private JLabel labSupplierBrief,labCompany,labTel,labFax,labContactName,labContactTel,labAddr;
private JTextField txtSupplierBrief,txtCompany,txtTel,txtFax,txtContactName,txtContactTel,txtAddr;
private JButton btnInsert,btnUpdate,btnDelete;
private JScrollPane scop;
private JTable tabShow;
private DefaultTableModel dtm;
private int row=0;
static
{
//设置窗体为豪华框架
JDialog.setDefaultLookAndFeelDecorated(true);
}
public SuppliersDialog(MainFrame owner,boolean bo)
{
super(owner,bo);
labSupplierBrief = new JLabel("*供贷商代码");
txtSupplierBrief = new JTextField();
labCompany = new JLabel("*公司名称");
txtCompany = new JTextField();
labTel = new JLabel("*公司电话");
txtTel = new JTextField();
labFax = new JLabel("传真号码");
txtFax = new JTextField();
labContactName = new JLabel("*联系人姓名");
txtContactName = new JTextField();
labContactTel = new JLabel("*联系人电话");
txtContactTel = new JTextField();
labAddr = new JLabel("公司地址");
txtAddr = new JTextField();
btnInsert = new JButton("插入");
btnUpdate = new JButton("修改");
btnDelete = new JButton("删除");
scop = new JScrollPane();
tabShow = new JTable();
labSupplierBrief.setBounds(new Rectangle(12, 16, 73, 14));
txtSupplierBrief.setBounds(new Rectangle(82, 16, 121, 23));
labCompany.setBounds(new Rectangle(12, 49, 73, 14));
txtCompany.setBounds(new Rectangle(82, 49, 121, 23));
labTel.setBounds(new Rectangle(12, 82, 73, 14));
txtTel.setBounds(new Rectangle(82, 82, 121, 23));
labAddr.setBounds(new Rectangle(12, 114, 73, 14));
txtAddr.setBounds(new Rectangle(82, 114, 224, 23));
labContactName.setBounds(new Rectangle(254, 18, 73, 14));
txtContactName.setBounds(new Rectangle(330, 18, 121, 23));
labContactTel.setBounds(new Rectangle(254, 49, 73, 14));
txtContactTel.setBounds(new Rectangle(330, 49, 121, 23));
labFax.setBounds(new Rectangle(254, 82, 73, 14));
txtFax.setBounds(new Rectangle(330, 82, 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, 445, 98));
btnInsert.addActionListener(this);
btnUpdate.addActionListener(this);
btnDelete.addActionListener(this);
tabShow.addMouseListener(this);
Container me=this.getContentPane();
me.setLayout(null);
me.add(labSupplierBrief);
me.add(txtSupplierBrief);
me.add(labCompany);
me.add(txtCompany);
me.add(labTel);
me.add(txtTel);
me.add(labFax);
me.add(txtFax);
me.add(labContactName);
me.add(txtContactName);
me.add(labContactTel);
me.add(txtContactTel);
me.add(labAddr);
me.add(txtAddr);
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("Suppliers"),datao.getColumnNamesByTabname("Suppliers"));
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 sae)
{
String strbtn=sae.getActionCommand();
if(strbtn.equals("Insert"))
{
String strsub=txtSupplierBrief.getText().trim();
if(check.checkPrimary("suppliers","供货商代码",strsub))
{
JOptionPane.showMessageDialog(null,"该供货商代码已存在","提示",JOptionPane.INFORMATION_MESSAGE);
txtSupplierBrief.setText("");
txtSupplierBrief.requestFocus();
}
else if(operationBeforeCheck())
{
InsertData ind =new InsertData();
Vector vid = dataVector();
if(ind.SuppliersInsert(sd))
{
JOptionPane.showMessageDialog(null,"成功插入一行数据","提示",JOptionPane.INFORMATION_MESSAGE);
dtm.addRow(vid);//添加一行到模型的结尾
}
else
{
JOptionPane.showMessageDialog(null,"数据插入失败","提示",JOptionPane.INFORMATION_MESSAGE);
}
}
}
else if(strbtn.equals("Update"))
{
String strsub1=tabShow.getValueAt(row,0).toString().trim();
String strsub=txtSupplierBrief.getText().trim();
if(!strsub.equals(strsub1))
{
if(check.checkPrimary("stockTel","供货商代码",strsub1)||check.checkPrimary("productsTel","供货商代码",strsub1))
{
JOptionPane.showMessageDialog(null,"该供货商代码被进货信息表和手机信息表引用,禁止更改","提示",JOptionPane.INFORMATION_MESSAGE);
txtSupplierBrief.setText(strsub1);
}
else if(check.checkUpdataAfterUnique("suppliers","供货商代码",strsub,strsub1))
{
JOptionPane.showMessageDialog(null,"该供货商代码已存在,无法修改","提示",JOptionPane.INFORMATION_MESSAGE);
txtSupplierBrief.setText(strsub1);
txtSupplierBrief.requestFocus();
}
}
else if(operationBeforeCheck())
{
UpdateData upd=new UpdateData();
if(upd.suppliersUpdate(sd,strsub1))
{
JOptionPane.showMessageDialog(null,"成功修改一行数据","提示",JOptionPane.INFORMATION_MESSAGE);
changeRowData();//修改表模型中的数据
}
else
{
JOptionPane.showMessageDialog(null,"数据修改失败","提示",JOptionPane.INFORMATION_MESSAGE);
}
}
}
else if(strbtn.equals("Delete"))
{
String strsubd=tabShow.getValueAt(row,0).toString().trim();
DeleteData ded=new DeleteData();
if(check.checkPrimary("stockTel","供货商代码",strsubd)||check.checkPrimary("productsTel","供货商代码",strsubd))
{
JOptionPane.showMessageDialog(null,"该供货商代码被进货信息表和手机信息表引用,请先删除或修改其他表中相关数据","提示",JOptionPane.INFORMATION_MESSAGE);
}
else
{
if(ded.deleteDate("suppliers","供货商代码",strsubd))
{
JOptionPane.showMessageDialog(null,"成功删除一行数据","提示",JOptionPane.INFORMATION_MESSAGE);
dtm.removeRow(row);//移除模型中 row 位置的行
}
else
{
JOptionPane.showMessageDialog(null,"数据删除失败","提示",JOptionPane.INFORMATION_MESSAGE);
}
}
}
txtSupplierBrief.setText("");
txtCompany.setText("");
txtTel.setText("");
txtFax.setText("");
txtContactName.setText("");
txtContactTel.setText("");
txtAddr.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 strsub=(String)tabShow.getValueAt(row,0);
String strcom=(String)tabShow.getValueAt(row,1);
String strtel=(String)tabShow.getValueAt(row,2);
String strfax=(String)tabShow.getValueAt(row,3);
String strcn=(String)tabShow.getValueAt(row,4);
String strct=(String)tabShow.getValueAt(row,5);
String straddr=(String)tabShow.getValueAt(row,6);
txtSupplierBrief.setText(strsub.trim());
txtCompany.setText(strcom.trim());
txtTel.setText(strtel.trim());
if(strfax==null)
{
txtFax.setText(strfax);
}
else
{
txtFax.setText(strfax.trim());
}
txtContactName.setText(strcn.trim());
txtContactTel.setText(strct.trim());
if(straddr==null)
{
txtAddr.setText(straddr);
}
else
{
txtAddr.setText(straddr.trim());
}
}
/* 执行操作前对数据进行验证*/
public boolean operationBeforeCheck()
{
String strsub=txtSupplierBrief.getText().trim();
String strcom=txtCompany.getText().trim();
String strtel=txtTel.getText().trim();
String straddr=txtAddr.getText().trim();
String strcn=txtContactName.getText().trim();
String strct=txtContactTel.getText().trim();
String strfax=txtFax.getText().trim();
if(straddr.equals(""))
{
straddr=null;
}
if(strfax.equals(""))
{
strfax=null;
}
sd.setSupplierBrief(strsub);
sd.setCompany(strcom);
sd.setTel(strtel);
sd.setFax(strfax);
sd.setContactName(strcn);
sd.setContactTel(strct);
sd.setAddr(straddr);
if(strsub.length()==0||strcom.length()==0||strtel.length()==0||strcn.length()==0||strct.length()==0)
{
JOptionPane.showMessageDialog(null,"带*号的必填,请填写完整","提示",JOptionPane.INFORMATION_MESSAGE);
return false;
}
else if(strsub.length()>10)
{
JOptionPane.showMessageDialog(null,"供货商代码在10位以内","提示",JOptionPane.INFORMATION_MESSAGE);
txtSupplierBrief.setText("");
txtSupplierBrief.requestFocus();
return false;
}
else if(!check.checkTel(strtel))
{
JOptionPane.showMessageDialog(null,"电话号码格式或位数不对","提示",JOptionPane.INFORMATION_MESSAGE);
txtTel.setText("");
txtTel.requestFocus();
return false;
}
else if(!check.checkTel(strct))
{
JOptionPane.showMessageDialog(null,"电话号码格式不对","提示",JOptionPane.INFORMATION_MESSAGE);
txtFax.setText("");
txtFax.requestFocus();
return false;
}
else if(strfax!=null&&strfax.length()>15)
{
JOptionPane.showMessageDialog(null,"传真号必须小于15位","提示",JOptionPane.INFORMATION_MESSAGE);
txtContactTel.setText("");
txtContactTel.requestFocus();
return false;
}
else if(straddr!=null&&straddr.length()>25)
{
JOptionPane.showMessageDialog(null,"字数过长","提示",JOptionPane.INFORMATION_MESSAGE);
txtAddr.setText("");
txtAddr.requestFocus();
return false;
}
else
{
return true;
}
}
/*得到一行加入模型中的数据*/
public Vector dataVector()
{
Vector vd=new Vector();
vd.add(sd.getSupplierBrief());
vd.add(sd.getCompany());
vd.add(sd.getTel());
vd.add(sd.getFax());
vd.add(sd.getContactName());
vd.add(sd.getContactTel());
vd.add(sd.getAddr());
return vd;
}
/*修改表模型中的数据*/
public void changeRowData()
{
tabShow.setValueAt(sd.getSupplierBrief(),row,0);
tabShow.setValueAt(sd.getCompany(),row,1);
tabShow.setValueAt(sd.getTel(),row,2);
tabShow.setValueAt(sd.getFax(),row,3);
tabShow.setValueAt(sd.getContactName(),row,4);
tabShow.setValueAt(sd.getContactTel(),row,5);
tabShow.setValueAt(sd.getAddr(),row,6);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -