?? goodsdiscountmanageframe.java
字號(hào):
package baseinforinterface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import data.*;
import maininterface.*;
import user.*;
import method.*;
public class GoodsDiscountManageFrame extends JFrame implements ActionListener {
JPanel contentPane;
//創(chuàng)建列表框數(shù)據(jù)類和列表框控件
DefaultListModel listData1 = new DefaultListModel();
DefaultListModel listData2 = new DefaultListModel();
JList jList1 = new JList(listData1);
JList jList2 = new JList(listData2);
//創(chuàng)建組合框控件
JComboBox jComboBox1 = new JComboBox();
//創(chuàng)建滾動(dòng)框控件
JScrollPane jScrollPane1 = new JScrollPane();
JScrollPane jScrollPane2 = new JScrollPane();
//創(chuàng)建標(biāo)簽控件
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
//創(chuàng)建編輯框控件
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField3 = new JTextField();
JTextField jTextField4 = new JTextField();
//創(chuàng)建按鈕控件
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JButton jButton6 = new JButton();
JButton jButton7 = new JButton();
//創(chuàng)建字體類
Font dialog13 = new java.awt.Font("Dialog", 0, 13);
//聲明數(shù)據(jù)類
StockManagementData stockManagementData = null;
//聲明用戶類
User user = null;
//聲明主窗口類
StockManagementMainFrame stockManagementMainFrame = null;
//創(chuàng)建商品類別數(shù)組
String[][] categories = new String[0][4];
//創(chuàng)建商品數(shù)組
String[][] goods = new String[0][13];
//創(chuàng)建方法類
DataMethod dataMethod = new DataMethod();
public GoodsDiscountManageFrame(StockManagementMainFrame stockManagementMainFrame) {
this.stockManagementMainFrame = stockManagementMainFrame;
//取得主窗口的數(shù)據(jù)類
stockManagementData = stockManagementMainFrame.getStockManagementData();
//取得主窗口的用戶類
user = stockManagementMainFrame.getUser();
//取得基礎(chǔ)信息模塊的用戶權(quán)限
int baseInforFunction = user.getBaseInforFunction();
//檢查用戶權(quán)限
if((baseInforFunction & 16) != 16){
JOptionPane.showMessageDialog(null, user.getUserName() + "用戶不具有該權(quán)限.");
System.exit(0);
}
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(643, 382));
this.setTitle("商品折扣管理窗口");
//設(shè)置標(biāo)簽控件屬性
jLabel1.setText("商品類別列表:");
jLabel1.setBounds(new Rectangle(28, 19, 97, 16));
jLabel2.setText("商品列表:");
jLabel2.setBounds(new Rectangle(176, 19, 97, 16));
jLabel3.setText("查詢條件:");
jLabel3.setBounds(new Rectangle(344, 45, 97, 16));
jLabel4.setText("查詢值:");
jLabel4.setBounds(new Rectangle(344, 93, 97, 16));
jLabel5.setBounds(new Rectangle(344, 141, 110, 16));
jLabel5.setText("商品條形碼:");
jLabel6.setText("商品名稱:");
jLabel6.setBounds(new Rectangle(344, 190, 72, 16));
jLabel7.setText("商品折扣:");
jLabel7.setBounds(new Rectangle(344, 238, 78, 16));
//設(shè)置編輯框控件屬性
jTextField1.setBounds(new Rectangle(427, 93, 102, 22));
jTextField2.setEditable(false);
jTextField2.setBounds(new Rectangle(427, 141, 196, 22));
jTextField3.setEditable(false);
jTextField3.setBounds(new Rectangle(427, 190, 196, 22));
jTextField4.setBounds(new Rectangle(427, 238, 58, 22));
//設(shè)置按鈕屬性
jButton1.setText("顯示類別");
jButton1.setActionCommand("showCategory");
jButton1.setBounds(new Rectangle(28, 249, 139, 25));
jButton2.setText("修改折扣");
jButton2.setActionCommand("updateDiscount");
jButton2.setBounds(new Rectangle(344, 286, 95, 25));
jButton3.setText("確定");
jButton3.setActionCommand("ok");
jButton3.setEnabled(false);
jButton3.setBounds(new Rectangle(440, 286, 61, 25));
jButton4.setText("取消");
jButton4.setActionCommand("cancel");
jButton4.setEnabled(false);
jButton4.setBounds(new Rectangle(503, 286, 61, 25));
jButton5.setText("退出");
jButton5.setActionCommand("exit");
jButton5.setBounds(new Rectangle(565, 286, 61, 25));
jButton6.setText("查詢");
jButton6.setActionCommand("search");
jButton6.setBounds(new Rectangle(546, 93, 77, 25));
jButton7.setText("顯示折扣商品");
jButton7.setActionCommand("showDiscountGoods");
jButton7.setBounds(new Rectangle(28, 286, 139, 25));
//設(shè)置組合框?qū)傩? jComboBox1.setBounds(new Rectangle(427, 45, 196, 22));
jComboBox1.addItem("按商品條形碼查詢");
jComboBox1.addItem("按商品名稱查詢");
jComboBox1.addItem("按生產(chǎn)廠商查詢");
//設(shè)置滾動(dòng)框的屬性
jScrollPane1.getViewport().add(jList1, null);
jScrollPane2.getViewport().add(jList2, null);
jScrollPane1.setBounds(new Rectangle(28, 45, 139, 193));
jScrollPane2.setBounds(new Rectangle(176, 45, 153, 266));
//為列表框加入選擇接收器
jList1.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList1_valueChanged(e);
}
});
jList2.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList2_valueChanged(e);
}
});
contentPane.add(jScrollPane1, null);
contentPane.add(jScrollPane2, null);
contentPane.add(jLabel1, null);
contentPane.add(jLabel2, null);
contentPane.add(jLabel3, null);
contentPane.add(jLabel4, null);
contentPane.add(jLabel5, null);
contentPane.add(jLabel6, null);
contentPane.add(jLabel7, null);
contentPane.add(jButton1, null);
contentPane.add(jButton2, null);
contentPane.add(jButton3, null);
contentPane.add(jButton4, null);
contentPane.add(jButton5, null);
contentPane.add(jButton6, null);
contentPane.add(jButton7, null);
contentPane.add(jComboBox1, null);
contentPane.add(jTextField1, null);
contentPane.add(jTextField2, null);
contentPane.add(jTextField3, null);
contentPane.add(jTextField4, null);
//設(shè)置窗口類的字體和為按鈕加入動(dòng)作接收器
setupFontAndListener();
//顯示全部商品類別的方法
showAllCategories();
}
//設(shè)置窗口類的字體和為按鈕加入動(dòng)作接收器的方法
public void setupFontAndListener(){
Component[] components = contentPane.getComponents();
//創(chuàng)建臨時(shí)按鈕控件
JButton tmpBtn = new JButton();
for(int i = 0; i < components.length; i++){
components[i].setFont(dialog13);
if(components[i].getClass().getName().equals("javax.swing.JButton")){
tmpBtn = (JButton)components[i];
tmpBtn.addActionListener(this);
}
}
}
//顯示全部商品類別的方法
public void showAllCategories(){
listData1.clear();
//取得商品類別數(shù)據(jù)
categories = stockManagementData.getAllGoodsCategory();
//為商品類別列表框加入商品類別數(shù)據(jù)
for(int i = 0; i < categories.length; i++){
listData1.addElement(categories[i][2]);
}
}
//根據(jù)商品類別顯示商品的方法
public void showCategory(){
//取得當(dāng)前選擇項(xiàng)的位置
int selectedIndex = jList1.getSelectedIndex();
//取得商品數(shù)據(jù)
goods = stockManagementData.getGoodsByGoodsCategory(Integer.parseInt(
categories[selectedIndex][0]));
this.showSearchGoods();
}
//顯示查詢商品的方法
public void showSearchGoods(){
listData2.clear();
//為商品列表框加入商品數(shù)據(jù)
for(int i = 0; i < goods.length; i++){
listData2.addElement(goods[i][2]);
}
}
//顯示單個(gè)商品的方法
public void showGood(){
//取得當(dāng)前選擇項(xiàng)的位置
int selectedIndex = jList2.getSelectedIndex();
//當(dāng)列表框不處于選擇狀態(tài),不顯示商品數(shù)據(jù)
if(selectedIndex == -1){
return;
}
//顯示商品條形碼
jTextField2.setText(goods[selectedIndex][0]);
//顯示商品名稱
jTextField3.setText(goods[selectedIndex][2]);
//顯示折扣
jTextField4.setText(goods[selectedIndex][12]);
}
//清空單個(gè)商品顯示的方法
public void clearGood(){
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
}
//退出方法
public void exit(){
//隱藏窗口
this.setVisible(false);
//清空數(shù)組的內(nèi)容
categories = new String[0][4];
goods = new String[0][13];
//清空列表框的內(nèi)容
listData1.clear();
listData2.clear();
//取得面板上的所有控件
Component[] components = contentPane.getComponents();
//創(chuàng)建臨時(shí)文本框控件
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];
//清空編輯框的內(nèi)容
tmpTextField.setText("");
}
}
}
//設(shè)置用戶的方法
public void setUser(User user) {
this.user = user;
}
//檢查商品按鈕的狀態(tài)
public void checkGoodsBtn(boolean isManipulated){
if(isManipulated){
jButton2.setEnabled(false);
jButton3.setEnabled(true);
jButton4.setEnabled(true);
}else{
jButton2.setEnabled(true);
jButton3.setEnabled(false);
jButton4.setEnabled(false);
}
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
exit();
}
}
//列表1的選擇事件
void jList1_valueChanged(ListSelectionEvent e) {
if(listData1.size() > 0){
this.showCategory();
}
}
//列表2的選擇事件
void jList2_valueChanged(ListSelectionEvent e) {
if(listData2.size() > 0){
this.showGood();
}else{
this.clearGood();
}
}
//單擊事件
public void actionPerformed(ActionEvent e) {
//取得按鈕的動(dòng)作字符串
String actionCommand = e.getActionCommand().trim();
//單擊按鈕的處理代碼
if (actionCommand.equals("showCategory")) {
this.showAllCategories();
}else if(actionCommand.equals("showDiscountGoods")){
//取得折扣商品
goods = stockManagementData.getDiscountGoods();
this.showSearchGoods();
}else if(actionCommand.equals("search")){
//取得查詢編輯框的內(nèi)容
String searchValue = jTextField1.getText().trim();
//取得查詢選項(xiàng)
int selectedIndex = jComboBox1.getSelectedIndex();
if(searchValue.length() == 0){
JOptionPane.showMessageDialog(null, "請(qǐng)輸入查詢值");
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("updateDiscount")){
//進(jìn)行商品折扣修改時(shí)檢查商品的選擇狀態(tài)
if (jList2.isSelectionEmpty()) {
JOptionPane.showMessageDialog(null, "請(qǐng)選擇商品.");
return;
}
this.checkGoodsBtn(true);
}else if(actionCommand.equals("ok")){
//取得商品條形碼和折扣
String goodsBarCode = jTextField2.getText().trim();
String discountStr = jTextField4.getText().trim();
double discount = 0;
if(dataMethod.checkNumIn0To1(discountStr) == 0){
JOptionPane.showMessageDialog(null, "商品折扣的數(shù)值范圍必須大于0小于等于1.");
return;
}else{
discount = Double.parseDouble(discountStr);
}
int result = stockManagementData.setGoodsDiscount(goodsBarCode, discount);
if(result == 1){
//更新商品數(shù)組的折扣數(shù)據(jù)
int selectedIndex = jList2.getSelectedIndex();
goods[selectedIndex][12] = discountStr;
}else{
JOptionPane.showMessageDialog(null, "商品折扣更改失敗.");
}
this.checkGoodsBtn(false);
}else if(actionCommand.equals("cancel")){
this.jList2_valueChanged(null);
this.checkGoodsBtn(false);
}else if(actionCommand.equals("exit")){
exit();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -