|
|
package com.lingnan.supermarket.dialog; // 声明当前类所在的包
|
|
|
|
|
|
import java.awt.Container; // 导入Container类,用于包含和布局组件
|
|
|
import java.awt.FlowLayout; // 导入FlowLayout类,用于流式布局管理
|
|
|
import java.awt.event.ActionEvent; // 导入ActionEvent类,用于处理按钮点击等动作事件
|
|
|
import java.awt.event.ActionListener; // 导入ActionListener接口,用于监听和处理动作事件
|
|
|
|
|
|
import javax.swing.JButton; // 导入JButton类,用于创建按钮
|
|
|
import javax.swing.JDialog; // 导入JDialog类,用于创建对话框
|
|
|
import javax.swing.JFrame; // 导入JFrame类,用于创建主窗口框架
|
|
|
import javax.swing.JLabel; // 导入JLabel类,用于创建标签
|
|
|
import javax.swing.JOptionPane; // 导入JOptionPane类,用于显示对话框
|
|
|
import javax.swing.JPanel; // 导入JPanel类,用于创建面板容器
|
|
|
import javax.swing.JTextField; // 导入JTextField类,用于创建文本输入框
|
|
|
|
|
|
import com.lingnan.supermarket.*; // 导入lingnan.supermarket包下的所有类
|
|
|
import com.lingnan.supermarket.dao.SupplierInfService; // 导入SupplierInfService接口,用于供应商信息数据访问
|
|
|
import com.lingnan.supermarket.dao.impl.SupplierInfImpl; // 导入SupplierInfImpl类,实现SupplierInfService接口
|
|
|
import com.lingnan.supermarket.dto.SupplierInf; // 导入SupplierInf类,用于表示供应商信息数据传输对象
|
|
|
import com.lingnan.supermarket.view.SupplierView; // 导入SupplierView类,用于显示供应商信息视图
|
|
|
|
|
|
|
|
|
public class SupplierInfDialog extends JDialog implements ActionListener { // 定义一个对话框类SupplierInfDialog,继承自JDialog并实现ActionListener接口
|
|
|
|
|
|
private JPanel namePanel, addressPanel, contactPanel, emailPanel, opePanel; // 声明面板变量,用于不同信息的布局
|
|
|
|
|
|
private JLabel nameLabel, addressLabel, contactLabel, emailLabel; // 声明标签变量,用于显示文本信息
|
|
|
private JTextField nameTF, addressTF, contactTF, emailTF; // 声明文本框变量,用于输入信息
|
|
|
|
|
|
private JButton saveBtn, cancelBtn; // 声明按钮变量,用于保存和取消操作
|
|
|
|
|
|
private SupplierInfService supplierInfService = new SupplierInfImpl(); // 创建供应商信息服务实例
|
|
|
|
|
|
private SupplierView supplierView; // 声明供应商视图变量
|
|
|
|
|
|
private SupplierInf supplierInf; // 声明供应商信息变量
|
|
|
|
|
|
// 构造方法,接收父窗口和供应商视图作为参数
|
|
|
public SupplierInfDialog(JFrame parent, SupplierView supplierView) {
|
|
|
super(parent, "添加"); // 调用父类构造方法,设置对话框标题为"添加"
|
|
|
this.supplierView = supplierView; // 初始化供应商视图
|
|
|
|
|
|
setSize(350, 300); // 设置对话框大小
|
|
|
|
|
|
setLocationRelativeTo(null); // 设置对话框相对于屏幕居中
|
|
|
|
|
|
setModal(true); // 设置对话框为模态
|
|
|
setResizable(false); // 设置对话框不可调整大小
|
|
|
|
|
|
this.setLayout(new FlowLayout()); // 设置对话框布局为流式布局
|
|
|
|
|
|
initView(); // 初始化视图
|
|
|
}
|
|
|
|
|
|
|
|
|
private void initView() { // 初始化视图的私有方法
|
|
|
namePanel = new JPanel(); // 创建名称的面板
|
|
|
nameLabel = new JLabel("名称"); // 创建名称的标签
|
|
|
nameTF = new JTextField(15); // 创建名称的文本框,宽度为15
|
|
|
namePanel.add(nameLabel); // 将名称标签添加到名称面板
|
|
|
namePanel.add(nameTF); // 将名称文本框添加到名称面板
|
|
|
|
|
|
addressPanel = new JPanel(); // 创建地址的面板
|
|
|
addressLabel = new JLabel("地址"); // 创建地址的标签
|
|
|
addressTF = new JTextField(15); // 创建地址的文本框,宽度为15
|
|
|
addressPanel.add(addressLabel); // 将地址标签添加到地址面板
|
|
|
addressPanel.add(addressTF); // 将地址文本框添加到地址面板
|
|
|
|
|
|
contactPanel = new JPanel(); // 创建联系方式的面板
|
|
|
contactLabel = new JLabel("电话"); // 创建联系方式的标签
|
|
|
contactTF = new JTextField(15); // 创建联系方式的文本框,宽度为15
|
|
|
contactPanel.add(contactLabel); // 将联系方式标签添加到联系方式面板
|
|
|
contactPanel.add(contactTF); // 将联系方式文本框添加到联系方式面板
|
|
|
|
|
|
emailPanel = new JPanel(); // 创建邮箱的面板
|
|
|
emailLabel = new JLabel("邮箱"); // 创建邮箱的标签
|
|
|
emailTF = new JTextField(15); // 创建邮箱的文本框,宽度为15
|
|
|
emailPanel.add(emailLabel); // 将邮箱标签添加到邮箱面板
|
|
|
emailPanel.add(emailTF); // 将邮箱文本框添加到邮箱面板
|
|
|
|
|
|
opePanel = new JPanel(); // 创建操作按钮的面板
|
|
|
saveBtn = new JButton("保存"); // 创建保存按钮
|
|
|
cancelBtn = new JButton("取消"); // 创建取消按钮
|
|
|
saveBtn.addActionListener(this); // 为保存按钮添加动作监听器
|
|
|
cancelBtn.addActionListener(this); // 为取消按钮添加动作监听器
|
|
|
opePanel.add(saveBtn); // 将保存按钮添加到操作面板
|
|
|
opePanel.add(cancelBtn); // 将取消按钮添加到操作面板
|
|
|
|
|
|
Container container = getContentPane(); // 获取对话框的内容面板
|
|
|
container.add(namePanel); // 将名称面板添加到内容面板
|
|
|
container.add(addressPanel); // 将地址面板添加到内容面板
|
|
|
container.add(contactPanel); // 将联系方式面板添加到内容面板
|
|
|
container.add(emailPanel); // 将邮箱面板添加到内容面板
|
|
|
container.add(opePanel); // 将操作按钮面板添加到内容面板
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) { // 实现ActionListener接口的actionPerformed方法
|
|
|
Object source = e.getSource(); // 获取事件源
|
|
|
if (source == saveBtn) { // 如果事件源是保存按钮
|
|
|
// 思路获取数据
|
|
|
// 保存到数据库
|
|
|
// 关闭对话框
|
|
|
// 刷新table
|
|
|
|
|
|
String name = nameTF.getText(); // 获取名称文本框的内容
|
|
|
String address = addressTF.getText(); // 获取地址文本框的内容
|
|
|
String contact = contactTF.getText(); // 获取联系方式文本框的内容
|
|
|
String email = emailTF.getText(); // 获取邮箱文本框的内容
|
|
|
|
|
|
// TODO 参数校验
|
|
|
if (this.supplierInf == null) { // 如果supplierInf为null,表示是添加新供应商
|
|
|
SupplierInf supplierInf = new SupplierInf(); // 创建新的供应商信息对象
|
|
|
supplierInf.setName(name); // 设置供应商名称
|
|
|
supplierInf.setAddress(address); // 设置供应商地址
|
|
|
supplierInf.setContact(contact); // 设置供应商联系方式
|
|
|
supplierInf.setEmail(email); // 设置供应商邮箱
|
|
|
int result = supplierInfService.addSupplierInf(supplierInf); // 调用服务层添加供应商信息
|
|
|
// int result = 1; // 假设添加成功
|
|
|
if (result == 1) { // 如果添加成功
|
|
|
|
|
|
JOptionPane.showMessageDialog(this, "添加成功", "提示", // 显示添加成功的消息对话框
|
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
|
this.dispose(); // 关闭对话框
|
|
|
} else { // 如果添加失败
|
|
|
JOptionPane.showMessageDialog(this, "添加失败", "提示", // 显示添加失败的消息对话框
|
|
|
JOptionPane.ERROR_MESSAGE);
|
|
|
}
|
|
|
}/*else{ // 如果supplierInf不为null,表示是更新供应商信息
|
|
|
//更新
|
|
|
SupplierInf supplierInf= new SupplierInf(); // 创建新的供应商信息对象
|
|
|
supplierInf.setName(name); // 设置供应商名称
|
|
|
supplierInf.setAddress(address); // 设置供应商地址
|
|
|
supplierInf.setContact(contact); // 设置供应商联系方式
|
|
|
supplierInf.setId(this.supplierInf.getId()); // 设置供应商ID
|
|
|
|
|
|
int result = supplierInfService.updateSupplierInf(supplierInf); // 调用服务层更新供应商信息
|
|
|
if(result==1){ // 如果更新成功
|
|
|
JOptionPane.showMessageDialog(this, "更新成功", "提示", // 显示更新成功的消息对话框
|
|
|
JOptionPane.INFORMATION_MESSAGE);
|
|
|
}
|
|
|
}*/
|
|
|
|
|
|
} else if (source == cancelBtn) { // 如果事件源是取消按钮
|
|
|
|
|
|
this.dispose(); // 关闭对话框
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|