Related questions
Concept explainers
How can I / where can I add more / better comments and improve the overall Java code structure to enhance its readability and maintainability.
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;
public class GUIBankAccount extends JPanel implements ActionListener { //Detects when the user clicks an on screen button on panel.
//New Jbuttons
JButton deposit = new JButton("Deposit");
JButton withdrawal = new JButton("Withdrawal");
JButton balance = new JButton("Add Balance");
JButton exit = new JButton("Exit");
TextField depo, withdraw, bal, output; //Text fields
Label depositAccount, withdrawalAccount, addBalance, balanceTotal; //Labels
double accountBalance = 0.0; //initializing balance to zero.
public GUIBankAccount(JFrame frame) {
//Deposit section.
//Creating labels, boundaries, text box, action listener and adding them all to frame.
depositAccount = new Label("Deposit");
depositAccount.setBounds(90, 115, 60, 50);
frame.add(depositAccount);
depo = new TextField("");
depo.setBounds(150, 125, 150, 30);
frame.add(depo);
deposit.addActionListener(this);
deposit.setBounds(450, 125, 150, 30);
frame.add(deposit);
withdrawalAccount = new Label("Withdrawal");
withdrawalAccount.setBounds(80, 190, 60, 50);
frame.add(withdrawalAccount);
withdraw = new TextField("");
withdraw.setBounds(150, 200, 150, 30);
frame.add(withdraw);
withdrawal.addActionListener(this);
withdrawal.setBounds(450, 200, 150, 30);
frame.add(withdrawal);
addBalance = new Label("Add Balance");
addBalance.setBounds(70, 40, 70, 30);
frame.add(addBalance);
bal = new TextField("");
bal.setBounds(150, 40, 150, 30);
frame.add(bal);
balance.addActionListener(this);
balance.setBounds(450, 40, 150, 30);
frame.add(balance);
balanceTotal = new Label("Balance");
balanceTotal.setBounds(90, 280, 60, 30);
frame.add(balanceTotal);
output = new TextField("");
output.setBounds(150, 280, 150, 30);
frame.add(output);
exit.addActionListener(this);
exit.setBounds(450, 280, 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("Deposit button has been clicked");
String balanceTotal = depo.getText(); //Getting input.
double depositAccount = Double.parseDouble(balanceTotal); //Method returns a new double initialized to the value.
accountBalance = accountBalance + depositAccount; //Adding funds to balance.
output.setText(String.valueOf(accountBalance)); //Setting new value
depo.setText("");//Clearing text in deposit text box.
}
//Withdrawal section
else if (e.getActionCommand().equals("Withdrawal")) {
System.out.println("Withdrawal button has been clicked");
String balanceTotal = withdraw.getText();
double withdrawalAccount = Double.parseDouble(balanceTotal);
accountBalance = accountBalance - withdrawalAccount;
output.setText(String.valueOf(accountBalance));
withdraw.setText("");
}
else if (e.getActionCommand().equals("Add Balance")) {
System.out.println("Add Balance button has been clicked");
String balanceTotal = bal.getText();
double addBalance = Double.parseDouble(balanceTotal);
accountBalance = accountBalance + addBalance;
output.setText(String.valueOf(accountBalance));
bal.setText("");
}
else if(e.getActionCommand().equals("Exit")) {
System.out.println("User has pressed exit. Good Bye");
System.exit(0);
}
}
}
Step by stepSolved in 4 steps with 8 images
- Please help me fix the java program below. I keep on getting hello world when I run it AnimatedBall.java import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class AnimatedBall extends JFrame implements ActionListener{private Button btnBounce; // declare a button to playprivate TextField txtSpeed; // declare a text field to enterprivate Label lblDelay;private Panel controls; // generic panel for controlsprivate BouncingBall display; // drawing panel for ballContainer frame;public AnimatedBall (){// Set up the controls on the appletframe = getContentPane();btnBounce = new Button ("Bounce Ball");//create the objectstxtSpeed = new TextField("10000", 10);lblDelay = new Label("Enter Delay");display = new BouncingBall();//create the panel objectscontrols = new Panel();setLayout(new BorderLayout()); // set the frame layoutcontrols.add (btnBounce); // add controls to panelcontrols.add (lblDelay);controls.add...arrow_forwardSolve the following recurrence relationship M(n) = 3M(n-1) + 1, M(0) = 5arrow_forwardCreate a class RectangleExample in eclipse. Copy the following code into the class. RectangleExample.java package mypackage; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.Rectangle; public class RectangleExample extends Application { Rectangle rect1, rect2; @Override public void start(Stage stage) { Group root = new Group(); //Drawing a Rectangle rect1 = new Rectangle(); //Setting the properties of the rectangle rect1.setX(20.0f); rect1.setY(20.0f); rect1.setWidth(300.0f); rect1.setHeight(10.0f); rect1.setFill(javafx.scene.paint.Color.RED); //Add to a Group object root.getChildren().add(rect1); //Drawing a Rectangle rect2 = new Rectangle(); //Setting the properties of the rectangle rect2.setX(20.0f); rect2.setY(40.0f); rect2.setWidth(200.0f); rect2.setHeight(10.0f); rect2.setFill(javafx.scene.paint.Color.GREEN); //Add to a Group object root.getChildren().add(rect2); //Creating a scene...arrow_forward
- The first one is answered as follows. Help with the second one. package test;import javax.swing.*;import java.awt.*;import java.awt.event.*; public class JavaApplication1 { public static void main(String[] arguments) { JFrame.setDefaultLookAndFeelDecorated(true);JFrame frame = new JFrame("print X and Y Coordinates");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout());frame.setSize(500,400); final JTextField output = new JTextField();;frame.add(output,BorderLayout.SOUTH); frame.addMouseListener(new MouseListener() {public void mousePressed(MouseEvent me) { }public void mouseReleased(MouseEvent me) { }public void mouseEntered(MouseEvent me) { }public void mouseExited(MouseEvent me) { }public void mouseClicked(MouseEvent me) {int x = me.getX();int y = me.getY();output.setText("Coordinate X:" + x + "|| Coordinate Y:" + y);}}); frame.setVisible(true);}}arrow_forwardThere is something wrong with this Java program but I do not know what it is. can someone help 2 import java.awt.*; 3 import javax.swing.*; 4 import java.awt.event.*; 5 import java.awt.Color; 6 7 8 class Radiobutton extends JFrame { 9 10 11 JRadioButton jRadioButton1; 12 JRadioButton jRadioButton2; 13 JButton jButton; 14 ButtonGroup G1; 15 JLabel L1; 16 17 public Radiobutton() 18 { 19 this.setLayout(null); 20 jRadioButton1 = new JRadioButton(); 21 jRadioButton2 = new JRadioButton(); 22 jButton = new JButton("Click"); 23 G1 = new ButtonGroup(); 24 L1 = new JLabel("Qualification"); 25 26 jRadioButton1.setText("Under-Graduate"); 27 jRadioButton2.setText("Graduate"); 28 29 jRadioButton1.setBounds(120, 30, 120, 50); 30 jRadioButton2.setBounds(250, 30, 80, 50); 31 jButton.setBounds(125, 90, 80, 30); 32 L1.setBounds(20, 30, 150, 50); 33 34 this.add(jRadioButton1); 35...arrow_forwardPlease help me create a matching game using java and 2D arrays. Create a reset button and titlearrow_forward
- I know it is a lot but can someone explain (with comments) line by line what is happening in the below Java program. It assists in my learning/gives me a deeper understanding. Please and thank you! Source code: import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.SwingUtilities; class ProductCalculator { public static int calculateProduct(int[] numbers, int index) { if (index == numbers.length - 1) { return numbers[index]; } else { return numbers[index] * calculateProduct(numbers, index + 1); } }} class RecursiveProductCalculatorView extends JFrame { private JTextField[] numberFields; private JButton calculateButton; private JLabel resultLabel; public RecursiveProductCalculatorView() { setTitle("Recursive Product Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(7, 1)); numberFields =...arrow_forwardGuidelines are creating a cardlayout with 3 cards: login, fred and bob. import java.awt.event.*;import javax.swing.*;import java.awt.*;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.FlowLayout;import java.awt.Color;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener; public class SimpleCardWindow extends JFrame implements ActionListener {public static final int WIDTH = 300;public static final int HEIGHT = 300; private CardLayout theCardsLayoutManager;private JPanel theCardsPanel;private JTextField userTextField;private JTextField passwordTextField;private JButton loginButton;private JButton bobWordButton;private JTextField bobEntryTextField;private JTextArea bobDisplayTextArea;private JButton bobReturnButton;private JPanel fredCard;private JPanel fredButtonPanel;private JButton fredRedColorButton;private JButton...arrow_forwardWhen comparing the java.io and java.nio packages, what distinctions should be made?arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education