GridBagLayout layout implements the simple email sending interface and gridbaglayout Layout

Source: Internet
Author: User

GridBagLayout layout implements the simple email sending interface and gridbaglayout Layout

Grid Block Layout (GridBagLayoutIn JAVA, it is the most complex and powerful. It is developed on the basis of GridLayout. This layout also divides the entire container into several rows and columns, but each row can have different heights and different widths. One part can occupy one, it can also occupy multiple cells.

GridBagLayoutA lot of information is required to describe the position, size, and scalability of a part during component arrangement. Therefore, when adding a part in the layout, you must specify a constraint pair (GridBagConstraints),

The constraint data related to location and size is encapsulated. The specific command format is: add (part, constraint object );

Common attributes of a constrained object are as follows:

I: specified location attribute: generally passedGridx, gridySpecify the location where the layout occupies the cell. The upper left corner is 0. You can also use the direction position parameter to control the location of the widget, similarBorderLayoutLayout. The direction and position parameters include:CENTER, EAST, NORTH, NORTHEAST, SOUTH, SOUTHWEST, WEST.

II:Gridheight, gridwidth: Number of cells occupied by parts. When specifying the position and height and width of a part, two constants can also be used:GridwidthThe value isRELATIVEIndicates that the widget occupies the next position relative to the previous widget. IfGridwidthThe value isREMAINDER, Indicates that the part occupies all the remaining cells in this column. If the gridwidth of the last cell in a row uses the REMAINDER, the value of gridwidth in the next row should be changed to 1; otherwise, the first part of the next line will be full.

III:RowHeights, columnWidth: Specifies the Row Height and column width. By default, the downlink and width are determined by the highest and widest parts.

IV:Weightx, weighty: Controls the row and width of a cell. You can specify only one part in one row and one column to specify the extension parameter. This ensures that the part size is adjusted when the form size changes.

V: Fill (Fill) Attribute: specifies how the part fills in the grid. Constants includeBOTH, HORIZONTAL, VERTICAL, and NONE, WhereBOTHIt means horizontal and vertical stretching, that is, occupying the entire cell; andNONEIt indicates that the parts are not stretched and the original size is maintained.

Paste the Code:

Import java. awt. *; import java. awt. event. windowAdapter; import java. awt. event. using wevent; public class GridBagLayoutExample extends Frame {private static final long serialVersionUID = 1L; public GridBagLayoutExample () {super ("GB memory programming"); Label using elabel = new Label ("Recipient: "); Label ccLabel = new Label (" cc: "); Label subjectLabel = new Label (" topic "); TextField receiveField = new TextField (); // recipient; textField ccField = new TextField (); // cc; TextField subjectField = new TextField (); // topic; TextArea mailArea = new TextArea (8, 40); // enter the text area of the email; setLayout (new GridBagLayout (); GridBagConstraints gridBag = new GridBagConstraints (); gridBag. fill = GridBagConstraints. HORIZONTAL; // horizontally filled; gridBag. weightx = 0; // The Governor remains unchanged. gridBag. weighty = 0; // The height of the row remains unchanged. addToBag (receiveLabel, gridBag,); // recipient tag; addToBag (ccLabel, gridBag ); // CC label; addToBag (subjectLabel, gridBag,); // subject label location gridBag. weightx = 100; // adaptive gridBag for row scaling. weighty = 0; // The Row Height is unchanged. addToBag (receiveField, gridBag,); addToBag (ccField, gridBag,); addToBag (subjectField, gridBag, 2, 1); gridBag. fill = GridBagConstraints. BOTH; // layout in full filling mode; gridBag. weightx = 100; // adaptive gridBag for row scaling. weighty = 100; // adaptive column scaling; addToBag (mailArea, gridBag,); // occupies a row of addWindowListener (new WindowAdapter () {public void windowClosing (incluwevent e) {dispose () ;}}); setSize (300,300); setVisible (true );} /* Add a part to the specified position of the GridBagLayout layout according to the specified size */void addToBag (Component c, GridBagConstraints gdc, int x, int y, int w, int h) {gdc. gridx = x; gdc. gridy = y; gdc. gridheight = h; gdc. gridwidth = w; add (c, gdc); // add parts according to specified constraints;} public static void main (String [] args) {new GridBagLayoutExample ();}}

If this article is helpful to you, click the follow GB memory blog button.
 

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.