Java FlowLayout, BorderLayout, GridLayout, GridBagLayout, and CardLayout layout manager

Source: Internet
Author: User

Java FlowLayout, BorderLayout, GridLayout, GridBagLayout, and CardLayout layout manager

 

 

1. FlowLayout layout manager

FlowLayout ()
Construct a new FlowLayout, which is centered and aligned. The default horizontal and vertical gaps are 5 units.

FlowLayout (int align)
Construct a new FlowLayout with the specified alignment. The default horizontal and vertical gaps are 5 units.

FlowLayout (int align, int hgap, int vgap)
Create a new flow layout manager with the specified alignment and the specified horizontal and vertical gaps.

 

Package flowlayout. doldolphin. awt; import java. awt. button; import java. awt. flowLayout; import java. awt. frame; public class FlowLayoutTest {public static void main (String [] args) {// method stub automatically generated by TODO Frame f = new Frame (FlowLayoutTest); f. setLayout (new FlowLayout (FlowLayout. LEFT, 20, 5); for (int I = 0; I <10; ++ I) {f. add (new Button (Button + I);} // set the window to the optimum size. pack (); f. setVisible (true );}}

 

2. BorderLayout layout manager

This is a border layout for container layout. It can arrange container components and adjust their size to meet the following five areas: north, south, east, west, and middle. Each region can contain up to one component and is identified by corresponding constants: NORTH, SOUTH, EAST, WEST, and CENTER.

 

Package borderlayout. doldolphin. awt; import java. awt. borderLayout; import java. awt. button; import java. awt. frame; public class BorderLayoutTest {public static void main (String [] args) {// method stub automatically generated by TODO Frame f = new Frame (FlowLayoutTest); f. setLayout (new BorderLayout (30, 5); f. add (new Button (south), BorderLayout. SOUTH); f. add (new Button (north), BorderLayout. NORTH); f. add (new Button (in); f. add (new Button (East), BorderLayout. EAST); f. add (new Button (West), BorderLayout. WEST); // set the window to the optimum size. pack (); f. setVisible (true );}}

 

Package borderlayout. doldolphin. awt; import java. awt. borderLayout; import java. awt. button; import java. awt. frame; import java. awt. panel; import java. awt. textField; public class BorderLayoutTest {public static void main (String [] args) {// method stub automatically generated by TODO Frame f = new Frame (BorderLayoutTest); f. setLayout (new BorderLayout (30, 5); f. add (new Button (south), BorderLayout. SOUTH); f. add (new Button (north), BorderLayout. NORTH); Panel p = new Panel (); p. add (new TextField (20); p. add (new Button (click); // It is added to the intermediate area f by default. add (p); f. add (new Button (East), BorderLayout. EAST); // set the window to the optimum size. pack (); f. setVisible (true );}}


 

3. GridLayout layout manager

GridLayout ();
GridLayout (int rows, int cols );
GridLayout (int rows, int cols, int hgap, int vgap );
Create a layout object for a table. Rows indicates several rows, cols indicates several columns, hgap indicates the horizontal distance between components, and vgap indicates the vertical distance between components.

 

Package gridlayout. doldolphin. awt; import java. awt. borderLayout; import java. awt. button; import java. awt. frame; import java. awt. gridLayout; import java. awt. panel; import java. awt. textField; public class GridLayoutTest {public static void main (String [] args) {// method stub automatically generated by TODO Frame f = new Frame (GridLayoutTest ); panel p1 = new Panel (); p1.add (new TextField (30); // The default layout is BorderLayout f. add (p1, BorderLayout. NORTH); Panel p2 = new Panel (); p2.setLayout (new GridLayout (,); String [] name = {, +, -,*,/,.}; for (int I = 0; I <name. length; ++ I) {p2.add (new Button (name [I]);} // It is added in the center by default. add (p2); // set the window to the optimum size. pack (); f. setVisible (true );}}


Iv. GridBagLayout layout manager

To use GridBagLayout, you must use the GridBagConstraints object to specify the position of Components in GridBagLayout.

The setConstraints method of the GridBagLayout class uses Component and GridBagConstraints as parameters to set Component constraints.

Constructor:

GridBagLayout (): Create a grid package layout manager.

Common Methods:

Void setConstraints (Component comp, GridBagConstraints constraints): sets constraints for the specified Component in this layout.

Java. awt. GridBagConstraints: constraints used to specify components that use the GridBagLayout class.

Constructor:

GridBagConstraints (): Create a GridBagConstraint object and set all its fields to the default value.

GridBagConstraints (int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx, int ipady ): create a GridBagConstraints object and set all its fields as input parameters.

Gridx: Specify the cell that contains the start edge of the display area of the component. [The default value is RELATIVE. Gridx should be a non-negative value.]

The first cell of the row is gridx = 0. The START edge of the component display area refers to the left edge of the horizontal, left-right container, and the right edge of the horizontal, right-to-left container. Value RELATIVE specifies to place the component

After the component is added to the container.

Gridy: Specifies the cell at the top of the display area of the component. [The default value is RELATIVE. Gridy should be non-negative.]

The cell at the top is gridy = 0. Value RELATIVE specifies that the component is placed under the component just added to the container before the component is added.

Gridwidth: specify the number of cells in a row of the component display area. [Gridwidth should be non-negative. The default value is 1. ]

Use REMAINDER to specify the display area of the component, which ranges from gridx to the last cell of the row. Use RELATIVE to specify the display area of the component. The range of the display area is from gridx to its row.

The second to the last cell.

Gridheight: specify the number of cells in the column of the component display area. [Gridheight should be non-negative. The default value is 1. ]

Use REMAINDER to specify the display area of the component. The range of the display area is from gridy to the last cell in the column. Use RELATIVE to specify the display area of the component. The range of this area is from gridy to its column.

The second to the last cell.

Weightx: Specifies how to distribute extra horizontal space. [The default value of this field is 0. Weightx should be a non-negative value. ]

The grid package layout manager calculates the maximum weightx among all components of the column. If the layout is smaller than the area to be filled in the horizontal direction, the system distributes the additional space to each

One column. Columns with zero weight will not get extra space.

If all weights are zero, all extra space will appear between the grid of cells and between the left and right edges.

Weighty: Specifies how to distribute extra vertical space. [The default value of this field is 0. Weighty should be non-negative.]

The grid package layout manager calculates the travel weight as the largest weighty among all the components of the row. If the layout is smaller than the area to be filled vertically, the system distributes the additional space to each area according to the weight ratio.

Line. Rows with zero weight will not get extra space.

If all weights are zero, all extra space will appear between the grid of cells and between the upper and lower edges.

Anchor: this field is used when the widget is smaller than the display area. It determines the position of the component in the display area. [The default value is CENTER.]

There are three possible values: the value relative to the direction, the value relative to the baseline, and the absolute value. The value relative to the direction is interpreted relative to the component direction attribute of the container. The value relative to the baseline value is interpreted relative to the baseline, and the absolute value is not

However. Absolute values include CENTER, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, and NORTHWEST. Direction relative values: PAGE_START,

PAGE_END, LINE_START, LINE_END, FIRST_LINE_START, FIRST_LINE_END, LAST_LINE_START, and LAST_LINE_END. The baseline values are as follows:

BASELINE, BASELINE_LEADING, BASELINE_TRAILING, abve_baseline, abve_baseline_leading, abve_baseline_trailing,

BELOW_BASELINE, BELOW_BASELINE_LEADING, and BELOW_BASELINE_TRAILING.

Fill: this field is used when the display area of the component is greater than the size of the display area requested by the component. It determines whether to adjust the component size and how to adjust it as needed. [The default value is NONE.]

The following values apply to fill:

NONE: Do not adjust the component size.

HORIZONTAL: a widening component that fills the display area horizontally without changing the height.

VERTICAL: add the component to fill the display area vertically without changing the width.

BOTH: Fill the display area of the component completely.

Insets: this field specifies the external fill of the component, that is, the minimum gap between the component and its display area edge. [The default value is new Insets (0, 0, 0, 0).]

The four parameters of insets represent the gap between the top, left, bottom, and right directions.

Ipadx: this field specifies the internal filling of the component, that is, how much space is added to the minimum width of the component. The width of a widget must at least add ipadx pixels to its minimum width. [The default value is 0. ]

Ipady: this field specifies the size of the space to add to the minimum height of the component. The height of the component must be at least the minimum height plus the ipady pixel. [The default value is 0. ]

 

[It is necessary to understand the specific meaning of each attribute in GridBagConstraints so that we can better customize the layout.

@ Gridx, gridy:

Position of the component in the upper left corner. For example, if the panel on the left is in the column 0 in the row 1, gridy = 0 and gridx = 1. The row corresponds to gridy, and the column corresponds to gridx.

@ Gridwidth, gridheight

The number of rows and columns occupied by the component. For example, if the top panel occupies one row and two columns, gridwidth = 2, gridheight = 1

@ Weightx, weighty

It can be simply understood as the increment value of the component size change. For example, if weightx = 100 is set, the component size changes with the cell. When weightx is set to 0, the component size does not change. Of course, weightx and weighty can also be set to other values, but it is of little significance and will not be described in detail.

@ Fill

Filling mode of components in the grid (Allocation Area)

For example, if fill = HORIZONTAL is used, the component fills up the full cells horizontally. If fill = BOTH is used, the entire grid is filled.

@ Anchor

When a component is located in the grid, anchor = EAST indicates the right alignment.

@ Ipadx, ipady

Internal filling refers to adding ipadx to the x direction and ipady to the y direction based on the preferred size of the component. This ensures that the component will not contract to ipadx, the size determined by ipady is below the size, so we can use the value of ipadx and ipady to specify the size of the component, instead of the size of the component, otherwise it will have an unexpected effect.

@ Insets

The filling area is the part between the component and the grid border. Four parameters are left, top, right, and bottom. However, when the fill of the component is set to NONE, the specified insects value is meaningless. This section is taken from the ITeye blog, http://yyzjava.iteye.com/blog/1181552]

 

Package gridbaglayout. doldolphin. awt; import java. awt. button; import java. awt. frame; import java. awt. gridBagConstraints; import java. awt. gridBagLayout; public class GridBagLayoutTest {public static void main (String [] args) {// method automatically generated by TODO Frame f = new Frame (GridBagLayoutTest ); gridBagLayout gb = new GridBagLayout (); GridBagConstraints gbc = new GridBagConstraints (); Button [] bs = new Button [10]; f. setLayout (gb); for (int I = 0; I <bs. length; ++ I) {bs [I] = new Button (Button + I);} // All components can be expanded horizontally and vertically in gbc. fill = GridBagConstraints. BOTH; gbc. weightx = 1; gb. setConstraints (bs [0], gbc); f. add (bs [0]); gb. setConstraints (bs [1], gbc); f. add (bs [1]); gb. setConstraints (bs [2], gbc); f. add (bs [2]); // The GridBagConstraints-controlled GUI component will become the last horizontal component gbc. gridwidth = GridBagConstraints. REMAINDER; gb. setConstraints (bs [3], gbc); f. add (bs [3]); // The GUI components controlled by GridBagConstraints will not be expanded horizontally by gbc. weightx = 0; gb. setConstraints (bs [4], gbc); f. add (bs [4]); // The GridBagConstraints-controlled GUI component will span two grids (gbc. gridwidth = 2; gb. setConstraints (bs [5], gbc); f. add (bs [5]); // The GridBagConstraints-controlled GUI component will span across a grid gbc. gridwidth = 1; // The GUI component controlled by GridBagConstraints vertically spans two grids (gbc. gridheight = 2; // The GUI component controlled by GridBagConstraints will become the last horizontal component gbc. gridwidth = GridBagConstraints. REMAINDER; gb. setConstraints (bs [6], gbc); f. add (bs [6]); // The GridBagConstraints-controlled GUI component will span one grid horizontally and vertically across two grids gbc. gridwidth = 1; gbc. gridheight = 2; // The vertical weight of the GUI component controlled by GridBagConstraints is 1gbc. weighty = 1; gb. setConstraints (bs [7], gbc); f. add (bs [7]); // set the following buttons to not expand gbc. weighty = 0; // The GUI component controlled by GridBagConstraints will become the last horizontal component gbc. gridwidth = GridBagConstraints. REMAINDER; // The GUI components controlled by GridBagConstraints vertically cross a grid gbc. gridheight = 1; gb. setConstraints (bs [8], gbc); f. add (bs [8]); gb. setConstraints (bs [9], gbc); f. add (bs [9]); f. pack (); f. setVisible (true );}}

 

Package gridbaglayout. doldolphin. awt; import java. awt. *; public class CGridBagLayout {public static void main (String [] args) {Frame f = new Frame (GridBagLayoutTest); GridBagLayout gb = new GridBagLayout (); gridBagConstraints gbc = new GridBagConstraints (); Button [] btn = new Button [10]; f. setLayout (gb); for (int I = 0; I <btn. length; ++ I) {btn [I] = new Button (BUTTON + I);} // All components can be expanded horizontally and vertically in gbc. fill = GridBagConstraints. BOTH; // The left-aligned gbc. anchor = GridBagConstraints. WEST; // the vertical and horizontal weighting of the GridBagConstraints GUI component is 1 gbc. weighty = 1; gbc. weightx = 1; // The GUI components controlled by GridBagConstraints will be horizontally across one grid and vertically across two grids gbc. gridwidth = 1; gbc. gridheight = 2; gb. setConstraints (btn [0], gbc); f. add (btn [0]); // The GridBagConstraints-controlled GUI component will span two grids horizontally and vertically across one grid gbc. gridwidth = 2; gbc. gridheight = 1; gb. setConstraints (btn [1], gbc); f. add (btn [1]); // The GridBagConstraints-controlled GUI component will span one grid horizontally and vertically across two grids gbc. gridwidth = 1; gbc. gridheight = 2; // The GUI component controlled by GridBagConstraints will become the last horizontal component gbc. gridwidth = GridBagConstraints. REMAINDER; gb. setConstraints (btn [2], gbc); f. add (btn [2]); gbc. gridx = 1; gbc. gridy = 1; // the second row, the second column // The GUI component controlled by GridBagConstraints will be horizontally across two grids and vertically across one grid gbc. gridwidth = 2; gbc. gridheight = 1; gb. setConstraints (btn [3], gbc); f. add (btn [3]); gbc. gridx = 0; gbc. gridy = 2; // the third row, the first column // The GUI component controlled by GridBagConstraints will span one grid horizontally and vertically across two grids gbc. gridwidth = 1; gbc. gridheight = 2; gb. setConstraints (btn [4], gbc); f. add (btn [4]); gbc. gridx = 1; gbc. gridy = 2; // the third row, the second column // The GUI component controlled by GridBagConstraints vertically and horizontally across a grid gbc. gridwidth = 1; gbc. gridheight = 1; gb. setConstraints (btn [5], gbc); f. add (btn [5]); gbc. gridx = 2; gbc. gridy = 2; // the third row, the third column gb. setConstraints (btn [6], gbc); f. add (btn [6]); gbc. gridx = 3; gbc. gridy = 2; // the third row, column 4 // The GUI components controlled by GridBagConstraints will span one grid horizontally and vertically across two grids gbc. gridwidth = 1; gbc. gridheight = 2; // The GUI component controlled by GridBagConstraints will become the last horizontal component gbc. gridwidth = GridBagConstraints. REMAINDER; gb. setConstraints (btn [7], gbc); f. add (btn [7]); gbc. gridx = 1; gbc. gridy = 3; // Row 4, column 2 // The GUI component controlled by GridBagConstraints vertically spans two grids gbc. gridwidth = 1; gbc. gridheight = 1; gb. setConstraints (btn [8], gbc); f. add (btn [8]); gbc. gridx = 2; gbc. gridy = 3; // the third row, the third column gb. setConstraints (btn [9], gbc); f. add (btn [9]); f. pack (); f. setVisible (true );}}

 

5. CardLayout layout manager

The CardLayout layout manager manages the components in the container by time rather than space. It views all the components added to the container into a stacked card, and each time only the Component at the top is visible. It's like a pair of playing cards. They are stacked together and each time only the top playing card is visible.

CardLayout provides the following two constructors:

1) CardLayout (): Creates the default CardLayout layout manager.

2) CardLayout (int hgap, int vgap): Creates a CardLayout layout manager by specifying the gap between the left and right sides of the card and the container (hgap) and between the upper and lower boundaries (vgap.

CardLayout:

1) first (Container target): displays the first card in the target Container.

2) last (Container target): displays the last card in the target Container.

3) previous (Container target): displays the previous card in the target Container.

4) next (Container target): displays the next card in the target Container.

5) show (Container target, String name): displays the card with the specified name in the target Container.

 

Package cardlayout. doldolphin. awt; import java. awt. borderLayout; import java. awt. button; import java. awt. cardLayout; import java. awt. frame; import java. awt. panel; import java. awt. event. actionEvent; import java. awt. event. actionListener; public class CardLayoutTest {public static void main (String [] args) {// method stub automatically generated by TODO Frame f = new Frame (CardLayoutTest ); string [] names = {first, second, third, fourth, fifth}; final CardLayout c = new CardLayout (); final Panel p1 = new Panel (); p1.setLayout (c); for (int I = 0; I <names. length; ++ I) {p1.add (names [I], new Button (names [I]);} Panel p2 = new Panel (); // control the display of the previous Button btn_Pre = new Button (previous); btn_Pre.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {c. previous (p1) ;}}); // The control displays the next Button: Button btn_Next = new Button (next); btn_Next.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {c. next (p1) ;}}); // control the display of the first Button. Button btn_First = new Button (first); btn_First.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {c. first (p1) ;}}); // control the display of the last Button. Button btn_Last = new Button (last); btn_Last.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {c. last (p1) ;}}); // control the display of the last Button. Button btn_3rd = new Button (third); btn_3rd.addActionListener (new ActionListener () {public void action=med (ActionEvent e) {c. show (p1, third) ;}}); p2.add (btn_Pre); p2.add (btn_Next); p2.add (btn_First); p2.add (btn_Last); p2.add (btn_3rd); f. add (p1); f. add (p2, BorderLayout. SOUTH); f. pack (); f. setVisible (true );}}


 

Address: http://blog.csdn.net/qingdujun/article/details/40985027

References: 1) edited by Li Gang, crazy Java handout (2nd)

2) Baidu encyclopedia, GridBagConstraint, http://baike.baidu.com/view/1966330.htm, November 10, 2014

3) Baidu encyclopedia, FlowLayout, http://baike.baidu.com/view/6762091.htm, November 10, 2014

4) Baidu encyclopedia, BorderLayout, http://baike.baidu.com/view/7097482.htm, November 10, 2014

5) Baidu encyclopedia, GridLayout, http://baike.baidu.com/view/10481980.htm, November 10, 2014

6) Netease blog, layout manager, November 10, 2014

 

Related Article

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.