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

时间:2014-01-12 02:32:45

标签: java arrays swing jpanel embedded-resource

我正在制作Connect Four游戏。我制作了一个6乘7的JPanel对象阵列,它们可以保存空或全(红色或蓝色)空间的图像,图像充当网格以制作电路板,并将从空切换到特定选择该列的玩家的颜色(我在Java方面还不太好,我决定不制作移动物体)。我在用空格填充网格时遇到问题。

我对如何做到这一点感到困惑,我有一个小组; gridPanel,6乘7 GridLayout,我有一组包含Images的面板。我想将6乘7阵列添加到6乘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;
      }
    } 
  }
}

2 个答案:

答案 0 :(得分:3)

您尚未向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]);
  }
}

您不能多次向任何父容器添加组件,因此您必须为添加到网格中的每个Jlabel创建JPanel的新实例。

此外, 还需要了解static的用法。你不必要地使用它。您可以在构造函数中创建所有内容,然后在new ConnectFour()中调用main。然后你不必制作所有方法static

答案 1 :(得分:2)

首先:如果这是作业,请停止使用静电!如果我纠正它,我会将此标记为错误。相反,用这样的主方法实例化电路板:

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

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