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 add the top label, the three panels, and the button to the content pane.
We want then to
line up in a vertical column.
FlowLayout
would do this if the frame
is not too wide.
But we want it to be certain.
For this, use BoxLayout
.
BoxLayout
aligns components either horizontally or vertically.
You pick the arrangement in the constructor:
BoxLayout(Container box, int axis) box: the container this layout manager is for axis: BoxLayout.X_AXIS for left to right BoxLayout.Y_AXIS for top to bottom
The box
field is a reference to the container
(often the content pane of a JFrame
) which the new layout manager
is for.