📄 goodfatherjpanel.java
字号:
package file1;
/*
* @Author:黄顺武
* Create Time:2008-2-16
* Description:对货品进行显示和录入等操作的界面
*/
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GoodFatherJPanel extends JPanel implements ActionListener {
private JLabel type = new JLabel("货品类型:");
private JComboBox typeBox = new JComboBox();
private JLabel goodName = new JLabel("货品名称:");
private JTextField goodNameTF = new JTextField(10);
private JLabel priceIn = new JLabel("进货价格:");
private JTextField priceInTF = new JTextField(10);
private JLabel priceOut = new JLabel("卖出价格:");
private JTextField priceOutTF = new JTextField(10);
private JLabel storeInLimit = new JLabel("最低库存量:");
private JTextField storeInLimitTF = new JTextField(10);
private String[] typeIDs = null;// 存储所有货品类型的id
private String[] goodIDs = null;// 存储所有货品的id
private String idModified = "";// 被修改的记录的id
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 AMDJPanel amdJPanel = null;// 含有增删改按钮的面版
public GoodFatherJPanel() {
headNum = head.length;
amdJPanel = new AMDJPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 0));
p1.add(type);
p1.add(typeBox);
p1.add(goodName);
p1.add(goodNameTF);
p1.add(priceIn);
p1.add(priceInTF);
p1.add(priceOut);
p1.add(priceOutTF);
p1.add(storeInLimit);
p1.add(storeInLimitTF);
getAllTypes();// 调用函数初始化货品类型下拉框
getAllRecords();// 调用函数从数据库中查询所有货品记录显示到指定的表格中
amdJPanel.getDeleteButton().addActionListener(this);
amdJPanel.getModifyButton().addActionListener(this);
amdJPanel.getAddButton().addActionListener(this);
}
private void getAllTypes() {// 从数据库中读取所有货品类型并赋给货品类型下拉框
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 goodTypeTemp = (GoodType) valuesGet.get(i);
typeBox.addItem(goodTypeTemp.getTypeName());
typeIDs[total++] = String.valueOf(goodTypeTemp.getID());
}
typeBox.setSelectedIndex(-1);// 使选择框初始选择状态为空
}
private void getAllRecords() {// 从数据库中读取所有要显示的货品记录并显示在表格中
Good good = new Good();
good.setID(0);// 表示查询所有货品记录
GoodOperate goodOperate = new GoodOperate();
ArrayList valuesGet = goodOperate.selectFromDB(good);
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);// 把"删除记录"按钮置为可用
}
goodIDs = new String[total];
data = new String[total][headNum];
total = 0;// 从开始位置给数组赋值
Iterator it = valuesGet.iterator();
for (int i = 0; i < valuesGet.size(); i++) {
Good goodTemp = (Good) valuesGet.get(i);
String id = String.valueOf(goodTemp.getID());
goodIDs[total] = id;
data[total][0] = id;
data[total][1] = (String) goodTemp.getProperty("typeName");
data[total][2] = goodTemp.getGoodName();
data[total][3] = String.valueOf(goodTemp.getSinglePriceIn());
data[total][4] = String.valueOf(goodTemp.getSinglePriceOut());
data[total][5] = String.valueOf(goodTemp.getStoreIn());
data[total][6] = String.valueOf(goodTemp.getStoreInLimit());
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("修改记录");
}
Good good = getGoodRecordDataIn();
if (good == null) {
return;
}
GoodOperate goodOperate = new GoodOperate();
goodOperate.insertToDB(good);
getAllRecords();// 刷新记录显示界面
}
if (ae.getSource() == amdJPanel.getModifyButton()) {// 点击了"修改记录"按钮
if (amdJPanel.getModifyButton().getActionCommand().equals("修改记录")) {
idModified = (String) JOptionPane.showInputDialog(null,
"请您选择要修改的货品ID!", "", JOptionPane.INFORMATION_MESSAGE,
null, goodIDs, goodIDs[0]);
if (idModified == null
|| (idModified != null && idModified.trim().equals(""))) {
return;
}
Good good = new Good();
good.setID(Integer.valueOf(idModified));
GoodOperate goodOperate = new GoodOperate();
ArrayList valuesGet = goodOperate.selectFromDB(good);
Iterator it = valuesGet.iterator();
Good goodGet = null;
int i = 0;
while (it.hasNext()) {
goodGet = (Good) valuesGet.get(i++);
}
if (goodGet != null) {
typeBox.setSelectedItem(goodGet.getProperty("typeName"));
goodNameTF.setText(goodGet.getGoodName());
priceInTF.setText(String
.valueOf(goodGet.getSinglePriceIn()));
priceOutTF.setText(String.valueOf(goodGet
.getSinglePriceOut()));
storeInLimitTF.setText(String.valueOf(goodGet
.getStoreInLimit()));
}
amdJPanel.getModifyButton().setText("确认修改");
return;
}
if (amdJPanel.getModifyButton().getActionCommand().equals("确认修改")) {// 用户确认修改
Good good = getGoodRecordDataIn();
good.setID(Integer.valueOf(idModified));// 被修改的记录的id
GoodOperate goodOperate = new GoodOperate();
goodOperate.modifyToDB(good);// 执行修改
getAllRecords();// 刷新记录显示界面
amdJPanel.getModifyButton().setText("修改记录");
}
}
if (ae.getSource() == amdJPanel.getDeleteButton()) {// 点击了"删除记录"按钮
if (amdJPanel.getModifyButton().getActionCommand().equals("确认修改")) {// 在点击了"修改记录"按钮后没有在修改完记录后按下确认修改,则把该按钮的文本重新置为"修改记录"
amdJPanel.getModifyButton().setText("修改记录");
}
String goodID = (String) JOptionPane.showInputDialog(null,
"请您选择要删除的货品ID!", "", JOptionPane.INFORMATION_MESSAGE, null,
goodIDs, goodIDs[0]);
if (goodID == null) {
return;
}
int confirm = JOptionPane.showConfirmDialog(null, "您真的确认删除吗?", "",
JOptionPane.INFORMATION_MESSAGE);
if (confirm == JOptionPane.YES_OPTION) {
Good good = new Good();
good.setID(Integer.valueOf(goodID));
GoodOperate goodOperate = new GoodOperate();
goodOperate.deleteFromDB(good);
getAllRecords();// 刷新记录显示界面
}
}
}
private Good getGoodRecordDataIn() {// 从货品录入录入窗口中读取货品记录数据
int index = typeBox.getSelectedIndex();
if (index == -1) {// 没有选择货品类型
JOptionPane.showMessageDialog(null, "请选择货品的类型!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
String goodName = goodNameTF.getText().trim();
if (("").equals(goodName)) {// 货品名称为空
JOptionPane.showMessageDialog(null, "货品名称不能为空!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
float priceIn = -1;
try {
priceIn = Float.valueOf(priceInTF.getText().trim());
if (priceIn <= 0) {
JOptionPane.showMessageDialog(null, "进货价格必须为大于零的整数或小数!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
} catch (NumberFormatException cnfe) {
JOptionPane.showMessageDialog(null, "进货价格必须为大于零的整数或小数!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
float priceOut = -1;
try {
priceOut = Float.valueOf(priceOutTF.getText().trim());
if (priceOut <= 0) {
JOptionPane.showMessageDialog(null, "卖出价格必须为大于零的整数或小数!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
} catch (NumberFormatException cnfe) {
JOptionPane.showMessageDialog(null, "卖出价格必须为大于零的整数或小数!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
int storeInLimit = -1;
try {
storeInLimit = Integer.valueOf(storeInLimitTF.getText().trim());
if (storeInLimit < 0) {
JOptionPane.showMessageDialog(null, "货品的最低库存量必须为大于零的整数!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
} catch (NumberFormatException cnfe) {
JOptionPane.showMessageDialog(null, "货品的最低库存量必须为大于零的整数!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
Good good = new Good();
good.setTypeID(Integer.valueOf(typeIDs[index]));
good.setGoodName(goodName);
good.setSinglePriceIn(priceIn);
good.setSinglePriceOut(priceOut);
good.setStoreInLimit(storeInLimit);
return good;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -