From 5cc7c67be807be7109db54366123865eebe1dd26 Mon Sep 17 00:00:00 2001 From: lx <3151245148@qq.com> Date: Thu, 18 Sep 2025 22:15:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E6=AC=A1=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/Home.java | 2 +- src/main/java/com/example/Main.java | 3 ++- .../com/example/SalesProductManagement.java | 27 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/Home.java b/src/main/java/com/example/Home.java index f5fdc4f..50d58b8 100644 --- a/src/main/java/com/example/Home.java +++ b/src/main/java/com/example/Home.java @@ -12,7 +12,7 @@ public class Home extends JFrame implements ActionListener { JMenuItem StockMenuItem,InventoryMenuItem,SalesMenuItem; Home() { - super("城市进销存信息管理系统"); + super("超市进销存信息管理系统"); menuBar = new JMenuBar(); menu = new JMenu("菜单"); StockMenuItem = new JMenuItem("库存信息管理"); diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index a3b0c11..1b7399b 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -49,7 +49,7 @@ public class Main extends JFrame implements ActionListener { String sql = "select * from user where username='"+tf1.getText()+"' and password='"+tf2.getText()+"'"; ; try { ResultSet rs = jdbc.query(sql); - jdbc.close(); + if(rs.next()&&tf3.getText().equals(jl5.getText())) { JOptionPane.showMessageDialog(null, "登录成功"); new Home(); @@ -70,4 +70,5 @@ public class Main extends JFrame implements ActionListener { public static void main(String[] args) { new Main(); } + } \ No newline at end of file diff --git a/src/main/java/com/example/SalesProductManagement.java b/src/main/java/com/example/SalesProductManagement.java index 82eb3f5..2c1dbaf 100644 --- a/src/main/java/com/example/SalesProductManagement.java +++ b/src/main/java/com/example/SalesProductManagement.java @@ -37,6 +37,7 @@ public class SalesProductManagement extends JFrame implements ActionListener { text10=new JTextField(5); btn1=new JButton("结算"); + btn1.addActionListener(this); add(panel1); @@ -70,9 +71,35 @@ public class SalesProductManagement extends JFrame implements ActionListener { setSize(500,500); setLocationRelativeTo(null); setVisible(true); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 添加窗口关闭功能 } @Override public void actionPerformed(ActionEvent e) { + // 判断事件源是否为结算按钮 + if (e.getSource() == btn1) { + try { + // 获取单价(从文本框提取数字部分) + double applePrice = Double.parseDouble(text2.getText().replace("元", "")); + double bananaPrice = Double.parseDouble(text5.getText().replace("元", "")); + double orangePrice = Double.parseDouble(text8.getText().replace("元", "")); + // 获取数量(处理空输入情况) + int appleCount = text3.getText().trim().isEmpty() ? 0 : Integer.parseInt(text3.getText().trim()); + int bananaCount = text6.getText().trim().isEmpty() ? 0 : Integer.parseInt(text6.getText().trim()); + int orangeCount = text9.getText().trim().isEmpty() ? 0 : Integer.parseInt(text9.getText().trim()); + + // 计算总金额 + double total = (applePrice * appleCount) + (bananaPrice * bananaCount) + (orangePrice * orangeCount); + + // 显示结果,保留两位小数 + text10.setText(String.format("%.2f元", total)); + } catch (NumberFormatException ex) { + // 处理输入非数字的情况 + JOptionPane.showMessageDialog(this, "请输入有效的数字", "输入错误", JOptionPane.ERROR_MESSAGE); + } + } + } + public static void main(String[] args) { + new SalesProductManagement(); } }