Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将JPanel数组添加到具有GridLayout的JPanel?_Java_Arrays_Swing_Jpanel_Embedded Resource - Fatal编程技术网

Java 如何将JPanel数组添加到具有GridLayout的JPanel?

Java 如何将JPanel数组添加到具有GridLayout的JPanel?,java,arrays,swing,jpanel,embedded-resource,Java,Arrays,Swing,Jpanel,Embedded Resource,我正在做一个连接四的游戏。我制作了一个6乘7的JPanel对象数组,它可以保存空的或满的(红色或蓝色)空间的图像,这些图像作为一个网格来制作棋盘,并在玩家选择该列时从空切换到特定的颜色(我还不太擅长Java,我决定不制作移动对象)。我在用空格填充网格时遇到问题 我不知道怎么做,我有一个小组gridPanel,具有6×7的GridLayout,我有一个包含图像的面板阵列。我想将6×7数组添加到具有6×7gridLayout的面板中,可以这样做吗 我在构建面板阵列时也遇到了问题,我是否正确地(在方法

我正在做一个连接四的游戏。我制作了一个6乘7的
JPanel
对象数组,它可以保存空的或满的(红色或蓝色)空间的图像,这些图像作为一个网格来制作棋盘,并在玩家选择该列时从空切换到特定的颜色(我还不太擅长Java,我决定不制作移动对象)。我在用空格填充网格时遇到问题

我不知道怎么做,我有一个小组
gridPanel
,具有6×7的
GridLayout
,我有一个包含
图像的面板阵列。我想将6×7数组添加到具有6×7
gridLayout
的面板中,可以这样做吗

我在构建面板阵列时也遇到了问题,我是否正确地(在方法:
createGrid
)进行了此操作

问题:没有图像显示在带有
网格布局的面板中

我的代码如下:

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;
      }
    } 
  }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共第四类{
静态JButton冒号=新JButton(“Drop”);
静态JButton colTwo=新JButton(“Drop”);
静态JButton colThree=新JButton(“Drop”);
静态JButton colFour=新JButton(“Drop”);
静态JButton colFive=新JButton(“Drop”);
静态JButton colSix=新JButton(“Drop”);
静态JButton colSeven=新JButton(“下降”);
静态JPanel[][]gridComponent=newjpanel[6][7];
静态JPanel gridPanel=newjpanel();
静态JPanel emptySlot=新JPanel();
静态JPanel redSlot=newjpanel();
静态JPanel blueSlot=newjpanel();
公共静态void main(字符串[]args){
JPanel mainPanel=新的JPanel();
JPanel buttonPanel=新的JPanel();
//创建3个可能的插槽映像
ImageIcon emptyCircle=新的ImageIcon(“emptyCircle.png”);
ImageIcon redCircle=新的ImageIcon(“redCircle.png”);
ImageIcon blueCircle=新的ImageIcon(“blueCircle.png”);
JLabel emptyLabel=新的JLabel(emptyCircle);
JLabel redLabel=新的JLabel(红色圆圈);
JLabel blueLabel=新的JLabel(蓝色圆圈);
emptySlot.add(emptyLabel);
redSlot.add(redLabel);
blueSlot.add(blueLabel);
setLayout(新的BorderLayout());
setLayout(新的GridLayout(6,7));
buttonPanel.setLayout(新的GridLayout(1,7));
添加(gridPanel、BorderLayout.CENTER);
主面板。添加(按钮面板,边框布局。北);
按钮面板。添加(冒号);
按钮面板添加(第二列);
按钮面板添加(第三列);
按钮面板添加(colFour);
按钮面板添加(第五列);
按钮面板添加(第六列);
按钮面板添加(第七列);
//JFrame的属性
JFrame窗口=新JFrame(“连接四个”);//标题
window.setContentPane(mainPanel);//内容窗格设置为mainPanel
window.setSize(500500);//JFrame size
setLocation(0,0);//外观的位置
window.setVisible(true);//设置为可见
window.setresizeable(true);//设置为可调整大小
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//程序在退出窗口时结束
createGrid();
透明板();
}
公共静态void createGrid(){

对于(int a=0;a首先:如果这是一个家庭作业,请停止使用static!如果我要更正它,我会将其标记为错误。相反,请使用如下主方法实例化电路板:

public static void main (String[] args){
  ConnectFour connectFour = new ConnectFour();
}

第二:你的clearBoard方法是错误的。你需要为每个面板设置一个新的emptyLabel。因此,请调用JPanel的构造函数并传递EmptyCircle ImageIcon。在gridComponent的add方法上使用此对象。

首先:如果这是一个家庭作业,请停止使用static!如果我要更正它,我会将其标记为错误。相反,请使用ins用如下主要方法对电路板进行测试:

public static void main (String[] args){
  ConnectFour connectFour = new ConnectFour();
}

第二:您的clearBoard方法错误。您需要为每个面板设置一个新的emptyLabel。因此,请调用JPanel的构造函数并传递EmptyCircle ImageIcon。在gridComponent的add方法上使用此对象。

您尚未向
GridComponents
添加任何标签。您需要直接向每个面板添加标签和图标

 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]);
  }
}

for(int a=0;a您尚未向
GridComponents
添加任何标签。您需要直接向每个标签添加标签和图标

 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]);
  }
}

for(int a=0;不确定是否重复,但几分钟前刚刚发布。似乎是另一个人做了相同的作业;)至少这段代码解决了另一个线程的问题。@OP:您当前发布的问题是什么?您不能期望我们扫描您的代码以查找可能的问题。您是否有错误的行为?或错误?@Carsten Hoffmann:没有图像显示在带有GridLayout的JPanel中您似乎有两个明显不同的问题代码:1)如何将面板阵列添加到网格布局。2)加载图像。--现在有两个人回答了有关网格布局的问题。--因此,每篇文章一个问题最有效,因此请将“加载图像”问题移至一个新问题。但最好发布其中一个问题。它应该只涉及一个图像和(如果图片是按钮)一个按钮。不确定是否重复,但几分钟前刚刚发布。似乎是另一个人做了相同的作业;)至少这段代码解决了另一个线程的问题。@OP:您当前发布的问题是什么?您不能期望我们扫描您的代码以查找可能的问题。您是否有错误的行为?或错误?@Carsten Hoffmann:没有图像显示在带有GridLayout的JPanel中您似乎有两个明显不同的问题代码:1)如何将面板阵列添加到网格布局。2)加载图像。--现在有两个人回答了有关网格布局的问题。--因此,每篇文章一个问题最有效,因此请将“加载图像”问题移至一个新问题。但最好发布一个这样的问题。它