Java FlowLayout, BorderLayout, GridLayout, GridBagLayout, cardlayout layout Manager

Source: Internet
Author: User
Tags pack stub

Ching

Original address: http://blog.csdn.net/qingdujun/article/details/40985027


One, FlowLayout layout manager

FlowLayout ()
Constructs a new flowlayout, which is center-aligned, with the default horizontal and vertical gaps being 5 units.

FlowLayout (int align)
Constructs a new flowlayout that has the specified alignment, and the default horizontal and vertical clearance is 5 units.

FlowLayout (int align, int hgap, int vgap)
Creates a new flow layout manager that has the specified alignment as well as the specified horizontal and vertical clearances.

Package Flowlayout.dolphin.awt;import Java.awt.button;import Java.awt.flowlayout;import Java.awt.Frame;public class flowlayouttest {public static void main (string[] args) {//TODO auto-generated method stub frame f = new Frame ("flowlayouttest"); f.setlayout (New FlowLayout (flowlayout.left,20,5)); for (int i = 0; i <; ++i) {F.add ("button" +i);} Set the window to the optimal size f.pack (); f.setvisible (True);}}


Second, BorderLayout layout manager

This is a layout of the container's border, which arranges the container components and resizes them to conform to the following five areas: North, South, east, west, middle. Each zone can contain at most one component and is identified by the corresponding constants: North, South, EAST, WEST, CENTER.

Package Borderlayout.dolphin.awt;import Java.awt.borderlayout;import Java.awt.button;import java.awt.Frame;public Class Borderlayouttest {public static void main (string[] args) {//TODO auto-generated method stub frame f = new Frame ("flowlayouttest"); F.s Etlayout (New BorderLayout (30,5)), F.add (New button ("South"), Borderlayout.south), F.add (New button ("North"), Borderlayout.north) F.add (New button ("Medium")), F.add (New button ("East"), Borderlayout.east), F.add (New button ("West"), Borderlayout.west);//Set window for optimal size f.pack (); f.setvisible (True);}}

Package Borderlayout.dolphin.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) {//TODO from Generated method stub 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);p. Add (New button ("click"));//Added to the Middle area f.add (p) By default, F.add (New button ("East"), Borderlayout.east);//Set window for optimal size f.pack (); f.setvisible (True);}}


Third, GridLayout layout manager

GridLayout ();
GridLayout (int rows,int cols);
GridLayout (int rows,int cols,int hgap,int vgap);
Creates a layout object for a table. Rows represents a few rows, cols represents a few columns, Hgap is the horizontal distance between components, and Vgap is the vertical distance between components.

Package Gridlayout.dolphin.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) {//TODO auto-generated method stub frame f = new Frame ("gridlayouttest"); panel p1 = new Panel ();p 1.add (new TextField (30));//default is BorderLayout layout F.add (P1, Borderlayout.north); panel P2 = new Panel ();p 2.setLayout (New GridLayout (3,5,4,4)); String[] name = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "."}; for (int i = 0; i < name.length; ++i) {P2.add (new Button (Name[i]));} Added by default in the Middle F.add (p2);//Set window for optimal size f.pack (); f.setvisible (True);}}


Iv. GridBagLayout Layout Manager

To use GridBagLayout, you must use the Gridbagconstraints object to specify the location of the components in the gridbaglayout.

The Setconstraints method of the GridBagLayout class uses component and gridbagconstraints as parameters to set the component constraint.

Construction Method:

GridBagLayout (): Creates a Grid package layout manager.

Common methods:

void Setconstraints (Component comp, gridbagconstraints constraints): Sets the constraints for the specified component in this layout.

Java.awt.GridBagConstraints: Used to specify constraints on components that are laid out using the GridBagLayout class.

Construction Method:

Gridbagconstraints (): Creates a Gridbagconstraint object that sets all of its fields to their default values.

gridbagconstraints (int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int a Nchor, int fill, Insets Insets, int ipadx, int ipady): Creates a Gridbagconstraints object and sets all its fields to the incoming parameter.

GRIDX: Specifies the cell that contains the beginning edge of the display area of the component. [The default value is RELATIVE. The GRIDX should be non-negative. ]

The first cell of a row is gridx=0. The start edge of the component display area refers to the horizontal, left-to-right container's left edge, and the right edge of the horizontal, right-to-left container. The value RELATIVE specifies that the component is placed

The component that was just added to the container before the component was added.

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

The topmost cell is gridy=0. The value RELATIVE specifies that the component is placed below the component that was just added to the container before the component was added.

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

Use remainder to specify the display area of the component, which is the range from GridX to the last cell in the row. Use RELATIVE to specify the display area of the component, ranging from GridX to the

The second-to-last cell.

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

Use remainder to specify the display area of the component, which is the range from GridY to the last cell in the column. Use RELATIVE to specify the display area of the component, which ranges from GridY to the column in which it is located.

The second-to-last cell.

WEIGHTX: Specifies how additional horizontal space is distributed. [The default value for this field is 0.] The WEIGHTX should be non-negative. ]

The grid Package Layout manager calculates that the weight of the column will be the largest weightx of all the components in the columns. If the resulting layout is smaller in the horizontal direction than the area that needs to be filled, the system distributes the extra space to its weighted proportions to each

A column. Columns with a weight of zero do not get extra space.

If all weights are zero, then all additional space will appear between the grid and the left and right edges of the cell.

Weighty: Specifies how additional vertical space is distributed. [The default value for this field is 0.] The weighty should be non-negative. ]

The grid Package Layout manager calculates the weight of the trip to be the largest weighty in all the components of the row. If the resulting layout is smaller in the vertical direction than the area that needs to be filled, the system distributes the extra space to its weighted proportions to each

Yes. A row with a weight of zero does not get extra space.

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

Anchor: Use this field when the component is smaller than its display area. It determines where to place the component in the display area. [Default value is CENTER. ]

There are three possible values: a value relative to the direction, a value relative to the baseline, and an absolute value. Values relative to the direction are interpreted relative to the container's component orientation property, and the value is interpreted relative to the baseline, and the absolute value is not

Then Absolute values are: CENTER, North, northeast, EAST, Southeast, south, SOUTHWEST, WEST, and northwest. Direction relative values are: Page_start,

Page_end, Line_start, Line_end, First_line_start, First_line_end, Last_line_start and Last_line_end. The values relative to the baseline are:

BASELINE, Baseline_leading, baseline_trailing, Above_baseline, above_baseline_leading, above_baseline_trailing,

Below_baseline, Below_baseline_leading and below_baseline_trailing.

Fill: Use this field when the display area of the component is larger than the size of the display area that it is requesting. It determines whether the component is resized and how it is adjusted when needed. [The default value is NONE.] ]

The following values apply to fill:

None: The component size is not resized.

Horizontal: Widens the component so that it fills its display area horizontally, but does not change the height.

VERTICAL: The heightening component, which fills its display area vertically, but does not change the width.

BOTH: Causes the component to fully fill its display area.

Insets: This field specifies the external padding of the component, which is the minimum amount of space between the component and the edge of its display area. [The default value is new Insets (0, 0, 0, 0). ]

Insets four parameters in order to represent the space between the top, left, bottom and right four directions, respectively.

IPADX: This field specifies the internal padding of the component, that is, how much space to add to the minimum width of the component. The width of the component is at least the minimum width plus ipadx pixels. [The default value is 0.] ]

Ipady: This field specifies the inner padding, which is how much space is added to the minimum height of the component. The height of the component is at least the minimum height plus ipady pixels. [The default value is 0.] ]

Package Gridbaglayout.dolphin.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) {//TODO auto-generated method stub fr Ame 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, vertically 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 GUI component of the Gridbagconstraints control will become the last component in the landscape gbc.gridwidth = gridbagconstraints.remainder;gb.setconstraints (bs[3], GBC) ; F.add (bs[3]);//The Gridbagconstraints-controlled GUI component will not expand in landscape GBC.WEIGHTX = 0;gb.setconstraints (bs[4], GBC); F.add (bs[4]);// The gridbagconstraints-controlled GUI component will span two meshes gbc.gridwidth = 2;gb.setconstraints (bs[5], GBC); F.add (bs[5]);//The GRIThe GUI component of the Dbagconstraints control will span a grid gbc.gridwidth = 1;//The gridbagconstraints-controlled GUI component will be vertically spanned by two meshes gbc.gridheight = 2;// The GUI component of the Gridbagconstraints control will become the last component in the landscape gbc.gridwidth = gridbagconstraints.remainder;gb.setconstraints (bs[6], GBC) ; F.add (bs[6]);//The Gridbagconstraints-controlled GUI component will cross a grid horizontally, vertically across two meshes gbc.gridwidth = 1;gbc.gridheight = 2;// The gridbagconstraints controls the vertical enlargement weights of GUI components are 1gbc.weighty = 1;gb.setconstraints (bs[7], GBC); F.add (bs[7]);// Setting the button below in portrait will not enlarge gbc.weighty = 0;//The Gridbagconstraints control GUI component will become the last component in the landscape Gbc.gridwidth = gridbagconstraints.remainder;//the Gridbagconstraints-controlled GUI component will be vertically spanned by 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);}}


V. CardLayout layout Manager

The CardLayout Layout Manager manages its components in time rather than space, and it sees all components of the container as a stack of cards, each time as long as the topmost component is visible. It's like a deck of cards, they stack together, and each time only the topmost card is visible.

CardLayout provides the following two constructors:

1) cardlayout (): Create the default CardLayout layout manager.

2) cardlayout (int hgap,int vgap): Creates a Vgap layout manager by specifying the spacing between the cards and the left and right boundaries of the container (HGAP), the upper and lower bounds (cardlayout).

CardLayout 5 common methods for controlling the visibility of components:

1) First (Container target): Displays the initial card in the target container.

2) Last (Container target): Displays the final card in the target container.

3) Previous (Container target): Displays the previous card in the target container.

4) Next (Container target): Displays the lower card in the target container.

5) Show (Container target,string name): Displays the card with the name specified in the target container.

Package Cardlayout.dolphin.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 ;p Ublic class Cardlayouttest {public static void main (string[] args) {//TODO auto-generated method stub frame f = new Frame ("Cardlayouttest "); String[] names = {"First", "second", "third", "fourth", "fifth"};final cardlayout C = new CardLayout (); final panel p1 = new Panel ();p 1.set Layout (c); for (int i = 0; i < names.length; ++i) {P1.add (names[i], new Button (Names[i]));} panel P2 = new Panel ();//control displays the previous button Btn_pre = New button ("previous"); Btn_pre.addactionlistener (new ActionListener () { public void actionperformed (ActionEvent e) {c.previous (p1);}}); /control Displays the Next button Btn_next = New button ("Next"); Btn_next.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {c.next (p1);}}); /Control Display First button Btn_first = New button ("First"); Btn_first.addactionlistener (new ActionListener () {public VOID actionperformed (ActionEvent e) {C.first (p1);}}); /control Display Last button Btn_last = New button ("last"); Btn_last.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {c.last (p1);}}); /control Display Last button btn_3rd = New button ("third"); Btn_3rd.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {c.show (P1, "Third");}); P2.add (Btn_pre);p 2.add (btn_next);p 2.add (Btn_first);p 2.add (btn_last);p 2.add (btn_3rd); F.add (p1); F.add (P2, Borderlayout.south); F.pack (); f.setvisible (True);}}


Original address: http://blog.csdn.net/qingdujun/article/details/40985027

References: 1) Crazy Java Handout (2nd edition) Li Gang authoring

2) Baidu Encyclopedia, gridbagconstraint,http://baike.baidu.com/view/1966330.htm,2014 November 10 20:53:27

3) Baidu Encyclopedia, flowlayout,http://baike.baidu.com/view/6762091.htm,2014 November 10 20:53:55

4) Baidu Encyclopedia, borderlayout,http://baike.baidu.com/view/7097482.htm,2014 November 10 20:54:17

5) Baidu Encyclopedia, gridlayout,http://baike.baidu.com/view/10481980.htm,2014 November 10 20:54:41

6) Little-dot NetEase blog, layout manager, Http://blog.163.com/[email protected]/blog/static/68847725201261851041680/,2014 November 10 20:55:33


Java FlowLayout, BorderLayout, GridLayout, GridBagLayout, cardlayout layout Manager

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.