📄 cartviewframe.java
字号:
package cartinterface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import data.*;
import java.util.*;
public class CartViewFrame extends JFrame implements ActionListener {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane cartTableScrollPane = new JScrollPane();
JPanel buttonPanel = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
//创建商品表格
JTable cartTable = null;
//创建商品表格的模块类
CartTableModel cartTableModel= new CartTableModel();
//创建字体类
Font dialog13 = new java.awt.Font("Dialog", 0, 13);
//创建购物车类
Cart cart = null;
public CartViewFrame(Cart cart) {
//取得传入的购物车对象
this.cart = cart;
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(500, 400));
this.setTitle("购物车查看窗口");
//设置按钮的属性
jButton1.setText("修改购物车的数量");
jButton1.setActionCommand("updateQuantity");
jButton1.setFont(dialog13);
jButton1.addActionListener(this);
jButton2.setText("删除选择商品");
jButton2.setActionCommand("delete");
jButton2.setFont(dialog13);
jButton2.addActionListener(this);
jButton3.setText("重新显示购物车的商品");
jButton3.setActionCommand("reShow");
jButton3.setFont(dialog13);
jButton3.addActionListener(this);
jButton4.setText("退出");
jButton4.setActionCommand("exit");
jButton4.setFont(dialog13);
jButton4.addActionListener(this);
buttonPanel.add(jButton1, null);
buttonPanel.add(jButton2, null);
buttonPanel.add(jButton3, null);
contentPane.add(buttonPanel, BorderLayout.NORTH);
contentPane.add(cartTableScrollPane, BorderLayout.CENTER);
//显示购物车的商品
showTable();
}
//显示购物车的商品
public void showTable(){
//取得购物车的商品数组
Object[][] goodsObjs = cart.getGoodsobjs();
//设置表格数据类的数组
cartTableModel.setData(goodsObjs);
//创建购物车的商品表格
cartTable = new JTable(cartTableModel);
//设置表格的字体
cartTable.setFont(dialog13);
//使表格的选择范围为一个单元格
cartTable.setRowSelectionAllowed(false);
cartTableScrollPane.getViewport().add(cartTable, null);
}
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
//单击退出按钮时隐藏该窗口
this.setVisible(false);
}
}
//按钮单击事件
public void actionPerformed(ActionEvent e) {
//取得按钮的动作字符串
String actionCommand = e.getActionCommand().trim();
//取得表格的数组
Object[][] goodsObjs = cartTableModel.getData();
//单击修改购物车的数量按钮的处理代码
if (actionCommand.equals("updateQuantity")) {
//创建商品集合
Hashtable goods = new Hashtable();
for(int i = 0; i < (goodsObjs.length -1); i++){
if(((Boolean)goodsObjs[i][4]).booleanValue()){
//检查商品数量是否是整字
if(!(new NumberVerify().verifyNum(goodsObjs[i][5].toString()))){
JOptionPane.showMessageDialog(null, goodsObjs[i][1]
+ "的购买数量需要输入整数并且大于等于0.");
//不再执行其它代码
return;
}
goods.put(goodsObjs[i][0], goodsObjs[i][1] + "@@" + goodsObjs[i][2]
+ "@@" + goodsObjs[i][3] + "@@" + goodsObjs[i][5]);
}
}
//更新购买商品的数量
cart.updateGoods(goods);
//重新显示表格内容
showTable();
}else if(actionCommand.equals("delete")){
Collection goodsId = new ArrayList();
for(int i = 0; i < (goodsObjs.length -1); i++){
if(((Boolean)goodsObjs[i][4]).booleanValue()){
goodsId.add(goodsObjs[i][0]);
}
}
//删除购物车的商品
cart.removeGoods(goodsId);
//重新显示表格内容
showTable();
}else if(actionCommand.equals("reShow")){
//重新显示表格内容
showTable();
}else if(actionCommand.equals("exit")){
//单击退出按钮时隐藏该窗口
this.setVisible(false);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -