使用idea 基于java,用JFrame创建页面,实现简单的,大界面,具有购物结账功能的页面的代码展示
时间: 2024-02-28 14:56:21 浏览: 58
好的,以下是一个基于Java Swing的简单购物结账页面的代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ShoppingCart extends JFrame implements ActionListener {
private JLabel lblTitle;
private JPanel pnlItems;
private JLabel lblItem1, lblItem2, lblItem3, lblItem4;
private JTextField txtItem1, txtItem2, txtItem3, txtItem4;
private JPanel pnlButtons;
private JButton btnAdd, btnRemove, btnCheckout;
private JTextArea txtAreaCart;
private double item1Price = 10.0;
private double item2Price = 20.0;
private double item3Price = 30.0;
private double item4Price = 40.0;
public ShoppingCart() {
super("购物结账页面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700, 500);
setLayout(new BorderLayout());
// 标题面板
lblTitle = new JLabel("欢迎来到我的商店!");
lblTitle.setFont(new Font("Serif", Font.BOLD, 20));
JPanel pnlTitle = new JPanel();
pnlTitle.add(lblTitle);
add(pnlTitle, BorderLayout.NORTH);
// 商品面板
pnlItems = new JPanel();
pnlItems.setLayout(new GridLayout(4, 2));
lblItem1 = new JLabel("商品1 ($" + item1Price + "):");
txtItem1 = new JTextField(5);
lblItem2 = new JLabel("商品2 ($" + item2Price + "):");
txtItem2 = new JTextField(5);
lblItem3 = new JLabel("商品3 ($" + item3Price + "):");
txtItem3 = new JTextField(5);
lblItem4 = new JLabel("商品4 ($" + item4Price + "):");
txtItem4 = new JTextField(5);
pnlItems.add(lblItem1);
pnlItems.add(txtItem1);
pnlItems.add(lblItem2);
pnlItems.add(txtItem2);
pnlItems.add(lblItem3);
pnlItems.add(txtItem3);
pnlItems.add(lblItem4);
pnlItems.add(txtItem4);
add(pnlItems, BorderLayout.CENTER);
// 按钮面板
btnAdd = new JButton("添加商品");
btnAdd.addActionListener(this);
btnRemove = new JButton("删除商品");
btnRemove.addActionListener(this);
btnCheckout = new JButton("结账");
btnCheckout.addActionListener(this);
pnlButtons = new JPanel();
pnlButtons.add(btnAdd);
pnlButtons.add(btnRemove);
pnlButtons.add(btnCheckout);
add(pnlButtons, BorderLayout.SOUTH);
// 购物车文本区域
txtAreaCart = new JTextArea();
add(new JScrollPane(txtAreaCart), BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e) {
String btnText = e.getActionCommand();
if (btnText.equals("添加商品")) {
addItem();
} else if (btnText.equals("删除商品")) {
removeItem();
} else if (btnText.equals("结账")) {
checkout();
}
}
private void addItem() {
StringBuilder cart = new StringBuilder(txtAreaCart.getText());
double total = 0;
if (!txtItem1.getText().isEmpty()) {
int quantity = Integer.parseInt(txtItem1.getText());
if (quantity > 0) {
double price = quantity * item1Price;
cart.append("商品1 x ").append(quantity).append(" = $").append(price).append("\n");
total += price;
}
}
if (!txtItem2.getText().isEmpty()) {
int quantity = Integer.parseInt(txtItem2.getText());
if (quantity > 0) {
double price = quantity * item2Price;
cart.append("商品2 x ").append(quantity).append(" = $").append(price).append("\n");
total += price;
}
}
if (!txtItem3.getText().isEmpty()) {
int quantity = Integer.parseInt(txtItem3.getText());
if (quantity > 0) {
double price = quantity * item3Price;
cart.append("商品3 x ").append(quantity).append(" = $").append(price).append("\n");
total += price;
}
}
if (!txtItem4.getText().isEmpty()) {
int quantity = Integer.parseInt(txtItem4.getText());
if (quantity > 0) {
double price = quantity * item4Price;
cart.append("商品4 x ").append(quantity).append(" = $").append(price).append("\n");
total += price;
}
}
cart.append("总计:$").append(total).append("\n");
txtAreaCart.setText(cart.toString());
}
private void removeItem() {
txtItem1.setText("");
txtItem2.setText("");
txtItem3.setText("");
txtItem4.setText("");
}
private void checkout() {
JOptionPane.showMessageDialog(this, "感谢光临!您的订单已提交。");
txtAreaCart.setText("");
txtItem1.setText("");
txtItem2.setText("");
txtItem3.setText("");
txtItem4.setText("");
}
public static void main(String[] args) {
ShoppingCart shoppingCart = new ShoppingCart();
shoppingCart.setVisible(true);
}
}
```
这个页面有一个欢迎标签,四个文本框和三个按钮,分别用于添加商品,删除商品和结账。它还有一个文本区域,用于显示购物车中的商品列表和总价。当用户单击添加商品按钮时,该程序将获取每个文本框中的商品数量,并将其添加到购物车中。当用户单击删除商品按钮时,文本框将被清空。当用户单击结账按钮时,程序将弹出一个对话框,显示感谢信息,并将购物车和文本框清空。
希望这个简单的代码能够帮助到你。
阅读全文