BoxLayout: You can specify whether the control is placed horizontally or vertically in the container, which is more flexible than the FlowLayout
BoxLayout is slightly different from other layout managers, you must pass a reference to the container instance to its constructor, and the container uses BoxLayout. You must also specify how the components in the boxlayout are laid out: vertically (by column) or horizontally (by row). Nesting multiple panels with different combinations of horizontal and vertical components is similar to gridbaglayout, but less complex.
1. Constructors
BoxLayout(Container target, int axis): Creates a layout manager that will place components along a given axis.
LINE_AXIS: Specifies that the component should be placed based on the ComponentOrientation direction of the text line determined by the properties of the target container.
PAGE_AXIS: Specifies that the component should be placed according to the ComponentOrientation flow of text lines in the page determined by the properties of the target container.
X_AXIS: Specifies that components should be placed from left to right.
Y_AXIS: Specifies that the component should be placed from top to bottom.
2. Common methods
getAxis(): Returns the axis used for the layout component.
getLayoutAlignmentX(Container target): Returns the alignment of the container along the X-axis.
getLayoutAlignmentY(Container target): Returns the alignment of the container along the Y axis
getTarget(): Returns a container that uses this layout manager.
3. Example
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >import java.awt.container;import javax.swing.boxlayout;import Javax.swing.jbutton;import javax.swing.JFrame; Import Javax.swing.uimanager;public class Boxlayoutdemo {public static void main (string[] args) {try { Uimanager.setlookandfeel (Uimanager.getsystemlookandfeelclassname ());} catch (Exception e) {}jframe frame = new JFrame ("BoxLayout Test"); Frame.setdefaultcloseoperation (Jframe.exit_on_close) ; Container panel = Frame.getcontentpane ();p anel.setlayout (new BoxLayout (panel, Boxlayout.y_axis)); for (Float align = 0.0f; Align <= 1.0f; Align + = 0.25f) {JButton button = new JButton ("X align =" + align); Button.setalignmentx (align);p anel.add (button);} Frame.setsize (+); frame.setvisible (True);}} </span>
4. Results
Java Swing layout Management boxlayout layout