Home / Expert Answers / Computer Science / i-am-creating-a-simple-gui-in-java-for-a-bank-account-and-i-want-to-add-an-addbalance-button-with-a-pa809

(Solved): I am creating a simple GUI in Java for a bank account and I want to add an addBalance button with a ...



I am creating a simple GUI in Java for a bank account and I want to add an addBalance button with a text box so that users can initialize their balance instead of having to start by making an initial deposit first. Once they add their balance it should display in the current balance text box area. How do I do that? Also if you have any design ideas feel free to toss that in as well. I appreciate any knowledge you can give me. Thank you and have a wonderful day!

Screenshot of GUI:

student submitted image, transcription available below

Source Code:

package bankAccount;

import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class GUIBankAccount extends JPanel implements ActionListener {
JButton deposit = new JButton("deposit");
JButton withdrawal = new JButton("Withdrawal");
JButton exit = new JButton("Exit");

TextField t1, t2, output;
Label debitaccount, creditaccount, balanceamount;
double accountBalance = 0.0;
public GUIBankAccount(JFrame frame) {

debitaccount = new Label("Deposit");
debitaccount.setBounds(90, 30, 60, 50);
frame.add(debitaccount);

t1 = new TextField("");
t1.setBounds(150, 40, 150, 30);
frame.add(t1);

creditaccount = new Label("Withdrawal");
creditaccount.setBounds(80, 115, 60, 50);
frame.add(creditaccount);

t2 = new TextField("");
t2.setBounds(150, 125, 150, 30);
frame.add(t2);

deposit.addActionListener(this);
deposit.setBounds(450, 40, 150, 30);
frame.add(deposit);

withdrawal.addActionListener(this);
withdrawal.setBounds(450, 125, 150, 30);
frame.add(withdrawal);

balanceamount = new Label("Balance");
balanceamount.setBounds(90, 197, 60, 30);
frame.add(balanceamount);

output = new TextField("");
output.setBounds(150, 200, 150, 30);
frame.add(output);

exit.addActionListener(this);
exit.setBounds(450, 200, 150, 30);
frame.add(exit);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new GUIBankAccount(frame));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 400);
frame.setTitle("Bank Account GUI");
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("deposit")) {
System.out.println("Debit has been clicked");
String balanceamount = t1.getText();
double debitAmount = Double.parseDouble(balanceamount);

accountBalance = accountBalance + debitAmount;
output.setText(String.valueOf(accountBalance));
t1.setText("");
}
else if (e.getActionCommand().equals("Withdrawal")) {
System.out.println("withdrawal has been clicked");

String balanceamount = t2.getText();
double creditamount = Double.parseDouble(balanceamount);

accountBalance = accountBalance - creditamount;
output.setText(String.valueOf(accountBalance));
t2.setText("");
}
else if(e.getActionCommand().equals("Exit")) {
System.out.println("You are exitied : Bye");
System.exit(0);
}
}
}

Bank Account GUI Deposit Withdrawal Withdrawal Balance


We have an Answer from Expert

View Expert Answer

Expert Answer



In your code, there are a couple of issues:
Missing Initialization: You have declared the TextField v...
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe