简体   繁体   English

如何使用GridLayout将JPanel数组添加到JPanel?

[英]How do I add a JPanel array to a JPanel with GridLayout?

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). 我制作了一个6 x 7的JPanel对象数组,该对象将保留空白或全(红色或蓝色)空间的图像,这些图像用作制作板的网格,并在播放器上从空切换为特定颜色选择该列(我对Java的了解还不是很好,所以我决定不移动对象)。 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 . gridPanel ,具有6 gridPanel 7 GridLayout ,并且我有一个包含Images的面板数组。 I want to add the 6 by 7 array, to the panel with the 6 by 7 gridLayout , can this be done? 我想将6 x 7数组添加到具有6 x 7 gridLayout的面板中,可以这样做吗?

I'm also having trouble constructing the panel array, am I doing this properly (in method: createGrid )? 我在构造面板数组时也遇到麻烦,我是否正确地执行了此操作(在方法中: createGrid )?

Problem: No Images are appearing in the panel with the GridLayout . 问题:带有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 . 您尚未向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. 向一个父容器添加一个组件不能超过一次,因此必须为添加到网格的每个JPanel创建一个Jlabel新实例。

Also you do need to learn about the uses of static . 另外,您需要了解static的用法。 You are unnecessarily using it. 您不必要地使用它。 You can just create everything in the constructor then call new ConnectFour() in the main . 您可以在构造函数中创建所有内容,然后在main调用new ConnectFour() Then you wont have to make all the methods static 然后,您不必将所有方法设为static

First of all: If this is a homework, stop using static! 首先:如果这是一项家庭作业,请停止使用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. 第二:您的clearBoard方法错误。 You need to set a new emptyLabel for each Panel. 您需要为每个面板设置一个新的emptyLabel。 So call the constructor of a JPanel and pass the EmptyCircle ImageIcon. 因此,调用JPanel的构造函数并传递EmptyCircle ImageIcon。 Use this object on the gridComponent's add method. 在gridComponent的add方法上使用此对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM