I am building a GUI for a bank as my project. I have the frame and panel built,
ID: 3818689 • 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 clear listener.
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;
if(event.getSource()==balance){
String withdrawText = withdraw.getText();
String depositText = deposit.getText();
w = Double.parseDouble(withdrawText);
d = Double.parseDouble(depositText);
//Remaining balance calculation
r = 5826 + d - w;
//Remaining balance is set to Balance button
//You must enter both the deposit and withdraw to get the remaining balance
//enter 0 in withdraw if you only want to deposit and vice versa.
remaining.setText(String.valueOf(r));
// Clear push button-- listener
private class ClearListener implements ActionListener
{
public void actionPerformed (ActionEvent event){
//clear info from text fields
withdraw.setText("");
deposit.setText("");
remaining.setText("");
//clear info from balance label
remainingL.setText("");
}
}
}
}
}
}
Explanation / Answer
//Update code!! It works..
//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;
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);
}
public class remainingListener implements ActionListener
{
public void actionPerformed (ActionEvent event){
double w, d, r;
if(event.getSource()==balance){
String withdrawText = withdraw.getText();
String depositText = deposit.getText();
w = Double.parseDouble(withdrawText);
d = Double.parseDouble(depositText);
//Remaining balance calculation
r = 5826 + d - w;
//Remaining balance is set to Balance button
//You must enter both the deposit and withdraw to get the remaining balance
//enter 0 in withdraw if you only want to deposit and vice versa.
remaining.setText(String.valueOf(r));
} }}
// Clear push button-- listener
public class ClearListener implements ActionListener
{
public void actionPerformed (ActionEvent event){
//clear info from text fields
withdraw.setText("");
deposit.setText("");
remaining.setText("");
//clear info from balance label
remainingL.setText("");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.