Java Swing银行存取款窗口代码报错,try/catch放置位置是否正确?
问题排查
你的代码存在以下几类明确错误:
- 语法结构错误:
actionPerformed方法、第二个main方法直接定义在initialize方法内部,Java不允许方法嵌套定义普通方法;末尾多余多个闭合大括号;重复定义了两次main方法。 - 作用域错误:
- 余额变量
balance、余额显示标签totalBalance是initialize方法的局部变量,事件回调逻辑无法正常访问,需要提升为类的成员变量 - 三个按钮绑定的都是空的
ActionListener,你写的actionPerformed逻辑没有和按钮做绑定
- 余额变量
- 依赖缺失:没有导入
javax.swing.JOptionPane,调用弹窗时会报错 - 变量引用错误:调用
JOptionPane.showMessageDialog时第一个参数写了不存在的变量f,应该用当前的frame对象作为父窗口 - 命名错误:类名是
acmeBank,第二个main方法里实例化写的是ACMEBank,Java大小写敏感,找不到对应类
修正后的完整代码
import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.LayoutStyle.ComponentPlacement; // 类名遵循Java规范改为大驼峰 public class AcmeBank { private JFrame frame; private JTextField creditInput; private JTextField debitInput; // 提升为成员变量 private double balance = 0; private JLabel totalBalance; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { AcmeBank window = new AcmeBank(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public AcmeBank() { initialize(); } private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 470, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel lblNewLabel = new JLabel("DEPOSIT"); lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14)); JLabel lblNewLabel_1 = new JLabel("WITHDRAWAL"); lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14)); creditInput = new JTextField(); creditInput.setColumns(10); debitInput = new JTextField(); debitInput.setColumns(10); JButton creditButton = new JButton("CREDIT"); JButton debitButton = new JButton("DEBIT"); JButton clearButton = new JButton("CLEAR"); // 统一绑定监听器 ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == creditButton) { try { double amount = Double.parseDouble(creditInput.getText()); balance += amount; totalBalance.setText(String.valueOf(balance)); } catch (NumberFormatException ee) { JOptionPane.showMessageDialog(frame, "请输入合法数字"); } } else if (e.getSource() == debitButton) { try { double amount = Double.parseDouble(debitInput.getText()); if(amount > balance) { JOptionPane.showMessageDialog(frame, "余额不足"); return; } balance -= amount; totalBalance.setText(String.valueOf(balance)); } catch (NumberFormatException ee) { JOptionPane.showMessageDialog(frame, "请输入合法数字"); } } else if (e.getSource() == clearButton) { debitInput.setText(""); creditInput.setText(""); } } }; creditButton.addActionListener(listener); debitButton.addActionListener(listener); clearButton.addActionListener(listener); JLabel lblNewLabel_2 = new JLabel("BALANCE $"); lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 14)); totalBalance = new JLabel("0"); GroupLayout groupLayout = new GroupLayout(frame.getContentPane()); groupLayout.setHorizontalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false) .addGroup(groupLayout.createSequentialGroup() .addGap(47) .addComponent(lblNewLabel_2) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(totalBalance, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup() .addGap(18) .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) .addComponent(lblNewLabel, Alignment.TRAILING) .addComponent(lblNewLabel_1, Alignment.TRAILING)) .addGap(0) .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false) .addComponent(debitInput, Alignment.TRAILING) .addComponent(creditInput, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)))) .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) .addComponent(debitButton, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE) .addComponent(creditButton, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)) .addGap(18) .addComponent(clearButton, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE) .addContainerGap(39, Short.MAX_VALUE)) ); groupLayout.setVerticalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addGap(23) .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) .addComponent(lblNewLabel) .addComponent(creditInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(creditButton)) .addGap(11) .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE) .addComponent(lblNewLabel_1) .addComponent(debitInput, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(debitButton))) .addGroup(groupLayout.createSequentialGroup() .addGap(40) .addComponent(clearButton))) .addGap(33) .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING) .addComponent(lblNewLabel_2) .addComponent(totalBalance, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)) .addContainerGap(122, Short.MAX_VALUE)) ); frame.getContentPane().setLayout(groupLayout); } }
内容的提问来源于stack exchange,提问作者FingerLessB
相关产品推荐
相关产品推荐

