I'm making a Connect Four Game. I made a 6 by 7 array of JPanel
objects that will hold images of either empty, or full (red or blue) spaces, the images act as a grid as to make the board and will switch from empty to a specific color upon a player choosing that column (I'm not too good in Java yet, I decided not to make moving objects). I'm having a problem filling the grid with empty spaces.
I'm confused on how to do this, I have a panel; gridPanel
, with a 6 by 7 GridLayout
, and I have an array of panels that contain the Images
. I want to add the 6 by 7 array, to the panel with the 6 by 7 gridLayout
, can this be done?
I'm also having trouble constructing the panel array, am I doing this properly (in method: createGrid
)?
Problem: No Images are appearing in the panel with the GridLayout
.
My code goes as follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ConnectFour{
static JButton colOne = new JButton("Drop");
static JButton colTwo = new JButton("Drop");
static JButton colThree = new JButton("Drop");
static JButton colFour = new JButton("Drop");
static JButton colFive = new JButton("Drop");
static JButton colSix = new JButton("Drop");
static JButton colSeven = new JButton("Drop");
static JPanel[][] gridComponent = new JPanel[6][7];
static JPanel gridPanel = new JPanel();
static JPanel emptySlot = new JPanel();
static JPanel redSlot = new JPanel();
static JPanel blueSlot = new JPanel();
public static void main(String[] args){
JPanel mainPanel = new JPanel();
JPanel buttonPanel = new JPanel();
//Creation of the 3 possible slot images
ImageIcon emptyCircle = new ImageIcon("emptyCircle.png");
ImageIcon redCircle = new ImageIcon("redCircle.png");
ImageIcon blueCircle = new ImageIcon("blueCircle.png");
JLabel emptyLabel = new JLabel(emptyCircle);
JLabel redLabel = new JLabel(redCircle);
JLabel blueLabel = new JLabel(blueCircle);
emptySlot.add(emptyLabel);
redSlot.add(redLabel);
blueSlot.add(blueLabel);
mainPanel.setLayout(new BorderLayout());
gridPanel.setLayout(new GridLayout(6, 7));
buttonPanel.setLayout(new GridLayout(1, 7));
mainPanel.add(gridPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.NORTH);
buttonPanel.add(colOne);
buttonPanel.add(colTwo);
buttonPanel.add(colThree);
buttonPanel.add(colFour);
buttonPanel.add(colFive);
buttonPanel.add(colSix);
buttonPanel.add(colSeven);
//Properties of the JFrame
JFrame window = new JFrame("Connect Four"); //Title
window.setContentPane(mainPanel); //content pane set to mainPanel
window.setSize(500,500); //JFrame size
window.setLocation(0,0); //Location of appearance
window.setVisible(true); //Set to be visable
window.setResizable(true); //Set to be resizeable
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Program ends upon exiting window
createGrid();
clearBoard();
}
public static void createGrid(){
for(int a=0; a<6; a++){
for(int b=0; b<7; b++){
gridComponent[a][b] = new JPanel();
gridPanel.add(gridComponent[a][b]);
}
}
}
public static void clearBoard(){
for(int a=0; a<6; a++){
for(int b=0; b<7; b++){
gridComponent[a][b] = emptySlot;
}
}
}
}
You haven't added any labels to you gridCompoents
. You'll want to add a label and an icon directly to each one
for(int a=0; a<6; a++){
for(int b=0; b<7; b++){
gridComponent[a][b] = new JPanel();
gripComponent.add(new JLabel(emptyCirle)); <----
gridPanel.add(gridComponent[a][b]);
}
}
You can't add a component more than once to any parent container, so you would have to create new instances of a Jlabel
for each JPanel
you add to the grid.
Also you do need to learn about the uses of static
. You are unnecessarily using it. You can just create everything in the constructor then call new ConnectFour()
in the main
. Then you wont have to make all the methods static
First of all: If this is a homework, stop using static! I would mark this as wrong for sure if I would correct it. Instead, instantiate the board in a main method like this:
public static void main (String[] args){
ConnectFour connectFour = new ConnectFour();
}
Second: Your clearBoard method is wrong. You need to set a new emptyLabel for each Panel. So call the constructor of a JPanel and pass the EmptyCircle ImageIcon. Use this object on the gridComponent's add method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.