我正在制作一款四子棋游戏。我制作了一个 6 x 7 的 JPanel
对象数组,这些对象将保存空或满(红色或蓝色)空间的图像,这些图像充当网格来制作板,并将从空切换玩家选择该列时会显示为特定颜色(我对 Java 还不太了解,我决定不制作移动对象)。我在用空白空间填充网格时遇到问题。
我对如何执行此操作感到困惑,我有一个面板; gridPanel
,带有 6 x 7 GridLayout
,并且我有一个包含 Images
的面板数组。我想将 6 x 7 数组添加到具有 6 x 7 gridLayout 的面板中,可以这样做吗?
我在构建面板数组时也遇到了麻烦,我是否正确执行此操作(在方法:createGrid
中)?
问题:GridLayout
面板中未显示任何图像。
我的代码如下:
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;
}
}
}
}
最佳答案
您尚未向 gridCompoents
添加任何标签。您需要直接向每个标签和图标添加标签和图标
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]);
}
}
您不能向任何父容器多次添加组件,因此您必须为添加到父容器的每个 JPanel
创建一个 Jlabel
的新实例。网格。
此外,您确实需要了解static
的用法。你不必要地使用它。您只需在构造函数中创建所有内容,然后在 main
中调用 new ConnectFour()
即可。那么你就不必将所有方法设为静态
关于java - 如何使用 GridLayout 将 JPanel 数组添加到 JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21070616/