BoxLayout

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

FlowLayout adds components in raster order: left to right, top to bottom.

BoxLayout

Sometimes you want components to be lined up in a strictly vertical (or strictly horizontal) order. This can be done with a BoxLayout layout manager. Here is a constructor for it:

BoxLayout(Container contain, int axis) 

    contain: the container this layout manager is for
            
    axis: BoxLayout.X_AXIS  — for left to right alignment
          BoxLayout.Y_AXIS  — for top to bottom alignment

Notice that the constructor needs a reference to the container that it is manageing. To set the layout manager of a JPanel to BoxLayout with vertical alignment, do this:

JPanel panel = new JPanel();

panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS) );

Here is another example GUI. Look at it and decide where a vertical layout has been used

[vertical alignment used in some panels]

QUESTION 12:

Where has BoxLayout with vertical alignment been used?