JPanel fatPanel = new JPanel(); JPanel calPanel = new JPanel(); JPanel perPanel = new JPanel(); public percentFatPanel() { fatPanel.add( fatLabel ); fatPanel.add( inFat ); calPanel.add( calLabel ); calPanel.add( inCal ); perPanel.add( perLabel ); perPanel.add( outPer );
BoxLayout
Layout Manager
Now the three panels are added to the content pane.
We want the label, the panels, and the button to be
lined up in a vertical column.
FlowLayout
would do this if the frame
is not too wide.
But we want it to always be the case.
For this, use BoxLayout
.
BoxLayout
puts components in either a vertical column or a
horizontal row.
Say which arrangement you want in its constructor:
BoxLayout(Container target, int axis) target: the container this layout manager is for axis: BoxLayout.X_AXIS for left to right BoxLayout.Y_AXIS for top to bottom
The target
field is a reference to the container
(normally the conent pane) which the new layout manager
is for.