?? 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 + -