所以我正在创建一个标准银行账户余额为 500 的程序。该程序询问用户是否要提取或存入资金,然后计算他们提取或存入的金额并更新当前余额。为什么它不起作用,我将如何解决它?
public class MyFrame extends JFrame {
private JPanel panel;
private JLabel wordsLabel;
private JLabel balanceLabel;
private JLabel choiceLabel;
private JTextField transactionAmount;
private JButton depositButton;
private JButton withdrawButton;
private double balance;
public MyFrame() {
final int FIELD_WIDTH = 10;
balance = 500;
panel = new JPanel();
wordsLabel = new JLabel();
balanceLabel = new JLabel();
choiceLabel = new JLabel();
transactionAmount = new JTextField(FIELD_WIDTH);
JPanel buttonPanel = new JPanel();
ButtonGroup myGroup = new ButtonGroup();
//panel.setLayout(new BorderLayout());
depositButton = new JButton("Deposit");
withdrawButton = new JButton("Withdraw");
transactionAmount.setText("0");
wordsLabel.setText("Welcome to Wes Banco! Your current balance is: ");
balanceLabel.setText("500");
choiceLabel.setText("How much would you like to deposit/withdraw?");
panel.add(wordsLabel);
panel.add(balanceLabel);
panel.add(choiceLabel);
panel.add(transactionAmount);
myGroup.add(depositButton);
myGroup.add(withdrawButton);
buttonPanel.add(depositButton);
buttonPanel.add(withdrawButton);
panel.add(depositButton);
ButtonListener myListener = new ButtonListener();
depositButton.addActionListener(myListener);
withdrawButton.addActionListener(myListener);
panel.add(buttonPanel);
this.add(panel);
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
double amount = Double.parseDouble(transactionAmount.getText());
if (amount == 0) {
JOptionPane.showMessageDialog(null, "Enter an amount");
}
if (depositButton.isSelected()) {
balanceLabel.setText("" + 500 + amount);
JOptionPane.showMessageDialog(null,
"You have deposited: " + amount);
}
if (withdrawButton.isSelected()) {
}
}
}
}