使用idea 基于java,用JFrame创建页面,实现简单的具有简单购物功能的页面的代码展示
时间: 2024-02-28 14:55:46 浏览: 99
好的,以下是一个基于 Java 和 JFrame 创建具有简单购物功能的页面的代码展示:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
public class ShoppingCart extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel title, productLabel, qtyLabel, priceLabel, cartLabel;
private JTextField productText, qtyText, priceText;
private JButton addButton, removeButton, checkoutButton;
private JList<String> cartList;
private DefaultListModel<String> cartListModel;
private ArrayList<Product> productList;
public ShoppingCart() {
super("简单购物车");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 标题
title = new JLabel("简单购物车");
title.setFont(new Font("SansSerif", Font.BOLD, 24));
title.setHorizontalAlignment(JLabel.CENTER);
add(title, BorderLayout.NORTH);
// 商品列表
JPanel productPanel = new JPanel(new GridLayout(0, 3));
productLabel = new JLabel("商品名称:");
productPanel.add(productLabel);
qtyLabel = new JLabel("数量:");
productPanel.add(qtyLabel);
priceLabel = new JLabel("价格:");
productPanel.add(priceLabel);
productText = new JTextField();
productPanel.add(productText);
qtyText = new JTextField();
productPanel.add(qtyText);
priceText = new JTextField();
productPanel.add(priceText);
addButton = new JButton("添加");
addButton.addActionListener(this);
productPanel.add(addButton);
add(productPanel, BorderLayout.WEST);
// 购物车列表
JPanel cartPanel = new JPanel(new BorderLayout());
cartLabel = new JLabel("购物车:");
cartPanel.add(cartLabel, BorderLayout.NORTH);
cartListModel = new DefaultListModel<String>();
cartList = new JList<String>(cartListModel);
JScrollPane scrollPane = new JScrollPane(cartList);
cartPanel.add(scrollPane, BorderLayout.CENTER);
removeButton = new JButton("移除");
removeButton.addActionListener(this);
cartPanel.add(removeButton, BorderLayout.SOUTH);
add(cartPanel, BorderLayout.CENTER);
// 结账按钮
JPanel checkoutPanel = new JPanel(new FlowLayout());
checkoutButton = new JButton("结账");
checkoutButton.addActionListener(this);
checkoutPanel.add(checkoutButton);
add(checkoutPanel, BorderLayout.SOUTH);
// 商品列表数据
productList = new ArrayList<Product>();
productList.add(new Product("T恤", 50.00));
productList.add(new Product("牛仔裤", 80.00));
productList.add(new Product("运动鞋", 120.00));
productList.add(new Product("手表", 200.00));
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == addButton) {
// 添加商品到购物车
String product = productText.getText();
int qty = Integer.parseInt(qtyText.getText());
double price = Double.parseDouble(priceText.getText());
Product p = new Product(product, price);
int index = productList.indexOf(p);
if (index != -1) {
p = productList.get(index);
} else {
productList.add(p);
}
p.setQty(p.getQty() + qty);
DecimalFormat df = new DecimalFormat("#.00");
String item = p.getName() + " x " + p.getQty() + " = ¥" + df.format(p.getPrice() * p.getQty());
if (!cartListModel.contains(item)) {
cartListModel.addElement(item);
}
} else if (source == removeButton) {
// 从购物车移除商品
int index = cartList.getSelectedIndex();
if (index != -1) {
String item = cartListModel.getElementAt(index);
String[] parts = item.split(" x ");
String name = parts[0];
int qty = Integer.parseInt(parts[1].split(" = ")[0]);
Product p = new Product(name, 0);
int productIndex = productList.indexOf(p);
if (productIndex != -1) {
p = productList.get(productIndex);
p.setQty(p.getQty() - qty);
if (p.getQty() == 0) {
productList.remove(productIndex);
}
}
cartListModel.removeElementAt(index);
}
} else if (source == checkoutButton) {
// 结账
double total = 0;
for (Product p : productList) {
total += p.getPrice() * p.getQty();
}
DecimalFormat df = new DecimalFormat("#.00");
JOptionPane.showMessageDialog(null, "您的购物车总价为 ¥" + df.format(total), "结账", JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args) {
new ShoppingCart();
}
private class Product {
private String name;
private double price;
private int qty;
public Product(String name, double price) {
this.name = name;
this.price = price;
this.qty = 0;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
@Override
public boolean equals(Object o) {
if (o instanceof Product) {
Product p = (Product) o;
return name.equals(p.name);
}
return false;
}
}
}
```
这个代码展示了一个简单的具有简单购物功能的页面,包括一个商品列表、一个购物车列表和一个结账按钮。用户可以在商品列表中输入商品名称、数量和价格,然后点击“添加”按钮将商品添加到购物车中并显示在购物车列表中。用户还可以从购物车列表中选择某个商品并点击“移除”按钮将商品从购物车中移除。最后,用户可以点击“结账”按钮查看购物车的总价。
阅读全文