Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am building a GUI for a bank as my project. I have the frame and panel built,

ID: 3818527 • Letter: I

Question

I am building a GUI for a bank as my project. I have the frame and panel built, but am having trouble with the listeners. I am trying to get it so that when you enter the amount you want to withdraw or deposit and click the Balance button, it adds or subtracts the value you entered from your balance.

BankFrame.java

//Date
//Author:

import javax.swing.JFrame;

public class BankFrame {

   public static void main(String[] args) {
      
       //Object with frame title
       JFrame frame = new JFrame("Meal Selector");
      
       //Close button
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      
       Bankpanel panel = new Bankpanel();
       frame.getContentPane().add(panel);
      
       //size of frame
       frame.setSize(500, 1000);
      
       //make frame visible
       frame.setVisible(true);

   }

}

Bankpanel.java

//Date:
//Author:
//Purpose: To display my ability to create and build a Graphic User Interface

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.util.*;
import java.text.NumberFormat;

public class BankPanel extends JPanel {

   //1. declare instance variables in panel class level
   // create components
   private JLabel balanceL, optionL, withdrawL, depositL, remainingL;
   private JTextField withdraw, deposit, remaining;
   private JButton balance, Clear;
   private JRadioButton withdrawR, depositR;
  
   //panel constructor
   BankPanel(){
      
       //List of components
       //Labels
       balanceL = new JLabel ("Bank account balance: $5826");
       optionL = new JLabel ("Select your option: ");
       withdrawL = new JLabel ("How much would you like to withdraw?: ");
       depositL = new JLabel ("How much would you like to deposit?: ");
       remainingL = new JLabel ("Remaining balance: ");
      
       //Text fields
       withdraw = new JTextField(10);
       deposit = new JTextField(10);
       remaining = new JTextField(10);
      
       //Buttons
       balance = new JButton ("Balance");
       Clear = new JButton ("Clear");
      
       //Radio buttons (must be grouped together)
       withdrawR = new JRadioButton("Withdraw");
       depositR = new JRadioButton("Deposit");
      
       ButtonGroup transGroup = new ButtonGroup();
       transGroup.add(withdrawR);
       transGroup.add(depositR);
      
       //Listeners
       balance.addActionListener(new remainingListener());
       Clear.addActionListener(new ClearListener());
      
       //Adding components to panel
      
       add (balanceL);
      
       add (optionL);
       add (withdraw);
       add (deposit);
      
       add (withdrawL);
       add (withdraw);
      
       add (depositL);
       add (deposit);
      
       add (remainingL);
       add (remaining);
      
       add (balance);
       add (Clear);
      
       //Size and background for panel
       setPreferredSize(new Dimension(500, 1000));
       setBackground(Color.blue);
   }
  
   private class remainingListener implements ActionListener
   {
       public void actionPerformed (ActionEvent event){
          
           double w, d, r;
          
           String withdrawText = withdraw.getText();
           String depositText = deposit.getText();
          
          
           w = Double.parseDouble(withdrawText);
           d = Double.parseDouble(depositText);
          
          
           }
          
       }
}

Explanation / Answer

BankPanel.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.NumberFormat;
public class BankPanel extends JPanel
{
//1. declare instance variables in panel class level
// create components
private JLabel balanceL, optionL, withdrawL, depositL, remainingL;
private JTextField withdraw, deposit, remaining;
private JButton balance, Clear;
private JRadioButton withdrawR, depositR;
  
//panel constructor
BankPanel()
{
  
//List of components
//Labels
balanceL = new JLabel ("Bank account balance: $5826");
optionL = new JLabel ("Select your option: ");
withdrawL = new JLabel ("How much would you like to withdraw?: ");
depositL = new JLabel ("How much would you like to deposit?: ");
remainingL = new JLabel ("Remaining balance: ");
  
//Text fields
withdraw = new JTextField(10);
deposit = new JTextField(10);
remaining = new JTextField(10);
  
//Buttons
balance = new JButton ("Balance");
Clear = new JButton ("Clear");
  
//Radio buttons (must be grouped together)
withdrawR = new JRadioButton("Withdraw");
depositR = new JRadioButton("Deposit");
  
ButtonGroup transGroup = new ButtonGroup();
transGroup.add(withdrawR);
transGroup.add(depositR);
  
//Listeners
balance.addActionListener(new remainingListener());

//Clear.addActionListener(new ClearListener());
  
//Adding components to panel
  
add (balanceL);
  
add (optionL);
add (withdraw);
add (deposit);
  
add (withdrawL);
add (withdraw);
  
add (depositL);
add (deposit);
  
add (remainingL);
add (remaining);
  
add (balance);
add (Clear);
  
//Size and background for panel
setPreferredSize(new Dimension(500, 1000));
setBackground(Color.blue);
}
  
private class remainingListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
  
double w, d, r;

//When clicked on balance button

if(event.getSource() == balance)
{
  
     
   String withdrawText = withdraw.getText();
   String depositText = deposit.getText();
  
   w = Double.parseDouble(withdrawText);
   d = Double.parseDouble(depositText);
     
   // Remaining balance is calculated
     
   r=5826+d-w;
     
   // Remaining balance is set to balance button
   // You have to enter both the deposit and withdraw to get remaining balance
   //Or it will throw exception like if
   // you don't want to withdraw then withdraw=0 and so is deposit
   remaining.setText(String.valueOf(r));
}

  
}
  
}
}

BankFrame.java

import javax.swing.JFrame;
public class BankFrame {
public static void main(String[] args) {
  
//Object with frame title
JFrame frame = new JFrame("Meal Selector");
  
//Close button
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  
BankPanel panel = new BankPanel();
frame.getContentPane().add(panel);
  
//size of frame
frame.setSize(500, 1000);
  
//make frame visible
frame.setVisible(true);
}
}