Java Swing layout Management boxlayout layout

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.