Search code examples
javaswinguser-interfacelayout-managergrid-layout

Numberpad Not Displaying Correctly


I have a Java GUI project where I am creating an ATM. I have everything setup, but for some reason my number pad on the left is not displaying properly. It should appear as a 4x3 grid of numbers, but it is just displaying a 9. I have checked to make sure it is in a GridLayout and I have checked my loop, but I possibly may have looked over something. Any help is appreciated, thanks!

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;

public class ATMProject extends JPanel implements ActionListener {
    private JPanel mainPanel = null;
    private JPanel btnPanel = null;
    private JPanel userBtns = null;
    private JTextArea textArea = null;
    private JPanel keyPanel = null;
    private JTextField numField = null;
    private JPanel numpadPanel = null;
    private JButton[] userButtons = null;
    private JButton[] keypadButtons = null;
    private String[] btnPanelbtns = { "A", "B", "C" };
    private String[] numpadPanelbtns = { "7", "4", "1", "8", "5", "2", "9", "6", "3", "0", ".", "CE" };

    public ATMProject() {
        super();
        mainPanel = new JPanel();
        this.setLayout(new BorderLayout());
        this.add(mainPanel);

        btnPanel = new JPanel();
        btnPanel.setLayout(new GridLayout(3, 1));
        this.add(btnPanel, BorderLayout.EAST);

        textArea = new JTextArea();
        this.add(textArea, BorderLayout.CENTER);

        keyPanel = new JPanel();
        keyPanel.setLayout(new BorderLayout());
        this.add(keyPanel, BorderLayout.WEST);

        numpadPanel = new JPanel();
        numpadPanel.setLayout(new GridLayout(0, 3));
        keyPanel.add(numpadPanel, BorderLayout.CENTER);

        numField = new JTextField();
        keyPanel.add(numField, BorderLayout.NORTH);

        userButtons = new JButton[btnPanelbtns.length];
        for (int i = 0; i < userButtons.length; i++) {
            userButtons[i] = new JButton(btnPanelbtns[i]);
            userButtons[i].addActionListener(this);
            btnPanel.add(userButtons[i]);
        }

        keypadButtons = new JButton[numpadPanelbtns.length];
        for (int i = 0; i < userButtons.length; i++) {
            keypadButtons[i] = new JButton(numpadPanelbtns[i]);
            keypadButtons[i].addActionListener(this);
            numpadPanel.add(keypadButtons[i]);
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args) {
        MyFrame mf = new MyFrame();
    }

}


import javax.swing.JFrame;

public class MyFrame extends JFrame {
    private ATMProject atm = null;
    public MyFrame(){
        super();
        atm = new ATMProject();
        this.add(atm);
        this.setTitle("ATM");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setSize(800,300);
        this.setVisible(true);
    }
}

This is how it is supposed to appear:

enter image description here


Solution

  • My loop was incorrectly written. for (int i = 0; i < userButtons.length; i++) my program was only outputting 3 buttons because userButtons.length only had 3 elements. I changed userButtons.length to numpadPanelbtns.length and it fixed it because there are 12 elements in the numpadPanelbtns array.