そのため、標準的な銀行口座残高が 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()) {
}
}
}
}