📄 goodbuyfatherjpanel.java
字号:
package file1;
/*
* @Author:黄顺武
* Create Time:2008-2-17
* Description:对货品的进货情况进行录入显示的界面
*/
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class GoodBuyFatherJPanel extends JPanel implements ActionListener,
ItemListener {
private JLabel goodType = new JLabel("货品类型:");
private JComboBox goodTypeBox = new JComboBox();
private JLabel goodName = new JLabel("货品名称:");
private JComboBox goodNameBox = new JComboBox();
private JLabel numberBuy = new JLabel("进货数量:");
private JTextField numberBuyTF = new JTextField(10);
private JLabel timeBuy = new JLabel("进货时间:");
private JTextField timeBuyTF = new JTextField(10);
private String[] goodIDs = null;// 存储所有货品id的变量数组
private String[] goodBuyIDs = null;// 存储所有进货记录的变量数组
private String[] typeIDs = null;// 存储所有货品类型记录的变量数组
private String idModified = "";// 被修改的记录的id
private int numberBuyOld = 0;// 被修改的进货记录的进货数量
private JPanel p1 = new JPanel();
private JTable recTable = null;
private String[] head = { "ID", "货品类型", "货品名称", "进货数量", "进货时间" };
private int headNum = 0;
private String[][] data = null;
private JScrollPane recScrollPane = null;
private GetDate getD = new GetDate();
private AMDJPanel amdJPanel = null;// 含有增删改按钮的面版
public GoodBuyFatherJPanel() {
headNum = head.length;
amdJPanel = new AMDJPanel();
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));
p1.add(goodType);
p1.add(goodTypeBox);
p1.add(goodName);
p1.add(goodNameBox);
p1.add(numberBuy);
p1.add(numberBuyTF);
p1.add(timeBuy);
p1.add(timeBuyTF);
getAllGoodTypes();// 调用自定义函数初始化货品类型下拉框
getAllRecords();// 显示所有进货记录
goodTypeBox.addItemListener(this);
amdJPanel.getDeleteButton().addActionListener(this);
amdJPanel.getModifyButton().addActionListener(this);
amdJPanel.getAddButton().addActionListener(this);
}
private void getAllGoodTypes() {// 初始化货品类型下拉框
if (goodTypeBox.getItemCount() > 0) {
goodTypeBox.removeAllItems();
}
GoodType goodType = new GoodType();
goodType.setID(0);
GoodTypeOperate goodTypeOperate = new GoodTypeOperate();
ArrayList valuesGet = goodTypeOperate.selectFromDB(goodType);// 从数据库中查询所有货品类型记录
int total = valuesGet.size();// total为记录货品类型总数变量
typeIDs = new String[total];
total = 0;// 从开始位置给数组赋值
Iterator it = valuesGet.iterator();
for (int i = 0; i < valuesGet.size(); i++) {
GoodType gType = (GoodType) valuesGet.get(i);
typeIDs[total++] = String.valueOf(gType.getID());
goodTypeBox.addItem(gType.getTypeName());
}
goodTypeBox.setSelectedIndex(-1);// 初始状态置为空
}
private void getAllRecords() {// 从数据库中读取所有要显示的进货记录并显示在表格中
GoodBuy goodBuy = new GoodBuy();
goodBuy.setID(0);// 表示查询所有货品记录
GoodBuyOperate goodBuyOperate = new GoodBuyOperate();
HashMap valuesGet = goodBuyOperate.selectFromDB(goodBuy);
int total = valuesGet.size();// total为记录总数变量
if (total == 0) {// 数据库中没有货品记录
amdJPanel.getModifyButton().setEnabled(false);// 把"修改记录"按钮置为不可用
amdJPanel.getDeleteButton().setEnabled(false);// 把"删除记录"按钮置为不可用
} else {
amdJPanel.getModifyButton().setEnabled(true);// 把"修改记录"按钮置为可用
amdJPanel.getDeleteButton().setEnabled(true);// 把"删除记录"按钮置为可用
}
goodBuyIDs = new String[total];
data = new String[total][headNum];
total = 0;// 从开始位置给数组赋值
Set keySet = valuesGet.keySet();
Iterator it = keySet.iterator();
while (it.hasNext()) {
GoodBuy goodBuyTemp = (GoodBuy) valuesGet.get(it.next());
String id = String.valueOf(goodBuyTemp.getID());
goodBuyIDs[total] = id;
data[total][0] = id;
data[total][1] = (String) goodBuyTemp.getProperty("typeName");
data[total][2] = (String) goodBuyTemp.getProperty("goodName");
data[total][3] = String.valueOf(goodBuyTemp.getNumber_Buy());
data[total][4] = goodBuyTemp.getDate_Buy();
total++;
}
recTable = new JTable(data, head);
recScrollPane = new JScrollPane(recTable);
this.setLayout(new BorderLayout(0, 15));
this.add(p1, BorderLayout.NORTH);
this.add(recScrollPane, BorderLayout.CENTER);
this.add(amdJPanel, BorderLayout.SOUTH);
this.validate();
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == amdJPanel.getAddButton()) {// 点击了"增加记录"按钮
if (amdJPanel.getModifyButton().getActionCommand().equals("确认修改")) {// 在点击了"修改记录"按钮后没有在修改完记录后按下确认修改,则把该按钮的文本重新置为"修改记录"
amdJPanel.getModifyButton().setText("修改记录");
}
GoodBuy goodBuy = getGoodBuyRecordDataIn();
if (goodBuy == null) {
return;
}
GoodBuyOperate goodBuyOperate = new GoodBuyOperate();
goodBuyOperate.insertToDB(goodBuy);// 把记录插入到数据库中
getAllRecords();// 刷新页面
}
if (ae.getSource() == amdJPanel.getModifyButton()) {// 点击了"修改记录"按钮
if (amdJPanel.getModifyButton().getActionCommand().equals("修改记录")) {
idModified = (String) JOptionPane.showInputDialog(null,
"请您选择要修改的货品ID!", "", JOptionPane.INFORMATION_MESSAGE,
null, goodBuyIDs, goodBuyIDs[0]);
if (idModified == null
|| (idModified != null && idModified.trim().equals(""))) {
return;
}
GoodBuy goodBuy = new GoodBuy();
goodBuy.setID(Integer.valueOf(idModified));
GoodBuyOperate goodBuyOperate = new GoodBuyOperate();
HashMap valuesGet = goodBuyOperate.selectFromDB(goodBuy);
Set keySet = valuesGet.keySet();
Iterator it = keySet.iterator();
GoodBuy goodBuyGet = null;
while (it.hasNext()) {
goodBuyGet = (GoodBuy) valuesGet.get(it.next());
}
if (goodBuyGet != null) {
goodTypeBox.setSelectedItem(goodBuyGet
.getProperty("typeName"));
goodNameBox.setSelectedItem(goodBuyGet
.getProperty("goodName"));
numberBuyTF.setText(String.valueOf(goodBuyGet
.getNumber_Buy()));
numberBuyOld = goodBuyGet.getNumber_Buy();// 修改前的进货数量
timeBuyTF.setText(goodBuyGet.getDate_Buy());
}
amdJPanel.getModifyButton().setText("确认修改");
return;
}
if (amdJPanel.getModifyButton().getActionCommand().equals("确认修改")) {// 用户确认修改
GoodBuy goodBuy = getGoodBuyRecordDataIn();
goodBuy.setID(Integer.valueOf(idModified));// 被修改的记录的id
goodBuy.setProperty("difference", goodBuy.getNumber_Buy()
- numberBuyOld);// 修改前后的进货量的大小差别
GoodBuyOperate goodBuyOperate = new GoodBuyOperate();
goodBuyOperate.modifyToDB(goodBuy);// 执行修改
getAllRecords();// 刷新记录显示界面
amdJPanel.getModifyButton().setText("修改记录");
}
}
if (ae.getSource() == amdJPanel.getDeleteButton()) {// 点击了"删除记录"按钮
if (amdJPanel.getModifyButton().getActionCommand().equals("确认修改")) {// 在点击了"修改记录"按钮后没有在修改完记录后按下确认修改,则把该按钮的文本重新置为"修改记录"
amdJPanel.getModifyButton().setText("修改记录");
}
String goodBuyID = (String) JOptionPane.showInputDialog(null,
"请您选择要删除的货品ID!", "", JOptionPane.INFORMATION_MESSAGE, null,
goodBuyIDs, goodBuyIDs[0]);
if (goodBuyID == null) {
return;
}
int confirm = JOptionPane.showConfirmDialog(null, "您真的确认删除吗?", "",
JOptionPane.INFORMATION_MESSAGE);
if (confirm == JOptionPane.YES_OPTION) {
GoodBuy goodBuy = new GoodBuy();
goodBuy.setID(Integer.valueOf(goodBuyID));
GoodBuyOperate goodBuyOperate = new GoodBuyOperate();
GoodBuy goodBuyNew = null;
HashMap valuesGet = goodBuyOperate.selectFromDB(goodBuy);
Set keySet = valuesGet.keySet();
Iterator it = keySet.iterator();
while (it.hasNext()) {
goodBuyNew = (GoodBuy) valuesGet.get(it.next());
}
goodBuyOperate.deleteFromDB(goodBuyNew);
getAllRecords();// 刷新记录显示界面
}
}
}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == goodTypeBox) {
int index = goodTypeBox.getSelectedIndex();
if (index == -1) {// 没有选择
return;
}
Good good = new Good();
good.setTypeID(Integer.valueOf(typeIDs[index]));
GoodOperate goodOperate = new GoodOperate();
ArrayList valuesGet = goodOperate.selectFromDB(good);// 从数据库中查询所有该类型的记录
int total = valuesGet.size();// total为记录总数变量
goodIDs = new String[total];
total = 0;// 计数重新置零
Iterator it = valuesGet.iterator();
for (int i = 0; i < valuesGet.size(); i++) {
Good goodTemp = (Good) valuesGet.get(i);
goodIDs[total++] = String.valueOf(goodTemp.getID());
goodNameBox.addItem(goodTemp.getGoodName());
}
goodNameBox.setSelectedIndex(-1);// 初始状态为空
}
}
private GoodBuy getGoodBuyRecordDataIn() {// 从货品录入录入窗口中读取货品记录数据
int index = goodNameBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "进货的货品不能为空!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
int numberBuy = -1;
try {
numberBuy = Integer.valueOf(numberBuyTF.getText().trim());
if (numberBuy <= 0) {
JOptionPane.showMessageDialog(null, "进货数量必须为大于零的整数!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "进货数量必须大于零的整数!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
String dateGet = getD.getDate(timeBuyTF.getText().trim());
if (dateGet == null) {
JOptionPane.showMessageDialog(null, "进货时间必须为如2008-2-17的格式!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
GoodBuy goodBuy = new GoodBuy();
goodBuy.setGood_ID(Integer.valueOf(goodIDs[index]));
goodBuy.setNumber_Buy(numberBuy);
goodBuy.setDate_Buy(dateGet);
return goodBuy;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -