For the panel, you must use the appropriate Layoutmanager, for example GridLayout, Boxlayout or GridBagLayout.
It depends on what else you want to place in the panel.
GridLayout is easier to use IMO:
JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); // any number of rows, 1 column ... panel.add(button[i]);
BoxLayout is almost as easy:
JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); ... panel.add(button[i]);
GridBagLayout is more powerful, allowing more than one column, components spanning more than one cell ... require GridBagConstraints to add elements:
JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints( 0, RELATIVE, // x = 0, y = below previous element 1, 1, // cell width = 1, cell height = 1 0.0, 0.0 // how to distribute space: weightx = 0.0, weighty = 0,0 GridBagConstraints.CENTER, // anchor GridBagConstraints.BOTH, // fill new Insets(0, 0, 0, 0), // cell insets 0, 0); // internal padding ... panel.add(button[i], constraints);
Take a look at this tutorial: Aligning components inside a container (visual guide is a good starting point)
EDIT :
You can also lay out components manually, that is, specify the location and size of each component in the container. To do this, you must set the LayoutManager to null to remove the default manager.
JPanel panel = new JPanel(); panel.setLayout(null); ... button[i].setLocation(x, y); button[i].setSize(width, heigth);