Hello, I am not a newbie in the sense that I just started but it has been a while since I have done any programming. I am making a "Savings account" program and I am having trouble with the GUI. What am I doing wrong? What should I do to fix it?
Here is my code
import java.awt.*; import javax.swing.*; import java.awt.event.*; class SavingsGUI extends JFrame implements ActionListener{ JButton deposit; JButton withdraw; JButton money; JButton num1; JButton num2; JButton num3; JButton num4; JButton num5; JButton num6; JButton num7; JButton num8; JButton num9; JButton num0; JButton num50; JButton num100; double balance = 0.0; // constructor for ButtonFrame SavingsGUI(String title) { super(title); setLayout(new FlowLayout()); deposit = new JButton("Deposit"); add(deposit); withdraw = new JButton("Withdraw"); add(withdraw); money = new JButton("Balance"); add(money); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); deposit.addActionListener(this); withdraw.addActionListener(this); money.addActionListener(this); withdraw.setActionCommand("w"); money.setActionCommand("b"); deposit.setActionCommand("d"); } public void actionPerformed(ActionEvent evt) { if (evt.getActionCommand().equals("d")) { //code for depositing JFrame dep = new JFrame("Deposit Amount"); setLayout(new FlowLayout()); num0 = new JButton("0"); num1 = new JButton("1"); num2 = new JButton("2"); num3 = new JButton("3"); num4 = new JButton("4"); num5 = new JButton("5"); num6 = new JButton("6"); num7 = new JButton("7"); num8 = new JButton("8"); num9 = new JButton("9"); num50 = new JButton("$50"); num100 = new JButton("$100"); dep.add(num7); dep.add(num8); dep.add(num9); dep.add(num4); dep.add(num5); dep.add(num6); dep.add(num1); dep.add(num2); dep.add(num3); dep.add(num50); dep.add(num0); dep.add(num100); dep.pack(); dep.setVisible(true); dep.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); System.out.println("Deposit works"); } if (evt.getActionCommand().equals("w")) { //code for withdrawing System.out.println("Withdraw works"); } if (evt.getActionCommand().equals("b")) { //code for displaying balance System.out.println("Total amount of money is: $" + balance); } } } public class test { public static void main (String[] args) { SavingsGUI frm = new SavingsGUI("Savings Account"); frm.setSize(300, 100); frm.setVisible(true); } }
Here is a rough sketch of what I want it to look like. Colors do not matter very much.
Savings.jpg
Thank you for your help.