JAVA 购物车示例代码import java.awt.*;
import java.awt.event.*;
class ShopFrame extends Frame implements ActionListener
{Label label1,label2,label3,label4;
Button button1,button2,button3,button4,button5;
TextArea text;
Panel panel1,panel2;
static float sum=0.0f;
ShopFrame(String s)
{ super(s);
setLayout(new BorderLayout());
label1=new Label("面纸:3元",Label.LEFT);
label2=new Label("钢笔:5元",Label.LEFT);
label3=new Label("书:10元",Label.LEFT);
label4=new Label("袜子:8元",Label.LEFT);
button1=new Button("加入购物车");
button2=new Button("加入购物车");
button3=new Button("加入购物车");
button4=new Button("加入购物车");
button5=new Button("查看购物车");
text=new TextArea("商品有:" "\n",5,10);
text.setEditable(false);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
}
);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
panel1=new Panel();
panel2=new Panel();
panel1.add(label1);
panel1.add(button1);
panel1.add(label2);
panel1.add(button2);
panel1.add(label3);
panel1.add(button3);
panel1.add(label4);
panel1.add(button4);
panel2.setLayout(new BorderLayout());
panel2.add(button5,BorderLayout.NORTH);
panel2.add(text,BorderLayout.SOUTH);
this.add(panel1,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
setBounds(100,100,350,250);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==button1)
{ text.append("一个面纸、");
sum=sum 3;
}
else if(e.getSource()==button2)
{ text.append("一只钢笔、");
sum=sum 5;
}
else if(e.getSource()==button3)
{ text.append("一本书、");
sum=sum 10;
}
else if(e.getSource()==button4)
{ text.append("一双袜子、");
sum=sum 8;
}
else if(e.getSource()==button5)
{
text.append("\n" "总价为:" "\n" sum);
}
}
}
public class Shopping {
public static void main(String[] args) {
new ShopFrame("购物车");
}
}
我没用Swing可能显示不出来你的效果 。不满意得话我在给你编一个 。
编程 java 关于购物车点击数量进入购物车页面,这个应该好做吧,跳动一个Action转发到购物车页面
下面是我的图书购物车(自己写的)
package com.jc.ts.services;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.jc.ts.entity.BookCar;
import com.jc.ts.entity.BookInfo;
/**
* 该类提供购物车的操作
* */
public class CartItemsService {
private MapString,BookCar itemMap=null;//购物车Map集合
private CollectionBookCaritems;//购物车项
public CartItemsService()
{
itemMap=new HashMapString ,BookCar();
}
public CollectionBookCar getItems() {
return items;
}
public void setItems(CollectionBookCar items) {
this.items = items;
}
public MapString, BookCar getItemMap() {
return itemMap;
}
public void setItemMap(MapString, BookCar itemMap) {
this.itemMap = itemMap;
}
publicint getBookCarSize()
{
return itemMap.size();
}
public boolean containById(String bookid)
{
return itemMap.containsKey(bookid);
}
public void addBookCarItems(BookInfo bookInfo)
{
if(bookInfo!=null)
{
BookCar bookCar=(BookCar)itemMap.get(bookInfo.getBid());
if(bookCar==null)
{
bookCar=new BookCar();
bookCar.setBookinfo(bookInfo);
bookCar.increaseQuantity();
itemMap.put(bookInfo.getBid(),bookCar);
items=itemMap.values();
}else {
bookCar.increaseQuantity();
}
}
}
public BookInfo removeCarItem(String bookid)
{
BookCar bookCar=itemMap.remove(bookid);
if(bookCar==null)
{
return null;
}
items=itemMap.values();
return bookCar.getBookinfo();
}
public BigDecimal getBookCarTotal()//获得总金额
{
BigDecimal carTotal=new BigDecimal("0.00");
IteratorBookCar iterator=this.getAllCartItems();
while(iterator.hasNext())
{
BookCar bookCar=iterator.next();
BigDecimal carPrice=bookCar.getBookinfo().getBprice();
BigDecimal quantity=new BigDecimal(String.valueOf(bookCar.getQuantity()));
carTotal=carTotal.add(carPrice.multiply(quantity));
}
return carTotal;
}
public IteratorBookCar getAllCartItems(){
return itemMap.values().iterator();
}
public void increaseQuantityById(String bookid)
{
BookCar bookCar=itemMap.get(bookid);
if(bookCar!=null)
{
bookCar.increaseQuantity();
}
}
public void setQuantityById(String bookid,int quantity)//根据图书ID增加数量
{
BookCar bookCar=itemMap.get(bookid);
if(bookCar!=null)
{
bookCar.setQuantity(quantity);
}
}
public void clear(){
itemMap.clear();
}
}
修改后传入这个方法就可以了setQuantityById()
推荐阅读
- 安卓8.0街机FBa模拟器,fba4driod手机街机模拟器
- js中函数的内存空间,js中函数的内存空间是多少
- 恋爱的手机游戏经营,手游版恋爱游戏
- c语言一些函数的用法 c语言一些函数的用法是什么
- 临汾外卖点餐小程序多少钱的简单介绍
- 射击怪物的网络游戏有哪些,射击打怪的游戏叫什么
- 抓饭直播有什么足球版权,抓饭直播足球怎么赚钱的
- c语言中_swap函数 c语言中swap什么意思
- 白色东芝u盘速度怎么样,东芝u盘为什么便宜