Problem with contraints of a container in a JFrame
Thank you everyone who helped me yesterday. I got a grid up an running. Today, I was furnishing the print function, which would change the image of a JLabel, and now my JContraints don't work, the error is somewhere in something called a "Hastable", which goes to setting the contraints in the GridBagLayout. Here's the code for you all
public class Life implements Runnable{
//I Create a JFrame, and a JLabel to stick on it
GridBagLayout lay=new GridBagLayout();
JFrame JF=new JFrame("Conway's Game of Life");
JPanel window=new JPanel(lay);
int[][]matrix=new int[20][20];
int turn=0;
//I make a matrix of JLabels, so I can have 400 easy labels to access
JLabel[][] Squares=new JLabel[20][20];
//An image of a Red, White, and Blue Square
private ImageIcon RedS=new ImageIcon("RedSquare.gif");
private ImageIcon BlueS=new ImageIcon("BlueSquare.gif");
private ImageIcon WhiteS=new ImageIcon("WhiteSquare.gif");
//Titles and buttons on the GUI
private JLabel TURNS,X,Y,REPLACE;
private JButton red,white,blue,take1turn,takeNturns;
private IntegerField xfile,yfile,nfile;
private JTextArea ERRORBOX;
public Life(){
TURNS=new JLabel("Turn # "+turn);
X=new JLabel("X:");
xfile=new IntegerField(0);
REPLACE=new JLabel("CHANGE SQUARE");
Y=new JLabel("Y:");
MakeObject(window,TURNS,lay,0,10,1,1,0,0);
//Builds the matrix of JLabels
for(int x=0;x<Squares.length;x++){
for(int y=0;y<Squares.length;y++){
Squares[x][y]=new JLabel(WhiteS);
MakeObject(window,Squares[x][y],lay,x+1,y,1,1,0,0);
}
}
MakeObject(window,REPLACE,lay,21,2,1,1,0,0);
MakeObject(window,X,lay,22,0,1,1,0,0);
MakeObject(window,xfile,lay,22,1,1,1,0,0);
MakeObject(window,Y,lay,22,2,1,1,0,0);
//Error occurs right around here
MakeObject(window,yfile,lay,22,4,1,1,0,0);
yfile=new IntegerField(0);
//Packs it all into a content pane
JF.getContentPane().add(window);
JF.pack();
JF.setVisible(true);
JF.setSize(1000,1000);
}
public static JComponent ResetComponents(JComponent o){
GridBagConstraints c=new GridBagConstraints();
c.fill=GridBagConstraints.BOTH;
c.gridx=0;
c.gridy=0;
c.gridheight=0;
c.gridwidth=0;
c.weightx=0;
c.weighty=0;
return o;
}
public static void MakeObject(Container pane,JComponent o,GridBagLayout lay,int x,int y,int w,int h, double wx,double wy){
GridBagConstraints c=new GridBagConstraints();
c.fill=GridBagConstraints.BOTH;
c.gridx=x;
c.gridy=y;
c.gridheight=h;
c.gridwidth=w;
//Error also occurs around here, in the weights
c.weightx=wx;
c.weighty=wy;
lay.setConstraints(o,c);
pane.add(o);
o=ResetComponents(o);
}
//Printing brings in the matrix, the labels, and the 3 color squares
public void PrintMatrix(int[][]matrix,JLabel[][]JLabels,ImageIcon White,ImageIcon Red,ImageIcon Blue){
for(int x=0;x<matrix.length;x++){
for(int y=0;y<matrix.length;y++){
if(matrix[x][y]==0)
JLabels[x][y].setIcon(White);
if(matrix[x][y]==1)
JLabels[x][y].setIcon(Red);
if(matrix[x][y]==2)
JLabels[x][y].setIcon(Blue);
MakeObject(JF,window,lay,x+1,y,1,1,1,1);
}
}
}
If any of you could help me, I would be really grateful.>

