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

📄 testframe.java~120~

📁 购物车模块实例。购物车模块可以浏览商品类别;可以根据商品类别浏览商品信息;可以购买商品;可以查看购物车的商品;可以修改购买商品的数量和删除购买的商品。
💻 JAVA~120~
字号:
package test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import data.*;
import java.util.*;
import cartinterface.*;

public class TestFrame extends JFrame implements ActionListener {
  JPanel contentPane;
  BorderLayout borderLayout1 = new BorderLayout();
  //声明商品数据类
  CartDataForWindow cartData = null;
  //创建表格
  JTable categoryTable = new JTable();
  JTable productTable = new JTable();
  //创建表格模式对象
  CartTableModel categoryTableModel = new CartTableModel();
  CartTableModel productTableModel = new CartTableModel();
  //创建表格的标题数组
  String[] categoryColumnNames = {"类别ID", "类别名字", "描述"};
  String[] productColumnNames = {"商品ID", "商品名称", "单位数量", "单价", "是否购买",
      "购买数量"};
  //创建数组
  String[][] categories = new String[1][1];
  Object[][] products = new Object[1][1];
  //创建字体类
  Font dialog13 = new java.awt.Font("Dialog", 0, 13);
  //创建分隔框
  JSplitPane jSplitPane1 = new JSplitPane();
  //创建表格滚动框
  JScrollPane categoryTableScrollPane = new JScrollPane();
  JScrollPane productTableScrollPane = new JScrollPane();
  //创建按钮面板
  JPanel buttonPanel = new JPanel();
  //创建按钮
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  //声明购物车窗口对象
  CartViewFrame cartViewFrame = null;

  public TestFrame() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(685, 530));
    this.setTitle("商品浏览窗口");
    //创建数据类
    cartData = new CartDataForWindow();
    //显示产品类别数组的数据
    showCategories();
    //显示产品数组的数据
    showProducts(Integer.parseInt(categories[0][0]));
    //设置按钮属性
    jButton1.setText("将选择商品放入购物车");
    jButton1.setActionCommand("buy");
    jButton1.setFont(dialog13);
    jButton1.addActionListener(this);
    jButton2.setText("查看购物车");
    jButton2.setActionCommand("viewCart");
    jButton2.setFont(dialog13);
    jButton2.addActionListener(this);
    jButton3.setText("退出");
    jButton3.setActionCommand("exit");
    jButton3.setFont(dialog13);
    jButton3.addActionListener(this);
    //定义分隔面板的分隔条大小
    jSplitPane1.setDividerSize(6);
    //定义分隔面板的分隔条的位置
    jSplitPane1.setDividerLocation(180);
    //将分隔面板定义为上下方向
    jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
    //为分隔面板的顶部加入category滚动框
    jSplitPane1.add(categoryTableScrollPane, JSplitPane.TOP);
    //为分隔面板的底部加入product滚动框
    jSplitPane1.add(productTableScrollPane, JSplitPane.BOTTOM);
    //为按钮面板加入按钮
    buttonPanel.add(jButton1, null);
    buttonPanel.add(jButton2, null);
    buttonPanel.add(jButton3, null);
    //为面板加入控件
    contentPane.add(buttonPanel, BorderLayout.NORTH);
    contentPane.add(jSplitPane1, BorderLayout.CENTER);
  }
  //显示产品类别的方法
  public void showCategories() throws Exception{
    categories = cartData.getCategories();
    categoryTableModel.setColumnNames(categoryColumnNames);
    categoryTableModel.setData(categories);
    categoryTable = new JTable(categoryTableModel);
    //取得category表格的行
    ListSelectionModel rowSM = categoryTable.getSelectionModel();
    //加入行选择接收器
    rowSM.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        categoryTableList_valueChanged(e);
      }
    });
    //设置表格的字体
    categoryTable.setFont(dialog13);
    categoryTableScrollPane.getViewport().add(categoryTable, null);
  }
  //显示产品表格的数据
  public void showProducts(int categoryId) throws Exception{
    //取得产品数组的数据
    products = cartData.getProducts(categoryId);
    //设置表格数据类的数组
    productTableModel.setData(products);
    //设置表格数据类的标题
    productTableModel.setColumnNames(productColumnNames);
    //创建购物车的商品表格
    productTable = new JTable(productTableModel);
    //设置表格的字体
    productTable.setFont(dialog13);
    //使表格的选择范围为一个单元格
    productTable.setRowSelectionAllowed(false);
    productTableScrollPane.getViewport().add(productTable, null);
  }
  //category表格的行接收器的处理方法
  public void categoryTableList_valueChanged(ListSelectionEvent e) {
    //当多种事件被激发的时候,不执行接收器后面的代码
    if (e.getValueIsAdjusting()) return;
    //取得行对象
    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
    //取得当前选择的行
    int selectedRow = lsm.getMinSelectionIndex();
    //取得当前选择行的类别ID,然后根据类别ID显示产品数据
    try{
      showProducts(Integer.parseInt(categoryTableModel.getValueAt(selectedRow,
          0).toString()));
    }catch(Exception ex){
      ex.printStackTrace();
    }
  }
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
  //按钮单击方法
  public void actionPerformed(ActionEvent e) {
    //取得按钮的动作字符串
    String actionCommand = e.getActionCommand().trim();
    //单击将选择商品放入购物车按钮的处理代码
    if (actionCommand.equals("buy")) {
      //创建购买商品集合
      Hashtable goods = new Hashtable();
      for(int i = 0; i < productTable.getRowCount(); i++){
        if(productTable.getValueAt(i, 4) != null){
          //取得是否购买列的数据
          boolean isSelected = ( (Boolean) productTable.getValueAt(i, 4)).
              booleanValue();
          if (isSelected) {
            //检验购买数量列是否是整数并且数字是否大于等于0
            if(!(new NumberVerify().verifyNum(
                productTable.getValueAt(i, 5).toString()))){
              JOptionPane.showMessageDialog(null, productTable.getValueAt(i, 1)
                                            + "的购买数量需要输入整数并且大于等于0.");
              //不再执行其它代码
              return;
            }
            String good = productTable.getValueAt(i, 1) + "@@" + productTable.
                getValueAt(i, 2) + "@@" + productTable.getValueAt(i, 3)
                + "@@" + productTable.getValueAt(i, 5);
            //将购买商品放入集合类
            goods.put(productTable.getValueAt(i, 0).toString(), good);
          }
        }
      }
      //将购买商品集合放入购物车
      cart.addGoods(goods);
      //更新购物车窗口的购物车类
      cartViewFrame.cart = cart;
      //重新显示购物车窗口的表格
      cartViewFrame.showTable();
    //单击查看购物车按钮的处理代码
    }else if(actionCommand.equals("viewCart")){
      //显示购物车窗口
      cartViewFrame.setVisible(true);
    }else if(actionCommand.equals("exit")){
      System.exit(0);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -