Java制作Connect Four游戏面板

时间:2014-01-12 02:24:46

标签: java swing layout-manager grid-layout

我正在为计算机科学课程制作Connect Four作为我的CPT。到目前为止,我已经创建了面板,但我遇到了问题。我想要做的是在电路板上每个插槽制作一个面板,并用连接四板的空槽图片填充,当每个点组合时,它看起来像一个完整的连接四板。基本上我是在我的主面板上添加一个网格布局面板,并用包含插槽图片的多个其他面板填充网格面板。我已经创建了一个子程序来做到这一点。但是当我运行我的程序时,中间只有一个插槽,而不是应该显示的42个(电路板是7乘6)。我现在的目标是为我的子程序创建42个JPanel并将它们放入我创建的网格面板中。我知道这可能没有多大意义,但希望代码可以帮助您了解更多。谢谢你的帮助。\

P.S。 emptyBox.jpg基本上是连接四板上空插槽的图片。我想用这些填充面板,所以它看起来像一个完整的板。

到目前为止,这是代码:

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;

      }
    }
  }
}

2 个答案:

答案 0 :(得分:3)

我认为你想和一个新的JPanel而不是仅仅将它变成空面板。发生的事情是你试图多次向父包含添加一个组件,这是行不通的。组件只能添加一次。所以你获得的结果是唯一添加了emptyPanel

的gridPanel插槽
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);

对于重复性任务,您还可以创建一个简单的帮助方法

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());

      }
    }
}

答案 1 :(得分:1)

您需要实际创建42个面板。目前,您有一个包含42个引用同一面板的数组。更改fillGrid方法,因此它调用JPanel的构造函数并设置必要的Label等(就像你上面做的那样)。然后将其添加到GridLayout。