用Java swing写一个代码,做一个超市商品结算界面并可以通过坐标调整,读取文件1.txt,文件包括商品名,商品数量,商品价格,可以通过搜索商品名称,显示该商品的信息,点击添加键,则将选择的商品展示在小的文本框,包括商品名,商品数量,商品价格,其中商品数量可以进行修改,并且计算出所有商品总价,点击结算按钮后,跳转至新界面

时间: 2024-04-18 13:33:33 浏览: 6
以下是一个简单的Java Swing代码示例,实现了超市商品结算界面的功能: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class SupermarketCheckout { private JFrame frame; private JTextField searchField, nameField, quantityField, priceField; private JTextArea cartArea; private JLabel totalPriceLabel; private JButton addButton, checkoutButton; private double totalPrice; public SupermarketCheckout() { frame = new JFrame("Supermarket Checkout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 400); frame.setLayout(new BorderLayout()); JPanel topPanel = new JPanel(); topPanel.setLayout(new FlowLayout()); JLabel searchLabel = new JLabel("Search:"); searchField = new JTextField(15); JButton searchButton = new JButton("Search"); searchButton.addActionListener(new SearchButtonListener()); topPanel.add(searchLabel); topPanel.add(searchField); topPanel.add(searchButton); JPanel centerPanel = new JPanel(); centerPanel.setLayout(new GridLayout(4, 2)); JLabel nameLabel = new JLabel("Name:"); nameField = new JTextField(15); nameField.setEditable(false); JLabel quantityLabel = new JLabel("Quantity:"); quantityField = new JTextField(15); JLabel priceLabel = new JLabel("Price:"); priceField = new JTextField(15); priceField.setEditable(false); addButton = new JButton("Add"); addButton.setEnabled(false); addButton.addActionListener(new AddButtonListener()); centerPanel.add(nameLabel); centerPanel.add(nameField); centerPanel.add(quantityLabel); centerPanel.add(quantityField); centerPanel.add(priceLabel); centerPanel.add(priceField); centerPanel.add(addButton); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new BorderLayout()); JLabel cartLabel = new JLabel("Cart:"); cartArea = new JTextArea(10, 30); cartArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(cartArea); JPanel totalPricePanel = new JPanel(); totalPricePanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); JLabel totalPriceTextLabel = new JLabel("Total Price:"); totalPriceLabel = new JLabel("0.0"); totalPricePanel.add(totalPriceTextLabel); totalPricePanel.add(totalPriceLabel); bottomPanel.add(cartLabel, BorderLayout.NORTH); bottomPanel.add(scrollPane, BorderLayout.CENTER); bottomPanel.add(totalPricePanel, BorderLayout.SOUTH); checkoutButton = new JButton("Checkout"); checkoutButton.addActionListener(new CheckoutButtonListener()); frame.add(topPanel, BorderLayout.NORTH); frame.add(centerPanel, BorderLayout.CENTER); frame.add(bottomPanel, BorderLayout.SOUTH); frame.add(checkoutButton, BorderLayout.EAST); frame.setVisible(true); } private void searchProduct(String name) { try { Scanner scanner = new Scanner(new File("1.txt")); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] parts = line.split(","); if (parts[0].equals(name)) { nameField.setText(parts[0]); quantityField.setText(parts[1]); priceField.setText(parts[2]); addButton.setEnabled(true); return; } } // If product not found nameField.setText(""); quantityField.setText(""); priceField.setText(""); addButton.setEnabled(false); JOptionPane.showMessageDialog(frame, "Product not found!"); } catch (FileNotFoundException e) { e.printStackTrace(); JOptionPane.showMessageDialog(frame, "File not found!"); } } private void addToCart() { String name = nameField.getText(); int quantity = Integer.parseInt(quantityField.getText()); double price = Double.parseDouble(priceField.getText()); double subtotal = quantity * price; cartArea.append(name + " x " + quantity + " - $" + subtotal + "\n"); totalPrice += subtotal; totalPriceLabel.setText(String.valueOf(totalPrice)); } private void checkout() { frame.dispose(); JFrame checkoutFrame = new JFrame("Checkout"); checkoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); checkoutFrame.setSize(300, 200); checkoutFrame.setLayout(new FlowLayout()); JLabel thankYouLabel = new JLabel("Thank you for shopping!"); JLabel totalLabel = new JLabel("Total Price: $" + totalPrice); checkoutFrame.add(thankYouLabel); checkoutFrame.add(totalLabel); checkoutFrame.setVisible(true); } private class SearchButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { String productName = searchField.getText(); searchProduct(productName); } } private class AddButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { addToCart(); } } private class CheckoutButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { checkout(); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new SupermarketCheckout(); } }); } } ``` 请确保在运行代码之前,将名为`1.txt`的商品信息文件放置在与代码文件相同的目录下,并按照以下格式编写文件内容: ``` 商品1,数量1,价格1 商品2,数量2,价格2 ... ``` 运行代码后,将会弹出一个窗口显示超市商品结算界面。你可以输入商品名称并点击搜索按钮来查找商品信息。找到商品后,它的名称、数量和价格将显示在相应的文本框中。你可以修改数量并点击“添加”按钮将商品添加到购物车中。购物车中的商品将显示在文本区域中,并计算出总价。当你点击“结算”按钮时,将会跳转到一个新的界面显示感谢信息和总价。 请注意,这只是一个简单的示例,可能需要根据具体需求进行进一步的修改和完善。

相关推荐

最新推荐

recommend-type

Java swing读取txt文件实现学生考试系统

主要为大家详细介绍了Java swing读取txt文件实现学生考试系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Java Swing组件文件选择器JFileChooser简单用法示例

主要介绍了Java Swing组件文件选择器JFileChooser简单用法,结合实例形式分析了Swing组件中的文件选择器JFileChooser的简单使用方法,需要的朋友可以参考下
recommend-type

情人节写给女朋友Java Swing代码程序

主要为大家分享了情人节写给女朋友的java小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,祝大家每天都是情人节
recommend-type

基于Yolov5的旋转检测

旋转检测 要求 torch==1.6 shapely==1.7.1 opencv==4.2.0.34
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依