I'm making Connect Four for computer science class as my CPT. SO far I've created the panels but I am having an issue. What I am trying to do is make one panel per slot on the board and fill it with a picture of an empty slot of a connect four board, and when every spot is combined, it'll look like a complete connect four board. Basically I'm adding a grid layout panel to my main panel and filling the grid panel with multiple other panels containing the the slot picture. I've created a sub-routine to do so. But when I run my program, only one slot comes up in the middle, not the 42 that are supposed to show (the board is 7 by 6). My goal right now is to for my sub-routine to create 42 JPanels and put them into the grid panel I have created. I know this may not make a lot of sense but hopefully the code will help you understand more. Thanks for your help.\
P.S. emptyBox.jpg is basically a picture of an empty slot on a connect four board. I want to fill the panel up with these so it looks like a complete board.
Here's the code so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class ConnectFour {
static JFrame mainWindow;
static JButton firstArrow = new JButton("Drop");
static JButton secondArrow = new JButton("Drop");
static JButton thirdArrow = new JButton("Drop");
static JButton fourthArrow = new JButton("Drop");
static JButton fifthArrow = new JButton("Drop");
static JButton sixthArrow = new JButton("Drop");
static JButton seventhArrow = new JButton("Drop");
static JPanel[][] gridArray = new JPanel[6][7];
static JLabel emptyLabel = new JLabel();
static JPanel emptyPanel;
static ImageIcon emptyBox;
static JLabel redLabel = new JLabel();
static JPanel redPanel;
static ImageIcon redBox;
static JLabel blackLabel = new JLabel();
static JPanel blackPanel;
static ImageIcon blackBox;
public static void main(String[] args) {
JPanel mainPanel = new JPanel();
JPanel gridPanel = new JPanel();
JPanel buttonPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
gridPanel.setLayout(new GridLayout(6, 7));
buttonPanel.setLayout(new GridLayout(1, 7));
mainPanel.setBackground(new Color(23, 13, 44));
emptyBox = new ImageIcon("emptyBox.jpg");
emptyLabel = new JLabel(emptyBox);
emptyPanel = new JPanel();
emptyPanel.add(emptyLabel);
mainPanel.add(gridPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.NORTH);
gridPanel.add(emptyPanel);
buttonPanel.add(firstArrow);
buttonPanel.add(secondArrow);
buttonPanel.add(thirdArrow);
buttonPanel.add(fourthArrow);
buttonPanel.add(fifthArrow);
buttonPanel.add(sixthArrow);
buttonPanel.add(seventhArrow);
mainWindow = new JFrame("Connect Four");
mainWindow.setContentPane(mainPanel);
mainWindow.setSize(846, 730);
mainWindow.setLocationRelativeTo(null);
mainWindow.setVisible(true);
mainWindow.setResizable(false);
fillGrid();
}
public static void fillGrid() {
for(int j = 0; j < 6; j++) {
for (int k = 0; k < 7; k++) {
gridArray[j][k] = emptyPanel;
}
}
}
}