CI'm试图使用cell[][]
为使用cell
JButton
,boolean
条件和坐标的网格编写Conway的生命游戏计划。我有一个方法可以创建cell
个对象的新N x M网格。我可以很容易地把它变大,但是把它做得更小就是一个问题:旧的按钮保持原样。只是归零cell[][]
不起作用。我应该用什么来处理旧按钮?
Upd8:一些代码
网格创建:
if(tfSizeX.getText().length() > 0 && tfSizeY.getText().length() > 0 && tfCellSize.getText().length() > 0)
{
grid = null; // Cell[][] grid;
int sizeGridX = Integer.parseInt(tfSizeX.getText());
int sizeGridY = Integer.parseInt(tfSizeY.getText());
int sizeCell = Integer.parseInt(tfCellSize.getText());
grid = new Cell[sizeGridX][sizeGridY];
for(int i = 0; i < sizeGridX; i++)
for(int j = 0; j < sizeGridY; j++)
grid[i][j] = new cell(i*(sizeCell+1), j*(sizeCell+1), panel, sizeCell);
panel.repaint();
}
cell.java
public class Cell
{
private JButton cell;
private boolean condition;
int locX, locY, size = 24;
public Cell(int locX, int locY, JPanel panel, int size)
{
this.size = size;
condition = false;
cell = new JButton("");
cell.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if(!condition)
{
cell.setBackground(Color.black);
}
else
{
cell.setBackground(Color.white);
}
condition = !condition;
}
});
cell.setBackground(Color.GRAY);
cell.setBounds(locX, locY, size, size);
panel.add(cell);
cell.setBackground(Color.white);
}
public void change(boolean condition)
{
this.condition = condition;
if(condition)
{
cell.setBackground(Color.black);
}
else
{
cell.setBackground(Color.white);
}
}
}