You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Swing表单Deposit按钮内层if语句不执行问题排查

问题分析与解决方案

你的Deposit按钮内层if语句不执行的核心原因是:当点击Deposit按钮时,事件源e.getSource()指向的是depositButton,而不是之前选择的Personal/Business/Community按钮,所以内层判断e.getSource()==personal这类条件永远为false,自然不会执行对应的代码块。

解决步骤:

  1. 添加成员变量记录用户选择的账户类型
  2. 在点击Personal/Business/Community按钮时,给该变量赋值
  3. 点击Deposit按钮时,根据记录的账户类型判断逻辑,而非事件源
  4. 修复余额计算的静态问题:用户输入存款金额后,需动态更新余额显示

修改后的完整代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.sql.*;

public class WelcomeNDeposit extends JFrame implements ActionListener {

    JLabel userId, message, notePs, personalOpeningMsg, busOpenMsg, 
           commOpenMsg, accountMsg, balanceMsg;
    JButton personal, business, community, depositButton, mainMenu;
    JTextField orgName, depositAmount;
    JSeparator sep, sep1;

    int min = 10000001;
    int max = 99999999;
    int accountNo = (int) Math.floor(Math.random() * (max - min + 1) + min);
    int overdraft = 1500;  
    // 记录选中的账户类型
    private String selectedAccount;

    Connection con = Model.connect();
    Statement stmt;
    ResultSet rs;
    String userTitle, userLName;

    public WelcomeNDeposit(User user) {
        this.setSize(600, 600);
        this.setTitle("Welcome");
        this.setLayout(null);

        userId = new JLabel("User ID: " + user.id);
        userId.setSize(250, 30);
        userId.setLocation(350, 15);

        message = new JLabel("<Html>Welcome " + user.title + " " + user.lastName + "<p><br> "
                + "Please select if you are applying as an individual, Business or Community Centre</p></Html>");
        message.setSize(540, 180);
        message.setLocation(10, 20);
        
        personal = new JButton("Personal");
        personal.setText("Individual");
        personal.setSize(120, 40);
        personal.setLocation(80, 135);
        personal.addActionListener(this);
        this.add(personal);

        business = new JButton();
        business.setText("Business");
        business.setSize(120, 40);
        business.setLocation(80, 170);
        business.addActionListener(this);
        this.add(business);

        community = new JButton();
        community.setText("Community");
        community.setSize(120, 40);
        community.setLocation(80, 200);
        community.addActionListener(this);
        this.add(community);

        notePs = new JLabel("Ps. You will be able to add other type of accounts later");
        notePs.setSize(500, 50);
        notePs.setLocation(50, 230);
        this.add(notePs);

        sep = new JSeparator();
        sep.setSize(420, 15);
        sep.setLocation(70, 270);

        personalOpeningMsg = new JLabel("<html>You are qualified to £1,500 overdraft facility, "
                + "with an initial deposit of £1,500<p>                     </p></html>");
        personalOpeningMsg.setSize(540, 50);
        personalOpeningMsg.setLocation(30, 280);
        personalOpeningMsg.setVisible(false);

        busOpenMsg = new JLabel("<html>Your Business qualifies to £2,500 overdraft facility, "
                + "with an initial deposit of £2,500<p><br>What is the name of your Company?</p></html>");
        busOpenMsg.setSize(540, 50);
        busOpenMsg.setLocation(30, 280);
        busOpenMsg.setVisible(false);

        commOpenMsg = new JLabel("<html>Your Community Centre qualifies to £1,000 overdraft facility, "
                + "with an initial deposit of £1,000<p> <br> Please provide the Community Centre name "
                + "below </p></html>");
        commOpenMsg.setSize(540, 50);
        commOpenMsg.setLocation(30, 280);
        commOpenMsg.setVisible(false);

        orgName = new JTextField();
        orgName.setSize(240, 25);
        orgName.setLocation(250, 305);
        orgName.setVisible(false);

        accountMsg = new JLabel("How much would you like to deposit?");
        accountMsg.setSize(210, 50);
        accountMsg.setLocation(30, 330);
        accountMsg.setVisible(false);

        depositAmount = new JTextField("0");
        depositAmount.setSize(220, 25);
        depositAmount.setLocation(260, 340);
        depositAmount.setVisible(false);
        depositAmount.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e3) {
                char c = e3.getKeyChar();
                if (((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
                    e3.consume(); // 非数字输入忽略
                }
            }
        });
        this.add(depositAmount);
        
        depositButton = new JButton();
        depositButton.setText("Deposit");
        depositButton.setSize(120, 40);
        depositButton.setLocation(200, 370);
        depositButton.addActionListener(this);
        depositButton.setVisible(false);
        this.add(depositButton);

        sep1 = new JSeparator();
        sep1.setSize(420, 20);
        sep1.setLocation(60, 420);
        sep1.setVisible(false);
        
        balanceMsg = new JLabel();
        balanceMsg.setSize(450, 150); 
        balanceMsg.setLocation(30, 380); 
        balanceMsg.setVisible(false);
        this.add(balanceMsg); 

        this.add(userId);
        this.add(message);
        this.add(personal);
        this.add(business);
        this.add(community);
        this.add(personalOpeningMsg);
        this.add(busOpenMsg);
        this.add(commOpenMsg);
        this.add(orgName);
        this.add(accountMsg);

        this.setVisible(true);

        this.add(sep);
        this.add(sep1);

        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("select * from registrationAtm");
            while (rs.next()) {
                userTitle = rs.getString("personal_title");
                userLName = rs.getString("personal_lastName");
            }
            rs.close();
            stmt.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == personal) {
            JOptionPane.showMessageDialog(null,
                    "You are qualified to £1,500 overdraft " + "facility, with an initial deposit of £1,500");
            personalOpeningMsg.setVisible(true);
            accountMsg.setVisible(true);
            depositAmount.setVisible(true);
            depositButton.setVisible(true);
            selectedAccount = "personal";
            overdraft = 1500;
        } else if (e.getSource() == business) {
            JOptionPane.showMessageDialog(null,
                    "Your Business qualifies to £2,500 overdraft " + "facility, with an initial deposit of £2,500");
            busOpenMsg.setVisible(true);
            orgName.setVisible(true);
            accountMsg.setVisible(true);
            depositAmount.setVisible(true);
            depositButton.setVisible(true);
            selectedAccount = "business";
            overdraft = 2500;
        } else if (e.getSource() == community) {
            JOptionPane.showMessageDialog(null, "Your Community Centre qualifies to £1,000 overdraft "
                    + "facility, with an initial deposit of £1,000");
            commOpenMsg.setVisible(true);
            orgName.setVisible(true);
            accountMsg.setVisible(true);
            depositAmount.setVisible(true);
            depositButton.setVisible(true);
            selectedAccount = "community";
            overdraft = 1000;
        }
        
        if(e.getSource() == depositButton) {
            System.out.print("Deposited");
            sep1.setVisible(true);
            
            if("personal".equals(selectedAccount)) {
                updateBalanceMsg();
                balanceMsg.setVisible(true);
            }else if ("business".equals(selectedAccount)) {
                updateBalanceMsg();
                balanceMsg.setVisible(true); 
            }else if ("community".equals(selectedAccount)) {
                updateBalanceMsg();
                balanceMsg.setVisible(true);
            }
        }   
    }

    // 动态更新余额信息
    private void updateBalanceMsg() {
        try {
            int depositAmountValue = Integer.parseInt(depositAmount.getText());
            int balance = overdraft + depositAmountValue;
            NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(new Locale("en", "UK"));
            balanceMsg.setText("<html> Your SortCode is 12-34-56, Account number is"
                    + " " + accountNo +"<p><br> Your available balance is " + currencyFormat.format(balance) + "</p></html>");
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "请输入有效的存款金额");
        }
    }

    public static void main(String[] args) {
        new LoginPage();
    }
}

额外修复点:

  • 修正了HTML标签闭合错误和拼写错误(inital改为initial)
  • 将余额标签改为动态生成,确保显示最新的存款金额
  • 添加了存款金额输入的异常处理,避免非数字输入崩溃

内容的提问来源于stack exchange,提问作者Glen Brook

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 01:45:33