Thanks guys
The methods worked good. I have
the JFrame set to a certain width which gives the
right most JButtons a nice space from the edge of
the frame. But i have to work on the line
" if(x > (jf.getWidth() - (width + space))){"
a bit so i can have consistent space from the
edge of the frame regardless of the JFrame size.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Layout{
public static void main(String [] args){
int height = 50;
int width = 50;
int space = 10;
int x = 0;
int y = 10;
JFrame jf = new JFrame("Hello");
Container c = jf.getContentPane();
c.setLayout(null);
jf.addWindowListener(new WindowMonitor());
jf.setSize(560,400);
jf.setResizable(false);
jf.setVisible(true);
JButton[] jb = new JButton[54];
for(int i = 0; i < jb.length; ++i){
jb[i] = new JButton(new Integer(i).toString());
jb[i].setSize(width, height);
}
for(int i = 0; i < jb.length; ++i){
if(x == 0){
x = 10;
jb[i].setBounds(new Rectangle(x, y, width, height));
c.add(jb[i]);
continue;
}
x += (width + space);
jb[i].setBounds(new Rectangle(x, y, width, height));
c.add(jb[i]);
if(x > (jf.getWidth() - (width + space))){
x = 10;
y += (height + space);
jb[i].setBounds(new Rectangle(x, y, width, height));
c.add(jb[i]);
}
}
}
}
class WindowMonitor extends WindowAdapter {
public void windowClosing(WindowEvent we){
System.exit(0);
}
}