问题
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;
}
}
}
}
回答1:
I think you want to and a new JPanel instead of just making it = to the empty panel. What's happening is that you are trying to add one component to a parent containter multiple times, which won't work. A component can only be added once. So you the result you're getting, is the only the gridPanel slot being added an emptyPanel
public static void fillGrid() {
for(int j = 0; j < 6; j++) {
for (int k = 0; k < 7; k++) {
gridArray[j][k] = new JPanel();
gridArray[j][k].add(new Label(emptybox));
gridPanel.add(gridArray[j][k]);
}
}
}
You'll also want to move this gridPanel.add(emptyPanel);
What you can also do, for repetitive tasks is create a simple helper method
private JPanel greateOnePanel(){
JPanel panel = new JPanel();
ImageIcon icon = new IMageIcon("emptybox.jpg"):
JLabel label = new JLabale(icon);
panel.add(label);
return panel;
}
Then in your loop just
public static void fillGrid() {
for(int j = 0; j < 6; j++) {
for (int k = 0; k < 7; k++) {
gridPanel.add(createOnePanel());
}
}
}
回答2:
You need to actually create 42 panels. Currently you have an array with 42 references to the same panel. Change the fillGrid method, so it calls the constructor of JPanel and set the necessary Label etc. (like you do above). Then add it to the GridLayout.
来源:https://stackoverflow.com/questions/21070555/java-making-connect-four-game-panel