Java制作Connect Four游戏面板 [英] Java making Connect Four game panel
问题描述
我将计算机科学课的Connect Four作为我的CPT.到目前为止,我已经创建了面板,但是遇到了问题.我要做的是在板上的每个插槽上制作一个面板,并用连接四块板上的空插槽的图片填充它,当每个点组合在一起时,看起来就像是一块完整的连接四块板上.基本上,我要在主面板中添加一个网格布局面板,并用包含插槽图片的其他多个面板填充该网格面板.我已经创建了一个子例程来执行此操作.但是,当我运行程序时,中间只会出现一个插槽,而不是应该显示的42个插槽(面板7乘6).我现在的目标是为我的子例程创建42个JPanel,并将它们放入我创建的网格面板中.我知道这可能没有多大意义,但希望代码能帮助您了解更多.感谢您的帮助.\
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基本上是一张连接四板上的空插槽的图片.我想用这些填充面板,以便看起来像一个完整的面板.
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;
}
}
}
}
推荐答案
我认为您想使用一个新的JPanel,而不是仅将其设置为空白面板.发生的情况是您试图将一个组件多次添加到父容器中,这是行不通的.一个组件只能一次添加.因此,您得到的结果是唯一添加了emptyPanel
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]);
}
}
}
您还将想要移动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;
}
然后在您的循环中
public static void fillGrid() {
for(int j = 0; j < 6; j++) {
for (int k = 0; k < 7; k++) {
gridPanel.add(createOnePanel());
}
}
}
这篇关于Java制作Connect Four游戏面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!