I am trying to build a matching game with icons attached to each button. Although, this isn't close to being finished, I have a problem with filling the panel with buttons.
With this code I get a grey colored frame. If i comment out the 3 methods i use under "//execution" the panel will be all black (which is how i am testing to see if the buttons are filling the space or not.)
For some reaso,n my buttons aren't being populated onto the panel.
I need some help!!! Where am I going wrong?
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MemoryMainFrame extends JFrame implements ActionListener {
    JButton button[] = new JButton[16];
    private static final int SIXTEEN_BUTTONS = 16;
    JPanel mainPanel = new JPanel();
    double dim = Math.sqrt(SIXTEEN_BUTTONS);
    int numOfColumns = (int) (dim);
    int numOfRows = (int) (dim);
    public static void main(String[] args) {
        new MemoryMainFrame();
    }
    public MemoryMainFrame() {
        this.setTitle("MemoryGame!!!");
        this.setSize(400, 400);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        mainPanel.setBackground(Color.BLACK);
        mainPanel.setLayout(new GridBagLayout());
        // execution
        FillButtonArray(SIXTEEN_BUTTONS);
        AddButtonListener(SIXTEEN_BUTTONS);
        ButtonToPanel(SIXTEEN_BUTTONS);
        this.add(mainPanel);
    }
    public void FillButtonArray(int numOfButtons) {
        int i = 0;
        while (i < numOfButtons) {
            button[i] = new JButton("asdf");
        }
    }
    public void AddButtonListener(int numOfButtons) {
        int i = 0;
        while (i < numOfButtons) {
            button[i].addActionListener(this);
        }
    }
    public void ButtonToPanel(int numOfButtons) {
        int n = 0;
        GridBagConstraints gbc = new GridBagConstraints();
        for (int i = 0; i < numOfColumns; i++) {
            for (int j = 0; j < numOfRows; j++) {
                gbc.gridx = i;
                gbc.gridy = j;
                n++;
                button[n].setBorder(BorderFactory.createLineBorder(
                        Color.DARK_GRAY, 2));
                mainPanel.add(button[n]);
            }
        }
    }
    public void actionPerformed(ActionEvent arg0) {
        JFrame j = new JFrame();
        j.setSize(300, 300);
        j.setVisible(true);
    }
}
I use the "asdf" as a test to see if the buttons work as well.
also, the actionperformed was a test as well. That part of the code is irrelevant.
 
     
    