4

我一直在为课程制作 Conway 的 GOL 副本,但在 GUI 呈现时遇到了问题。

快速概要:GUI 创建一个 Frame 和一个 mainPanel,设置为 BorderLayout。

一旦我实例化了 Grid 本身并将其分配给 mainPanel,它应该在 Grid 中显示我的 2D 数组,但它没有。在过去的 2 个小时里,我一直把头撞在墙上。

FWIW,我不能在此基础上使用 IDE 进行 GUI 构建。下面的代码:

图形用户界面

           import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.*;
import java.util.Observer;
import java.util.Observable;

public class GameOfLifeGUI extends JFrame implements Observer {

    private JPanel mainPanel;
    private JPanel gridPanel;
    private JPanel startPanel;
    private JPanel titlePanel;
    private JButton start;
    private Cell cell;
    private Grid grid;
    private MouseEvent mouseClicked;
    private MouseEvent mouseDragged;
    private MouseEvent mousePressed;
    private MouseEvent mouseRelease;
    private MouseListener mouseListener;

    public GameOfLifeGUI() {
        super("");
        //Create Start Button for startPanel
        JButton start = new JButton("Start");

        //Creates a Grid to add to the panel
        grid = new Grid(75,75);

        //Create JPanels
        mainPanel = new JPanel();
        gridPanel = new JPanel();
        startPanel = new JPanel();
        titlePanel = new JPanel();

        /**
         * Add Grid to gridPanel
         * Modify Grid(int, int) to change size of Grid. Per spec, this grid should always be 75x75
         */

        //Create gridPanel
        gridPanel.setLayout(new GridLayout(75,75));
        gridPanel.setBackground(Color.WHITE);
        gridPanel.add(grid);

        //Set Layout of Panels
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(gridPanel, BorderLayout.CENTER);
        mainPanel.add(startPanel, BorderLayout.SOUTH);
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        //Add Start Button to startPanel
        startPanel.add(start);

        //Creates a window for displaying the GUI
        this.setTitle("Conway's Game of Life");
        this.setSize(1000, 750);
        this.setLocationRelativeTo(null);
        this.add(mainPanel);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }//end Constructor

网格

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Color;
import javax.swing.JPanel.*;
import java.util.Observer;
import java.util.Arrays;
import java.util.Observable;

public class Grid extends JPanel{

  private Cell[][] grid;
  private int column;
  private int row;

  /**
   * Constructs a Grid of Cells
   * columns is a column of cells
   * rows is a row of cells
   */

  public Grid(int column, int row){
    this.column = column;
    this.row = row;

    // create a grid of cells
    grid = new Cell[row][column];
    for (int r = 0; r < row; r++){
      for (int c = 0; c < column; c++){
        grid[r][c] = new Cell(r,c);
      }
    }
    //Creates a border of cells around grid for edge case handling
    //All cells in this border will be dead and incapable of living
    for (int c = 0; c < column; c++){
        grid[0][c] = new Cell(row, column);
    }
    for (int c = 0; c < column-1; c++){
        grid[row-1][c] = new Cell(row, column);
    }
    for (int r = 0; r < row; r++){
        grid[r][0] = new Cell(row, column);
    }
    for (int r = 0; r < row-1; r++){
        grid[r][column - 1] = new Cell(row, column);
    }
  }//end Constructor

如果您需要更多信息,请告诉我 - 不想在我的第一篇文章中转储代码。

4

1 回答 1

2

Grid 类没有绘制组件的方法。使用 java 图形类中的 drawRect() 方法的简单嵌套 for 循环解决了该问题。

于 2012-04-30T19:47:39.243 回答