📄 vipmanagepanel.java
字号:
/**
* 该类用于创建一个会员管理面板, 用来显示并且操作会员信息
*/
package view.panel.vip;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import vo.VipChargeVo;
import vo.VipVo;
import control.vip.VipManagePanelListener;
import dao.common.DbException;
import dao.common.sql.VipSql;
import dao.vip.VipDao;
import dao.vip.impl.VipDaoImpl;
/**
* @author lulicheng
* @version 1.0
*/
public class VipManagePanel extends JPanel {
// =====该面板的两个主体面板=====
private JPanel buttonPanel; // 位于会员管理面板(ConsumerManagePanel)上部的按钮面板
private JSplitPane splitPane; // 位于会员管理面板(ConsumerManagePanel)下部的分割面板
// -----按钮面板组件-----
private JPanel inputPanel; // 输入区域
private JLabel inputLable; // 输入提示标签
private JTextField inputField; // 输入文本的区域
// 工具面板中查找按钮,充值按钮, 添加会员按钮, 修改会员按钮, 删除会员按钮, 换卡(补卡)按钮, 设置级别按钮,退出按钮
private JButton btnSearch, btnRecharge, btnAdd, btnModify, btnDelete,
btnChangeCard, btnExit;
// -----分割面板组件-----
private JScrollPane consumerInfoScroll; // 显示会员信息的滚动框
private JPanel rechargeRecPanel; // 会员充值记录面板
private JLabel rechargeRecLabel; // 会员充值标签
private JScrollPane rechargeRecScroll; // 显示会员充值记录的滚动框
// 位于滚动框中,用于显示会员信息,充值记录的表格。
private JTable consumerInfoTable, rechargeRecTable;
// 需要与其他界面交互的属性
String consumerName = "临时设立的客户";
private String[] selectedData;
/**
* 构造函数
*/
public VipManagePanel(JDialog dialog) {
this.setSize(860, 640);
this.setLayout(new BorderLayout());
this.add(buildButtonPanel(dialog), BorderLayout.NORTH);
this.add(buildSplitPane());
initialize();
}
/**
* @param selectedData
* the selectedData to set
*/
public void setSelectedData(String[] selectedData) {
this.selectedData = selectedData;
}
/**
* @return String[] the selectedData
*/
public String[] getSelectedData() {
return selectedData;
}
/*
* "会员编号", "会员姓名", "会员性别", "会员级别", "卡内余额", "会员卡状态", "联系手机", "会员积分", "会员生日",
* "注册日期"
*/
/**
* 构造buttonPanel
*
* @param panel
* @return JPanel buttonPanel
*/
public JPanel buildButtonPanel(JDialog dialog) {
if (buttonPanel == null) {
buttonPanel = new JPanel();
buttonPanel.setSize(850, 300);
inputPanel = new JPanel();
inputPanel.add(buildInputLabel());
inputPanel.add(buildInputField());
buttonPanel.add(inputPanel);
buttonPanel.add(buildButton("查找会员"));
buttonPanel.add(buildButton("会员充值", dialog));
buttonPanel.add(buildButton("添加会员", dialog));
buttonPanel.add(buildButton("修改信息"), dialog);
buttonPanel.add(buildButton("删除会员"));
buttonPanel.add(buildButton("退出", dialog));
}
return buttonPanel;
}
/**
* 调用该函数一次,就将构造一个通过参数命名的JButton
*
* @param name
* name of a button
* @return JButton button named 'name'
*/
public JButton buildButton(String name) {
JButton button = new JButton(name);
if(name.equals("查找会员")){
button.setToolTipText("当输入框内容为空,将搜索出所有会员");
}
button.addActionListener(new VipManagePanelListener(this));
return button;
}
/**
* 调用该函数一次,就将构造一个通过参数命名,且需要对上级Dialog进行操作的的JButton
*
* @param name
* name of a button
* @return JButton button named 'name'
*/
public JButton buildButton(String name, JDialog dialog) {
JButton button = new JButton(name);
button.addActionListener(new VipManagePanelListener(this, dialog));
return button;
}
public JLabel buildInputLabel() {
if (inputLable == null) {
inputLable = new JLabel("请输入会员名称或编号");
}
return inputLable;
}
public JTextField buildInputField() {
if (inputField == null) {
inputField = new JTextField(10);
}
return inputField;
}
/**
* 构造splitPane
*
* @param split
* @return JSplitPane splitPane
*/
public JSplitPane buildSplitPane() {
if (splitPane == null) {
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setSize(850, 340);
splitPane.setDividerLocation(0.8); // 设置初始化时上下分割的百分比
splitPane.setTopComponent(buildConsumerInfoScroll());
splitPane.setBottomComponent(buildRechargeRecPanel());
}
return splitPane;
}
/**
* 构建充值记录面板
*
* @param panel
* @return JPanel
*/
public JPanel buildRechargeRecPanel() {
if (rechargeRecPanel == null) {
rechargeRecPanel = new JPanel();
rechargeRecPanel.setLayout(new BorderLayout());
rechargeRecLabel = new JLabel("以下是客户 \"" + consumerName
+ "\" 的充值记录");
rechargeRecPanel.add(rechargeRecLabel, BorderLayout.NORTH);
rechargeRecPanel.add(buildRechargeRecScroll(rechargeRecScroll));
}
return rechargeRecPanel;
}
/**
* 构造rechargeRecScroll
*
* @param scrollPane
* @return JScrollPane
*/
public JScrollPane buildRechargeRecScroll(JScrollPane scrollPane) {
if (scrollPane == null) {
scrollPane = new JScrollPane(buildRechageRecTable());
}
return scrollPane;
}
/**
* 构造consumerInfoScroll
*
* @param scrollPane
* @return JScrollPane
*/
public JScrollPane buildConsumerInfoScroll() {
if (consumerInfoScroll == null) {
consumerInfoScroll = new JScrollPane(buildConsumerInfoTable());
}
return consumerInfoScroll;
}
/**
* 构造会员信息表
*
* @param table
* @return JTable
*/
public JTable buildConsumerInfoTable() {
if (consumerInfoTable == null) {
String[] names = { "会员编号", "会员姓名", "会员性别", "会员级别", "会员卡号", "卡内余额",
"会员卡状态", "联系手机", "会员积分", "会员生日", "注册时间" };
Object[][] data = {};
// 构造TableModel模型,不能被编辑
DefaultTableModel model = new DefaultTableModel(data, names) {
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
};
consumerInfoTable = new JTable(model);
// 给鼠标添加事件,传递表中选中的行
consumerInfoTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int row = consumerInfoTable.getSelectedRow();
int columns = consumerInfoTable.getColumnCount();
String[] data = new String[columns];
for (int i = 0; i < columns; i++) {
data[i] = consumerInfoTable.getValueAt(row, i)
.toString();
}
setSelectedData(data);
buildRecTableData();
}
});
}
consumerInfoTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
return consumerInfoTable;
}
/**
* 构造充值记录表
*
* @param table
* @return JTable
*/
public JTable buildRechageRecTable() {
if (rechargeRecTable == null) {
String[] names = { "充值日期", "充值金额", "实收金额", "收费操作员" };
Object[][] data = {};
// 构造TableModel模型,不能被编辑
DefaultTableModel model = new DefaultTableModel(data, names) {
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
};
rechargeRecTable = new JTable(model);
}
return rechargeRecTable;
}
/**
* 该方法用来构造管理面板下方的充值记录表 该方法通过在会员信息面板处单击表格的行触发
*/
public void buildRecTableData() {
VipDao dao = new VipDaoImpl();
Vector<VipChargeVo> v;
String[] strs = this.getSelectedData();
int vipId = Integer.parseInt(strs[0]);
v = dao.getRechargeRecord(VipSql.GET_RECHARGE_RECORD, vipId);
Iterator<VipChargeVo> iter = v.iterator();
VipChargeVo value = null;
JTable table = this.buildRechageRecTable();
DefaultTableModel model = (DefaultTableModel) table.getModel();
int rows = table.getRowCount();
for (int i = rows - 1; i >= 0; i--) {
model.removeRow(i);
}
rows = table.getRowCount();
while (iter.hasNext()) {
value = (VipChargeVo) iter.next();
Object[] data = { value.getChargeDate(), value.getChargeAmount(),
value.getReceiveAmount(), value.getOperatorName() };
model.addRow(data);
}
}
public void initialize() {
VipDao dao = new VipDaoImpl();
Vector<VipVo> v;
try {
// 通过判断传递进来的Sql语句进行判断,应该进行什么查询
v = dao.searchVipAll(VipSql.VIP_LIST);
Iterator<VipVo> iter = v.iterator();
if (iter == null) {
return;
}
VipVo value = null;
JTable table = buildConsumerInfoTable();
DefaultTableModel model = (DefaultTableModel) table.getModel();
int rows = table.getRowCount();
for (int i = rows - 1; i >= 0; i--) {
model.removeRow(i);
}
while (iter.hasNext()) {
value = (VipVo) iter.next();
Object[] data = {
new Integer(value.getVipCard().getVipId()),
value.getVipName(),
value.getVipSex(),
value.getVipCard().getVipLevel(),
value.getVipCard().getCardId(),
value.getVipCard().getRestMoney(),
value.getVipCard().getCardState(),
value.getVipPhone(),
value.getVipCard().getPoints(),
String.valueOf(value.getVipBirthday().substring(0, 10)),
String.valueOf(value.getVipCard().getRegistDate()
.substring(0, 10)) };
model.addRow(data);
}
} catch (DbException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "提示",
JOptionPane.YES_OPTION);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -