我一直在研究康威GOL課程的副本,並且在GUI呈現時遇到問題。Conway的生命GUI遊戲
Quick rundown: GUI創建一個Frame和一個mainPanel,設置爲BorderLayout。
一旦我實例化網格本身並將其分配給mainPanel,它應該在網格中顯示我的二維數組,但它不會。在過去的2個小時裏,我的頭撞在牆上。
FWIW,我無法使用IDE在此基礎上構建GUI。下面的代碼:
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
如果您需要更多的信息,請讓我知道 - 不希望在我的第一篇代碼轉儲。
繪製網格的代碼在哪裏?這只是佈局。在子組件上調用'setVisible',並且在將它附加到父項之前這樣做是沒有意義的。 –
網格存在於它自己的類中 - 你想看看Grid類還是GUI構建的其餘部分? – hedrick
作爲模型類的網格並不重要,但它是可視化的代碼。粘貼可視代碼的相關部分。 –