Java Swing Quest (i) LayoutManager

Source: Internet
Author: User
Tags cbtn

    1. BorderLayout

    2. FlowLayout

    3. GridLayout

    4. GridBagLayout

    5. CardLayout

    6. BoxLayout

1.BorderLayout

Java.lang.Object

--java.awt.borderlayout

Divide the layout into five regions, east, west, south, north, and middle, and place the components of the add-in at the specified location.

    • Borderlayout.east
    • Borderlayout.west
    • Borderlayout.south
    • Borderlayout.north
    • Borderlayout.center

constructor function:

BorderLayout ()

Create BorderLayout with no spacing between components

BorderLayout (int hgap,int vgap)

Set the horizontal spacing between components to hgap and the vertical spacing to vgap borderlayout

Example one:

Import Java.awt.borderlayout;import javax.swing.jframe;import javax.swing.jbutton;/** * * @author Chel */public Class Borderlayoutdemo {public static void main (string[] args) {//Create a jframe,jframe default layoutmanager for Borderlayo UT JFrame f=new JFrame ("BorderLayout"); JButton btn=new JButton ("Borderlayout.north"); F.add (Btn,borderlayout.north); Btn=new JButton ("Borderlayout.south"); F.add (Btn,borderlayout.south); Btn=new JButton ("Borderlayout.east"); F.add (Btn,borderlayout.east); Btn=new JButton ("Borderlayout.west"); F.add (btn,borderlayout.west); Btn=new JButton ("Borderlayout.center"); F.add (Btn,borderlayout.center); F.pack (); F.setvisible (TRUE); F.setdefaultcloseoperation (Jframe.exit_on_close); }}

Execution Result:

Insert the following code between lines 13th and 14 of the previous example code

f.setlayout (New BorderLayout (10,10));

Execution Result:

2.FlowLayout

Java.lang.Object

--java.awt.flowlayout

Components are arranged in order from left to right and then top to bottom, and one line cannot be placed until the next line is folded.

constructor Function :

FlowLayout ()

Create a flowlayout that feels centered and aligned, and the components have 5 units of horizontal and vertical spacing.

FlowLayout (int align)

Set up a flowlayout that can be arranged in a horizontal and vertical space with 5 units of each other

FlowLayout (int align,int hgap,int vgap)

Set up a flowlayout that arranges the arrangement and spacing of the components

Example two:

Import Java.awt.flowlayout;import javax.swing.jframe;import javax.swing.jbutton;/** * * @author Chel */public class Flowlayoutdemo {public static void main (string[] args) {JFrame f=new JFrame ("FlowLayout"); F.setlayout (New FlowLayout ()); for (int i=0;i<7;i++) {JButton btn=new JButton ("button" +i); F.add (BTN); } f.setsize (300,150); F.setvisible (TRUE); F.setdefaultcloseoperation (Jframe.exit_on_close); }}

Execution Result:

3.GridLayout

Java.lang.Object

--java.awt.gridlayout

Layout of the container's components by means of a rectangular grid

constructor Function :

GridLayout ()

Create a gridlayout that feels like one row

GridLayout (int rows,int cols)

Establishes a gridlayout for a specified row (rows) and column (cols)

GridLayout (int rows,int cols,int hgap,int vgap)

Establishes a specified row (rows) and column (cols) with horizontal spacing between components of hgap and vertical spacing of vgap GridLayout

Example three:

Import Java.awt.gridlayout;import javax.swing.jframe;import javax.swing.jbutton;/** * * @author Chel */public class Gridlayoutdemo {public static void main (string[] args) {JFrame f=new JFrame ("GridLayout"); Set F layout Manager to 3 rows of 3 columns of GridLayout, between the components horizontal and vertical spacing of 5 f.setlayout (new GridLayout (3,3,5,5)); for (int i=1;i<10;i++) {JButton btn=new JButton (string.valueof (i)); F.add (BTN); } f.pack (); F.setvisible (TRUE); F.setdefaultcloseoperation (Jframe.exit_on_close); }}

Execution Result:

4.GridBagLayout

Java.lang.Object

--java.awt.gridbaglayout

GridBagLayout the components within the container in a tabular form, placing each component within each cell, and a cell that can be merged across multiple cells into a single cell, where multiple cells can be combined into a single cell, allowing for free layout of the component.

constructor Function :

GridBagLayout ()

Create a default GridBagLayout

Each cell has its own properties, which are defined by the member variables of the Gridbagconstrainsts class, and all member variables in the gridbagconstriaints are public.

Java.lang.Object

--java.awt.gridbagconstratints

constructor function:

Gridbagconstraints ()

Create a default gridbagconstraints

Gridbagconstraints (intgridx,int gridy,int gridwidth,int gridheight,double weightx,double weighty,int anchor,int Fill, Insets insets,int ipadx,int Ipady)

Create a gridbagconstraints that specifies its value

Member variables for gridbagconstraints:

    • int GridX
    • int GridY
    • int Gridwidth
    • int Gridheight
    • Double WEIGHTX
    • Double weighty
    • int anchor
    • int fill
    • Insets Insets
    • int IPADX
    • int Ipady

Gridx,gridy: Sets the starting coordinates of the row and column where the component is located. For example, gridx=0,gridy=0 indicates that the component is placed in 0 rows and 0 columns of cells.

Gridwidth,gridheight: Sets the number of horizontal and vertical cell spans for the component.

Can be specified by Gridbagconstraints's reletive, and remainder, using the following methods:

When the GridX value is set to Gridbagconstriants.reletive, the component that is added will be placed on the right side of the previous component. Similarly, when the GridY value is set to Gridbagconstraints.reletive, the component that is added is placed below the previous component (this is a way to determine the relative placement of the current component based on the previous component)

For Gridweight and Gridheight, it is also possible to apply the gridbagconstraints remainder way, and the created component extends from the beginning of the creation to the bounds that the container can agree to. This feature allows you to create components that span certain rows or columns, changing the number of components in the corresponding direction, even if they are later added to other parts of the layout. The same is true for additional components.

Weightx,weighty: Sets the scale at which the form becomes larger.

Anchor: Sets how the component is aligned in the cell. defined by the following constants

Gridbagconstraints.center

Gridbagconstraints.east

Gridbagconstraints.west

Gridbagconstraints.south

Gridbagconstraints.north

Gridbagconstraints.southeast

Grisbagconstraints.southwest

Gridbagconstraints.northeast

Gridbagconstraints.northwest

Fill: When a component fails to fill a cell, it can be set to fill horizontally, vertically, or bidirectional by this property. defined by the following constants

Gridbagconstraints.none

Gridbagconstraints.horizontal

Gridbagconstraints.vertical

Gridbagconstraints.both

Insets: Sets the spacing of cells.

Java.lang.Object

--java.awt.insets

Insets (int top,int left,int bottom,int right)

Ipadx,ipady: Expands the minimum size of the component in the cell horizontally or vertically. If the dimensions of a component are 30*10 pixels and ipadx=2,ipady=3, the minimum size of the component in the cell is 34*16 pixels

Example four:

Import Java.awt.gridbaglayout;import java.awt.gridbagconstraints;import java.awt.insets;import javax.swing.JFrame; Import javax.swing.jbutton;/** * * @author Chel */public class Gridbaglayoutdemo {public static void main (string[] Arg s) {JFrame f=new JFrame ("GridBagLayout"); F.setlayout (New GridBagLayout ()); JButton btn=new JButton ("first"); Gridbagconstraints gbc=new gridbagconstraints (); Sets the property value of the first cell gbc.gridx=0; gbc.gridy=0; gbc.gridwidth=1; Gbc.gridheight=1; gbc.weightx=0; gbc.weighty=0; Gbc.anchor=gridbagconstraints.northwest; Gbc.fill=gridbagconstraints.none; Gbc.insets=new insets (0,0,0,0); gbc.ipadx=0; gbc.ipady=0; F.add (BTN,GBC); Sets the value of the second cell property gbc.gridx=1; gbc.gridy=0; Gbc.gridwidth=gridbagconstraints.remainder; Gbc.gridheight=1; Gbc.weightx=1; gbc.weighty=0; Gbc.anchor=gridbagconstraints.center; Gbc.fill=gridbagconstraints.horizontal; Gbc.insets=new insets (5,5,5,5); gbc.ipadx=0; gbc.ipady=0; Btn=new JButton ("second"); F.add (BTN,GBC); Sets the value of the third cell property gbc.gridx=0; Gbc.gridy=1; gbc.gridwidth=1; Gbc.gridheight=gridbagconstraints.remainder; gbc.weightx=0; Gbc.weighty=1; Gbc.anchor=gridbagconstraints.center; gbc.fill=gridbagconstraints.vertical; Gbc.insets=new insets (0,0,0,0); gbc.ipadx=10; gbc.ipady=10; Btn=new JButton ("three"); F.add (BTN,GBC); F.pack (); F.setvisible (TRUE); F.setdefaultcloseoperation (Jframe.exit_on_close); }}

Execution Result:

Effect of getting a form larger:

5.CardLayout

Java.lang.Object

--java.awt.cardlayout

Lay out the components in a cascading way, like a lot of cards stacked together, so that only the top card can be seen.

constructor Function :

CardLayout ()

Create a cardlayout with no spacing

CardLayout (int hgap,int vgap)

Create a cardlayout with a horizontal spacing of hgap and a vertical spacing of vgap

Example five:

Import Java.awt.borderlayout;import Java.awt.cardlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jframe;import Javax.swing.jpanel;import Javax.swing.JLabel; Import javax.swing.jbutton;/** * * @author Chel */public class Cardlayoutdemo {private static JPanel p; public static void Main (string[] args) {JFrame f=new JFrame ("CardLayout"); P=new JPanel (); Set the LayoutManager of JPanel p to cardlayout p.setlayout (new CardLayout ()); New two JPanel JPanel p1=new JPanel (); JPanel p2=new JPanel (); JLabel lb=new JLabel ("first panel"); P1.add (LB); Lb=new JLabel ("second panel"); P2.add (LB); Add the newly created two JPanel p1,p2 to P P.add (P1, "Frist"); P.add (P2, "second"); Sets the default display of the corresponding JPanel P1 ((cardlayout) p.getlayout ()) for first. Show (P, "Frist"); JButton cbtn=new JButton ("Change"); Cbtn.addactionlistener (new ActionListener () {public voidactionperformed (ActionEvent e) {//When Changebutton is pressed, display second corresponding JPanel P2 ((cardlayout) p.get Layout ()). Show (P, "second"); } }); F.add (Cbtn,borderlayout.north); F.add (P,borderlayout.center); F.setsize (400,150); F.setvisible (TRUE); F.setdefaultcloseoperation (Jframe.exit_on_close); }}

Execution Result:

Results after pressing Changebutton:

6.BoxLayout

Java.lang.Object

--javax.swing.boxlayout

The layout of the container in a nested box is arranged in a multi-layered manner by placing the component in a horizontal or vertical box.

constructor Function :

BoxLayout (Container target,int axis)

Establishing a horizontal or vertical boxlayout,boxlayout provides two constants X_axis and Y_axis to indicate horizontal or vertical arrangement.

Speaking of BoxLayout, you have to mention box this Container,box this container default layout is BoxLayout, and it can only use this layout, or compile will have error generated.

Java.lang.Object

--javax.swing.box

Box has both horizontal and vertical forms.

constructor Function :

Box (int axis)

Create a box Container (container) and specify how the components are arranged by using the two constants X_axis and Y_axis provided by BoxLayout.

Method :

public static Box Createhorizontalbox ()

Constructs a horizontal arrangement of box components.

Import Javax.swing.box;import javax.swing.jframe;import javax.swing.jbutton;/** * * @author Chel */public class Boxlayoutdemo {public static void main (string[] args) {JFrame f=new JFrame ("BoxLayout"); Create a horizontal Box component box Box=box.createhorizontalbox (); JButton btna=new JButton ("A"); JButton btnb=new JButton ("B"); Box.add (Btna); Box.add (BTNB); F.add (box); F.pack (); F.setvisible (TRUE); F.setdefaultcloseoperation (Jframe.exit_on_close); } }

Execution Result:

public static Component Createhorizontalglue ()

Constructs a glue component to extend horizontally.

Insert the following code in the above example 17,18

Box.add (Box.createhorizontalglue ());

Execution Result:

Effect of getting a form larger:

public static Component Createhorizontalstrut (int width)

Constructs a strut component with a horizontal specified width.

Change the example code to the following code

Box.add (Box.createhorizontalstrut ());

Execution Result:

public static Component Createrigidarea (Dimension D)

Constructs a two-dimensional rigid component that specifies the length of the width.

Change the example code to the following code

Box.add (Box.createrigidarea (New Dimension (50,50)));

Execution Result:

public static Box Createverticalbox ()

Constructs a vertically arranged box component.

public static Component Createverticalglue ()

Constructs a vertical glue component.

public static Component createverticalstrut (int height)

Constructs a vertical strut component.

public static Component Createglue ()

Constructs a glue component to extend horizontally.

Box.fillter

Fillter is the inner class (inner class) of box, which functions like rigid, can specify a limit size of length and width, and fillter can specify a maximum, a better, and a minimum length-width size.

http://www.5678520.com/kaiwangdian/130.html

http://www.5678520.com/kaiwangdian/129.html

http://www.5678520.com/kaiwangdian/128.html

http://www.5678520.com/kaiwangdian/127.html

http://www.5678520.com/kaiwangdian/126.html

http://www.lianzhiwei.com/News/389/20122116.html

http://www.lianzhiwei.com/News/389/20122115.html

http://www.lianzhiwei.com/News/389/20122114.html

http://www.lianzhiwei.com/News/389/20122113.html

http://www.lianzhiwei.com/News/389/20122112.html

Java Swing Quest (i) LayoutManager

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.