Java Learning Note (i) Grid bag layout

Source: Internet
Author: User

The grid bag layout is similar to the WIN8 's metro layout, which is used to place components in a grid of different positions in size, and the actual size of each component changes as the window changes, but the relative position is the same, which adapts well to the screen.

By reading the 21-day Learning Java book, we found a good example of a layout component, which was extracted for sharing and review.

In this book, we cite an example of a mail window that requires layout planning as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4B/69/wKioL1QrdLvi95RwAADcnWIATck294.jpg "title=" 1.png " alt= "Wkiol1qrdlvi95rwaadcnwiatck294.jpg"/>

Where two-dimensional coordinates represent the relative position of the mesh, with (0,0) as the starting point of the grid, horizontal x vertical y, and the width behind refers to the number of cells that span horizontally. Note that a grid is a whole that consists of one or more cells.

Common properties are;

GridX GridY: Cell position, upper-left position if spans multiple blocks

Gridwidth gridheight: Number of cells across the horizontal and vertical direction of the component

WEIGHTX Weighty: Component relative has been row, size of other components within column (relative weight)

Fill: extrude horizontally or vertically with optional properties as follows (in real int)

Regular members within the gridbagconstraints: NONE horizontal verital BOTH

Anchor: Alignment, optional attributes are as follows (in real int)

Regular members in Gridbagconstraints: WEST EAST is aligned to the left and right cells, respectively


In order to be able to simplify the code, you can make a layout method, each time just call the method to complete the layout of a component. The sample code is as follows:

import java.awt.*;import javax.swing.*;p ublic class learn3 extends jframe{     gridbaglayout gridbag = new gridbaglayout ();     Public learn3 () {        super ("Message");         setsize (380,120);         Setdefaultcloseoperation (Jframe.exit_on_close);         setlookandfeel ( );         setlayout (gridbag);         jlabel tolabel = new jlabel ("to:");         jtextfield to = new jtextfield ();         Jlabel subjectlabel = new jlabel ("Subject:");         jtextfield subject = new&nBsp JTextField ();         jlabel cclabel = new jlabel (" CC: ");         jtextfield cc = new jtextfield ();         jlabel bcclabel = new jlabel ("BCC");         jtextfield bcc = new jtextfield ();         addcomponent (tolabel,0,0,1,1,10,100,gridbagconstraints.none,         gridbagconstraints.east);         AddComponent (to,1,0,9,1,90,100,gridbagconstraints.horizontal,         Gridbagconstraints.west);         addcomponent (SubjectLabel, 0,1,1,1,10,100,gridbagconstraints.none,        gridbagconstraints.east );        addcomponent (subject,1,1,9,1,90,100,gridbagconstraints.horizontal,         gridbagconstraints.west);         AddComponent (cclabel,0,2,1,1,10,100,gridbagconstraints.none,         Gridbagconstraints.east);         addcomponent (cc,1,2,4,1,40,100, Gridbagconstraints.horizontal,        gridbagconstraints.west);         addcomponent (bcclabel,5,2,1,1,10,100,gridbagconstraints.none,         gridbagconstraints.east);         addcomponent (bcc,6,2,4,1,40,100,gridbagconstraints.horizontal,         gridbagconstraints.west);         setvisible (True);     }   &Nbsp;   private void addcomponent (Component comp,int gridx,int gridy     ,int gridwidth, int gridheight, int weightx, int  Weighty,     int fill, int anchor)    {        gridbagconstraints constraint = new gridbagconstraints ();        constraint.gridx = gridx;        constraint.gridy = gridy;       constraint.gridwidth  = gridwidth;       constraint.gridheight = gridheight;        constraint.weightx = weightx;        constraint.weighty = weighty;        Constraint.fill = fill;       constraint.anchor = anchor;        gridbag.setconstraints (Comp, constraint);        add (comp);    }        private void setlookandfeel () {         try{             uimanager.setlookandfeel (             " Com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel "             );             Swingutilities.updatecomponenttreeui (This);        }         catch (EXCEPTION EXC) {             //ignore error        }             }    public static void main (String[] args)  {     learn3 learn = new learn3 ();}}


Java Learning Note (i) Grid bag layout

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.