📄 cartframe.java
字号:
package cartinterface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import com.borland.dx.dataset.*;
import com.borland.dbswing.*;
import data.*;
import java.util.Hashtable;
public class CartFrame extends JFrame implements ActionListener {
JPanel contentPane;
//创建数据类
TableDataSet categoryDataSet = new TableDataSet();
TableDataSet productDataSet = new TableDataSet();
//创建数据表格
JdbTable categoryDBTable = new JdbTable();
JdbTable productDBTable = new JdbTable();
//创建滚动框
TableScrollPane categoryTableScrollPane = new TableScrollPane();
TableScrollPane productTableScrollPane = new TableScrollPane();
JSplitPane jSplitPane1 = new JSplitPane();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
//创建字体类
Font dialog13 = new java.awt.Font("Dialog", 0, 13);
//创建数据模块
CartDataModule cartDataModule1;
//创建购物车类
Cart cart = null;
//声明购物车窗口对象
CartViewFrame cartViewFrame = null;
public CartFrame(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(685, 530));
this.setTitle("商品浏览窗口");
//创建数据模块
cartDataModule1 = new CartDataModule();
//取得数据模块的数据类
categoryDataSet = cartDataModule1.getCategoryDataSet();
productDataSet = cartDataModule1.getProductDataSet();
//设置数据表格的数据源
categoryDBTable.setDataSet(categoryDataSet);
productDBTable.setDataSet(productDataSet);
//设置数据表格的字体
categoryDBTable.setFont(dialog13);
productDBTable.setFont(dialog13);
//使表格的选择范围为一个单元格
productDBTable.setRowSelectionAllowed(false);
//将数据表格加入滚动框
categoryTableScrollPane.getViewport().add(categoryDBTable, null);
productTableScrollPane.getViewport().add(productDBTable, null);
//设置按钮属性
jButton1.setActionCommand("buy");
jButton1.setText("将选择商品放入购物车");
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);
contentPane.add(jPanel1, BorderLayout.NORTH);
jPanel1.add(jButton1, null);
jPanel1.add(jButton2, null);
jPanel1.add(jButton3, null);
//为窗口面板加入分隔面板
contentPane.add(jSplitPane1, BorderLayout.CENTER);
//创建购物车窗口
cartViewFrame = new CartViewFrame(cart);
//使购物车窗口的位置居中对齐
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = cartViewFrame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
cartViewFrame.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
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 < productDBTable.getRowCount(); i++){
if(productDBTable.getValueAt(i, 4) != null){
//取得是否购买列的数据
boolean isSelected = ( (Boolean) productDBTable.getValueAt(i, 4)).
booleanValue();
if (isSelected) {
//检验购买数量列是否是整数并且数字是否大于等于0
if(!(new NumberVerify().verifyNum(
productDBTable.getValueAt(i, 5).toString()))){
JOptionPane.showMessageDialog(null, productDBTable.getValueAt(i, 1)
+ "的购买数量需要输入整数并且大于等于0.");
//不再执行其它代码
return;
}
String good = productDBTable.getValueAt(i, 1) + "@@" + productDBTable.
getValueAt(i, 2) + "@@" + productDBTable.getValueAt(i, 3)
+ "@@" + productDBTable.getValueAt(i, 5);
//将购买商品放入集合类
goods.put(productDBTable.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 + -