• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to apply "GridBagLayout"?

 
Gao Zhigang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make a 4*3's grid.
This is my program:

import java.awt.*;

public class JustTest
{
public static void main(String[] args)
{
Frame f = new Frame("just a test");
f.setLayout(new GridBagLayout());
Button b[] = new Button[12];
int att[][]={{0,0,1,1,0,0},{1,0,1,1,0,0},{2,0,1,1,0,0},{0,1,1,1,0,0},{1,1,1,1,0,0},{2,1,1,1,0,0},{0,2,1,1,0,0},{1,2,1,1,0,0},{2,2,1,1,0,0},{0,3,1,1,0,0},{1,3,1,1,0,0},{2,3,1,1,0,0}};

for(int i=0;i<b.length;i++)
{
b[i] = new Button("Button " + (i+1));
add(f,b[i],att[i]);
}
f.pack();
f.setVisible(true);
}
private static void add(Container con,Component com,int att[])
{
GridBagConstraints cons = new GridBagConstraints();
cons.gridx = att[0];
cons.gridy = att[1];
cons.gridwidth = att[2];
cons.gridheight = att[3];
cons.weightx = att[4];
cons.weighty = att[5];
con.add(com,cons);
}
}

But when i compiled it,the rusult is not my mine.last 3 Buttons do not same the front!
How can i do for it and then to make all the Buttons same.
 
Gao Zhigang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bay the way,the cons.weightx[4];and cons.weighty[5] add them or not the rusult are the same(centerly it must del the last 2 number of att)
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Gao Zhigang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,bay the way,why do i make it at C(my system) and apply, the rusult is nothing,and then i take it at D,and then apply,it is ok.
 
Gao Zhigang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My System is windows2000 and build it at C Tray.
I take the 2th in my c tray,and then apply,but the result is only a freme and not any Buttons.Then i take it to the D tray and apply it,then i saw the result and it's right.Why can't i apply it at C?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!