⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 goodssearchframe.java

📁 商品销售管理系统工程代码
💻 JAVA
字号:
package goodsinterface;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import data.*;
import java.awt.Rectangle;

public class GoodsSearchFrame extends JFrame implements ActionListener {
  JPanel contentPane;
  //创建标签控件
  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();
  JLabel jLabel8 = new JLabel();
  JLabel jLabel9 = new JLabel();
  JLabel jLabel10 = new JLabel();
  JLabel jLabel11 = new JLabel();
  JLabel jLabel12 = new JLabel();
  JLabel jLabel13 = new JLabel();
  JLabel jLabel14 = new JLabel();
  JLabel jLabel15 = new JLabel();
  JLabel jLabel16 = new JLabel();
  //创建编辑框控件
  JTextField jTextField1 = new JTextField();
  JTextField jTextField2 = new JTextField();
  JTextField jTextField3 = new JTextField();
  JTextField jTextField4 = new JTextField();
  JTextField jTextField5 = new JTextField();
  JTextField jTextField6 = new JTextField();
  JTextField jTextField7 = new JTextField();
  JTextField jTextField8 = new JTextField();
  JTextField jTextField9 = new JTextField();
  JTextField jTextField10 = new JTextField();
  JTextField jTextField11 = new JTextField();
  JTextField jTextField12 = new JTextField();
  JTextField jTextField13 = new JTextField();
  //创建按钮控件
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  //创建滚动框控件
  JScrollPane jScrollPane1 = new JScrollPane();
  JScrollPane jScrollPane2 = new JScrollPane();
  JScrollPane jScrollPane3 = new JScrollPane();
  //创建列表框数据类和列表框控件
  DefaultListModel listData1 = new DefaultListModel();
  DefaultListModel listData2 = new DefaultListModel();
  JList jList1 = new JList(listData1);
  JList jList2 = new JList(listData2);
  //创建文本框控件
  JTextArea jTextArea1 = new JTextArea();
  //创建组合框控件
  JComboBox jComboBox1 = new JComboBox();
  //创建字体类
  Font dialog13 = new java.awt.Font("Dialog", 0, 13);
  //声明数据类
  GoodsData goodsData = new GoodsData();
  //创建商品类别数组
  String[][] categories = new String[0][4];
  //创建商品数组
  String[][] goods = new String[0][13];
  //创建动作字符串
  String action = "";
  String user = "user";
  //创建字段数组
  String[] fieldNames = {
      "goodsBarCode", "goodsName", "goodsNickName", "goodsAssistantName",
      "goodsPYName", "unit", "specification", "producer"};

  public GoodsSearchFrame() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //传入用户变量
  public GoodsSearchFrame(String user) {
    //设置用户名字
    this.user = user;
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setSize(new Dimension(670, 550));
    this.setTitle("商品查询窗口(" + user + ")");
    //设置标签控件
    jLabel1.setText("商品类别列表:");
    jLabel1.setBounds(new Rectangle(26, 17, 97, 16));
    jLabel2.setText("类别序号:");
    jLabel2.setBounds(new Rectangle(197, 47, 83, 16));
    jLabel3.setText("类别父索引:");
    jLabel3.setBounds(new Rectangle(197, 101, 88, 16));
    jLabel4.setText("商品类别名称:");
    jLabel4.setBounds(new Rectangle(197, 156, 109, 16));
    jLabel5.setText("商品类别描述:");
    jLabel5.setBounds(new Rectangle(403, 47, 112, 16));
    jLabel6.setText("商品列表:");
    jLabel6.setBounds(new Rectangle(26, 184, 97, 16));
    jLabel7.setText("查询条件:");
    jLabel7.setBounds(new Rectangle(196, 210, 97, 16));
    jLabel8.setText("商品条形码:");
    jLabel8.setBounds(new Rectangle(196, 260, 110, 16));
    jLabel9.setText("商品分类标识:");
    jLabel9.setBounds(new Rectangle(490, 260, 92, 16));
    jLabel10.setText("商品名称:");
    jLabel10.setBounds(new Rectangle(196, 309, 72, 16));
    jLabel11.setText("商品别名:");
    jLabel11.setBounds(new Rectangle(490, 309, 85, 16));
    jLabel12.setText("助记码:");
    jLabel12.setBounds(new Rectangle(196, 359, 76, 16));
    jLabel13.setText("拼音码:");
    jLabel13.setBounds(new Rectangle(356, 359, 76, 16));
    jLabel14.setText("计量单位:");
    jLabel14.setBounds(new Rectangle(490, 359, 76, 16));
    jLabel15.setText("规格:");
    jLabel15.setBounds(new Rectangle(196, 408, 76, 16));
    jLabel16.setText("生产厂商:");
    jLabel16.setBounds(new Rectangle(356, 408, 72, 16));
    //设置编辑框
    jTextField1.setBounds(new Rectangle(288, 47, 93, 22));
    jTextField1.setEditable(false);
    jTextField2.setBounds(new Rectangle(288, 99, 93, 22));
    jTextField2.setEditable(false);
    jTextField3.setBounds(new Rectangle(288, 150, 190, 22));
    jTextField4.setBounds(new Rectangle(433, 210, 102, 22));
    jTextField5.setBounds(new Rectangle(281, 258, 179, 22));
    jTextField6.setBounds(new Rectangle(576, 258, 65, 22));
    jTextField7.setBounds(new Rectangle(281, 306, 179, 22));
    jTextField8.setBounds(new Rectangle(576, 306, 65, 22));
    jTextField9.setBounds(new Rectangle(281, 354, 65, 22));
    jTextField10.setBounds(new Rectangle(407, 354, 65, 22));
    jTextField11.setBounds(new Rectangle(576, 354, 65, 22));
    jTextField12.setBounds(new Rectangle(281, 402, 65, 22));
    jTextField13.setBounds(new Rectangle(462, 402, 179, 22));
    //设置按钮
    jButton1.setText("显示商品类别");
    jButton1.setActionCommand("showCategory");
    jButton1.setBounds(new Rectangle(196, 451, 217, 25));
    jButton2.setText("查询");
    jButton2.setActionCommand("search");
    jButton2.setBounds(new Rectangle(543, 210, 98, 25));
    jButton3.setText("退出");
    jButton3.setActionCommand("exit");
    jButton3.setBounds(new Rectangle(424, 451, 217, 25));
    //设置滚动框
    jScrollPane1.setBounds(new Rectangle(26, 47, 153, 125));
    jScrollPane2.setBounds(new Rectangle(488, 47, 153, 125));
    jScrollPane3.setBounds(new Rectangle(26, 210, 153, 266));
    jScrollPane1.getViewport().add(jList1, null);
    jScrollPane2.getViewport().add(jTextArea1, null);
    jScrollPane3.getViewport().add(jList2, null);
    //为列表框加入选择接收器
    jList1.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        jList1_valueChanged(e);
      }
    });
    jList2.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        jList2_valueChanged(e);
      }
    });
    //设置下拉列表框
    jComboBox1.setBounds(new Rectangle(281, 210, 141, 22));
    jComboBox1.addItem("按商品条形码查询");
    jComboBox1.addItem("按商品名称查询");
    jComboBox1.addItem("按商品别名查询");
    jComboBox1.addItem("按助记码查询");
    jComboBox1.addItem("按拼音码查询");
    jComboBox1.addItem("按计量单位查询");
    jComboBox1.addItem("按规格查询");
    jComboBox1.addItem("按生产厂商查询");
    contentPane.add(jScrollPane1, null);
    contentPane.add(jScrollPane2, null);
    contentPane.add(jScrollPane3, 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(jLabel8, null);
    contentPane.add(jLabel9, null);
    contentPane.add(jLabel10, null);
    contentPane.add(jLabel11, null);
    contentPane.add(jLabel12, null);
    contentPane.add(jLabel13, null);
    contentPane.add(jLabel14, null);
    contentPane.add(jLabel15, null);
    contentPane.add(jLabel16, null);
    contentPane.add(jTextField1, null);
    contentPane.add(jTextField2, null);
    contentPane.add(jTextField3, null);
    contentPane.add(jTextField4, null);
    contentPane.add(jTextField5, null);
    contentPane.add(jTextField6, null);
    contentPane.add(jTextField7, null);
    contentPane.add(jTextField8, null);
    contentPane.add(jTextField9, null);
    contentPane.add(jTextField10, null);
    contentPane.add(jTextField11, null);
    contentPane.add(jTextField12, null);
    contentPane.add(jTextField13, null);
    contentPane.add(jButton1, null);
    contentPane.add(jButton2, null);
    contentPane.add(jButton3, null);
    contentPane.add(jComboBox1, null);
    //设置窗口类的字体和为按钮加入动作接收器
    setupFontAndListener();
    //显示全部商品类别的方法
    showAllCategories();
  }
  //设置窗口类的字体和为按钮加入动作接收器的方法
  public void setupFontAndListener(){
    Component[] components = contentPane.getComponents();
    //创建临时按钮控件
    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();
    //取得商品类别数据
    categories = goodsData.getAllGoodsCategory();
    //为商品类别列表框加入商品类别数据
    for(int i = 0; i < categories.length; i++){
      listData1.addElement(categories[i][2]);
    }
  }
  //显示单个商品类别的方法
  public void showCategory(){
    //取得当前选择项的位置
    int selectedIndex = jList1.getSelectedIndex();
    //当列表框不处于选择状态,不显示商品类别数据
    if(selectedIndex == -1){
       return;
    }
    //显示商品类别序号
    jTextField1.setText(categories[selectedIndex][0]);
    //显示父索引
    jTextField2.setText(categories[selectedIndex][1]);
    //显示商品类别名称
    jTextField3.setText(categories[selectedIndex][2]);
    //显示商品类别描述
    jTextArea1.setText(categories[selectedIndex][3]);
    //取得商品数据
    goods = goodsData.getGoodsByGoodsCategory(Integer.parseInt(
        categories[selectedIndex][0]));
    this.showSearchGoods();
  }
  //清空单个商品类别显示的方法
  public void clearCategory(){
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
    jTextArea1.setText("");
  }
  //显示查询商品的方法
  public void showSearchGoods(){
    listData2.clear();
    //为商品列表框加入商品数据
    for(int i = 0; i < goods.length; i++){
      listData2.addElement(goods[i][0]);
    }
  }
  //显示单个商品的方法
  public void showGood(){
    //取得当前选择项的位置
    int selectedIndex = jList2.getSelectedIndex();
    //当列表框不处于选择状态,不显示商品数据
    if(selectedIndex == -1){
       return;
    }
    //显示商品条形码
    jTextField5.setText(goods[selectedIndex][0]);
    //显示商品分类标识
    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("");
      }
    }
    //System.exit(0);
  }
  //检查商品类别按钮的状态
  public void checkCategoryBtn(boolean isManipulated){
    if(isManipulated){
    }else{
    }
  }
  //检查商品按钮的状态
  public void checkGoodBtn(boolean isManipulated){
    if(isManipulated){
    }else{
    }
  }
  //事件单击方法
  public void actionPerformed(ActionEvent e) {
    //取得按钮的动作字符串
    String actionCommand = e.getActionCommand().trim();
    //单击按钮的处理代码
    if (actionCommand.equals("showCategory")) {
      this.showAllCategories();
    }else if(actionCommand.equals("search")){
      //取得查询编辑框的内容
      String searchValue = jTextField4.getText().trim();
      //取得查询选项
      int selectedIndex = jComboBox1.getSelectedIndex();
      if(searchValue.length() == 0){
        JOptionPane.showMessageDialog(null, "请输入查询值");
        return;
      }
      //根据字段取得商品记录
      goods = goodsData.getGoodsByField(fieldNames[selectedIndex], searchValue);
      this.showSearchGoods();
    }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 + -