📄 goodsmanageframe.java
字号:
jTextField6.setText(goods[selectedIndex][1]);
//显示商品名称
jTextField7.setText(goods[selectedIndex][2]);
//显示商品别名
jTextField8.setText(goods[selectedIndex][3]);
//显示商品助记码
jTextField9.setText(goods[selectedIndex][4]);
//显示商品拼音码
jTextField10.setText(goods[selectedIndex][5]);
//显示计量单位
jTextField11.setText(goods[selectedIndex][6]);
//显示规格
jTextField12.setText(goods[selectedIndex][7]);
//显示生产厂商
jTextField13.setText(goods[selectedIndex][8]);
}
//清空单个商品显示的方法
public void clearGood(){
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
jTextField8.setText("");
jTextField9.setText("");
jTextField10.setText("");
jTextField11.setText("");
jTextField12.setText("");
jTextField13.setText("");
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
exit();
}
}
//退出方法
public void exit(){
//隐藏窗口
this.setVisible(false);
//清空数组的内容
categories = new String[0][4];
goods = new String[0][13];
//清空列表框的内容
listData1.clear();
listData2.clear();
//清空文本框的内容s
jTextArea1.setText("");
//取得面板上的所有控件
Component[] components = contentPane.getComponents();
//创建临时文本框控件
JTextField tmpTextField = new JTextField();
for(int i = 0; i < components.length; i++){
if(components[i].getClass().getName().equals("javax.swing.JTextField")){
tmpTextField = (JTextField)components[i];
//清空编辑框的内容
tmpTextField.setText("");
}
}
}
//设置用户的方法
public void setUser(User user) {
this.user = user;
}
//检查商品类别按钮的状态
public void checkCategoryBtn(boolean isManipulated){
if(isManipulated){
jButton2.setEnabled(false);
jButton3.setEnabled(false);
jButton4.setEnabled(false);
jButton5.setEnabled(true);
jButton6.setEnabled(true);
}else{
jButton2.setEnabled(true);
jButton3.setEnabled(true);
jButton4.setEnabled(true);
jButton5.setEnabled(false);
jButton6.setEnabled(false);
}
}
//检查商品按钮的状态
public void checkGoodBtn(boolean isManipulated){
if(isManipulated){
jButton8.setEnabled(false);
jButton9.setEnabled(false);
jButton10.setEnabled(false);
jButton11.setEnabled(true);
jButton12.setEnabled(true);
}else{
jButton8.setEnabled(true);
jButton9.setEnabled(true);
jButton10.setEnabled(true);
jButton11.setEnabled(false);
jButton12.setEnabled(false);
}
}
//事件单击方法
public void actionPerformed(ActionEvent e) {
//取得按钮的动作字符串
String actionCommand = e.getActionCommand().trim();
//进行商品类别的修改和删除时检查商品类别的选择状态
if(actionCommand.equals("updateCategory") | actionCommand.equals("deleteCategory")){
if(jList1.isSelectionEmpty()){
JOptionPane.showMessageDialog(null, "请选择商品类别.");
return;
}
}
//进行商品的修改和删除时检查商品的选择状态
if(actionCommand.equals("updateGoods") | actionCommand.equals("deleteGoods")){
if(jList2.isSelectionEmpty()){
JOptionPane.showMessageDialog(null, "请选择商品.");
return;
}
}
//单击按钮的处理代码
if (actionCommand.equals("showCategory")) {
this.showAllCategories();
}else if(actionCommand.equals("createCategory")){
this.checkCategoryBtn(true);
this.clearCategory();
jTextField2.setText("0");
action = "createCategory";
}else if(actionCommand.equals("updateCategory")){
this.checkCategoryBtn(true);
action = "updateCategory";
}else if(actionCommand.equals("deleteCategory")){
this.checkCategoryBtn(true);
action = "deleteCategory";
}else if(actionCommand.equals("okCategory")){
//取得类别的值
String categoryIdStr = jTextField1.getText();
String parentIdStr = jTextField2.getText();
int parentId = Integer.parseInt(parentIdStr);
String categoryName = jTextField3.getText().trim();
String categoryDescription = jTextArea1.getText().trim();
if(action.equals("createCategory") || action.equals("updateCategory")){
if(categoryName.length() == 0){
JOptionPane.showMessageDialog(null, "商品类别名称不能为空.");
return;
}
}
if(action.equals("createCategory")){
int result = stockManagementData.createGoodsCategory(parentId,
categoryName, categoryDescription);
if(result == 1){
//重新取得类别数据
this.showAllCategories();
}else{
JOptionPane.showMessageDialog(null, "创建类别(" + categoryName + ")失败.");
}
}else if(action.equals("updateCategory")){
int categoryId = Integer.parseInt(categoryIdStr);
int result = stockManagementData.updateGoodsCategory(categoryId, parentId,
categoryName, categoryDescription);
if(result == 1){
//取得当前选择项的位置
int selectedIndex = jList1.getSelectedIndex();
//重新取得类别数据
this.showAllCategories();
jList1.setSelectedIndex(selectedIndex);
}else{
JOptionPane.showMessageDialog(null, "创建类别(" + categoryName + ")失败.");
}
}else if(action.equals("deleteCategory")){
int categoryId = Integer.parseInt(categoryIdStr);
int result = stockManagementData.deleteGoodsCategory(categoryId);
if(result == 1){
//重新取得类别数据
this.showAllCategories();
}else{
JOptionPane.showMessageDialog(null, "删除类别(" + categoryName + ")失败,"
+ "请检查该类别是否有商品数据.");
}
}
this.checkCategoryBtn(false);
}else if(actionCommand.equals("cancelCategory")){
this.jList1_valueChanged(null);
this.checkCategoryBtn(false);
}else if(actionCommand.equals("search")){
//取得查询编辑框的内容
String searchValue = jTextField4.getText().trim();
//取得查询选项
int selectedIndex = jComboBox1.getSelectedIndex();
if(searchValue.length() == 0){
JOptionPane.showMessageDialog(null, "请输入查询值");
return;
}
switch (selectedIndex) {
case 0:
goods = stockManagementData.getGoodsByGoodsBarCode(searchValue);
this.showSearchGoods();
break;
case 1:
goods = stockManagementData.getGoodsByGoodsName(searchValue);
this.showSearchGoods();
break;
case 2:
goods = stockManagementData.getGoodsByProducer(searchValue);
this.showSearchGoods();
break;
}
}else if(actionCommand.equals("createGoods")){
this.checkGoodBtn(true);
this.clearGood();
action = "createGoods";
}else if(actionCommand.equals("updateGoods")){
this.checkGoodBtn(true);
action = "updateGoods";
}else if(actionCommand.equals("deleteGoods")){
this.checkGoodBtn(true);
action = "deleteGoods";
}else if(actionCommand.equals("okGoods")){
//取得商品的值
String goodsBarCode = jTextField5.getText().trim();
String categoryIdStr = jTextField6.getText().trim();
int categoryId = 0;
String goodsName = jTextField7.getText().trim();
String goodsNickName = jTextField8.getText().trim();
String goodsAssistantName = jTextField9.getText().trim();
String goodsPYName = jTextField10.getText().trim();
String unit = jTextField11.getText().trim();
String specification = jTextField12.getText().trim();
String producer = jTextField13.getText().trim();
if(action.equals("createGoods") || action.equals("updateGoods")){
if (goodsBarCode.length() == 0 | goodsName.length() == 0 |
categoryIdStr.length() == 0) {
JOptionPane.showMessageDialog(null, "商品条形码、商品名称、商品分类标识不能为空.");
return;
}
if(dataMethod.checkInt(categoryIdStr) == 0){
JOptionPane.showMessageDialog(null, "商品分类标识必须是整数,请检查.");
return;
}else{
categoryId = Integer.parseInt(categoryIdStr);
}
//检查商品类别标识与否存在
boolean isExited = false;
for(int i = 0; i < categories.length; i++){
if(categoryId == Integer.parseInt(categories[i][0])){
isExited = true;
break;
}
}
if(!isExited){
JOptionPane.showMessageDialog(null, "该类别标识不存在,请检查相应的类别标识");
return;
}
}
if(action.equals("createGoods")){
//创建商品
int result = stockManagementData.createGoods(goodsBarCode, categoryId,
goodsName, goodsNickName, goodsAssistantName, goodsPYName, unit,
specification, producer, 0, 0, 0, 1);
if(result == 1){
//添加商品列表框的数据
listData2.addElement(goodsBarCode);
//更改商品数组的数据
String[][] tempStrs = new String[goods.length + 1][13];
System.arraycopy(goods, 0, tempStrs, 0, goods.length);
tempStrs[goods.length][0] = goodsBarCode;
tempStrs[goods.length][1] = categoryIdStr;
tempStrs[goods.length][2] = goodsName;
tempStrs[goods.length][3] = goodsNickName;
tempStrs[goods.length][4] = goodsAssistantName;
tempStrs[goods.length][5] = goodsPYName;
tempStrs[goods.length][6] = unit;
tempStrs[goods.length][7] = specification;
tempStrs[goods.length][8] = producer;
tempStrs[goods.length][9] = "0";
tempStrs[goods.length][10] = "0";
tempStrs[goods.length][11] = "0";
tempStrs[goods.length][12] = "1";
goods = tempStrs;
//选择新添加的商品
jList2.setSelectedIndex(goods.length -1);
}else{
JOptionPane.showMessageDialog(null, "创建商品(" + goodsName + ")失败.");
}
}else if(action.equals("updateGoods")){
int selectedIndex = jList2.getSelectedIndex();
//取得商品数组其余4个数据
int upperLimit = Integer.parseInt(goods[selectedIndex][9]);
int lowerLimit = Integer.parseInt(goods[selectedIndex][10]);
double salePrice = Double.parseDouble(goods[selectedIndex][11]);
double discount = Double.parseDouble(goods[selectedIndex][12]);
//更新商品
int result = stockManagementData.updateGoods(goodsBarCode, categoryId,
goodsName, goodsNickName, goodsAssistantName, goodsPYName, unit,
specification, producer, upperLimit, lowerLimit, salePrice, discount);
if(result == 1){
//更新商品数组
goods[selectedIndex][1] = categoryIdStr;
goods[selectedIndex][2] = goodsName;
goods[selectedIndex][3] = goodsNickName;
goods[selectedIndex][4] = goodsAssistantName;
goods[selectedIndex][5] = goodsPYName;
goods[selectedIndex][6] = unit;
goods[selectedIndex][7] = specification;
goods[selectedIndex][8] = producer;
}else{
JOptionPane.showMessageDialog(null, "更新商品(" + goodsName + ")失败.");
}
}else if(action.equals("deleteGoods")){
//删除商品
int result = stockManagementData.deleteGoods(goodsBarCode);
if(result == 1){
int selectedIndex = jList2.getSelectedIndex();
//删除商品列表框的数据
listData2.removeElementAt(selectedIndex);
//更改商品数组的数据
String[][] tempStrs = new String[goods.length -1][13];
int line = 0;
for(int i = 0; i < goods.length; i++){
if(i == selectedIndex){
continue;
}else{
for(int j = 0; j < 13; j++){
tempStrs[line][j] = goods[i][j];
}
line++;
}
}
goods = tempStrs;
//清空商品编辑框的值
this.clearGood();
}else{
JOptionPane.showMessageDialog(null, "删除类别(" + goodsName + ")失败,"
+ "请检查该类别是否有商品数据.");
}
}
this.checkGoodBtn(false);
}else if(actionCommand.equals("cancelGoods")){
this.jList2_valueChanged(null);
this.checkGoodBtn(false);
}else if(actionCommand.equals("exit")){
exit();
}
}
//列表1的选择事件
void jList1_valueChanged(ListSelectionEvent e) {
if(listData1.size() > 0){
this.showCategory();
}else{
this.clearCategory();
}
}
//列表2的选择事件
void jList2_valueChanged(ListSelectionEvent e) {
if(listData2.size() > 0){
this.showGood();
}else{
this.clearGood();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -