The usage of gridbaglayout layout manager in Java.

Source: Internet
Author: User

Gidbaglayout:
(1) gridbagconstraints. gridwidthgridbagconstraints. gridheight
Specify the number of units in the display area row (for gridwidth) or column (for gridheight) of the component. The default value is 1. The following is an example of adding a button that occupies two cells (two rows and one column) to the window:
Jframe F = new jframe ();
Gridbaglayout gridbag = new gridbaglayout ();
Gridbagconstraints c = new gridbagconstraints ();
F. setlayout (gridbag );
C. gridheight = 2;
C. gridwidth = 1;
Jbutton = new jbutton ("button 1 ");
Gridbag. setconstraints (button, C );
F. Add (jbutton );
(2) gridbagconstraints. Fill
When the display area of the component is greater than the size required by the component, it is used to determine whether (and how) to adjust the component.
Possible values: gridbagconstraints. None (default ),
Gridbagconstraints. Horizontal (widening the component until it is sufficient to fill its display area horizontally without changing its height ),

Gridbagconstraints. Vertical (add the component until it is sufficient to fill its display area vertically without changing its width) and

Gridbagconstraints. Both (to make the component fully fill its display area ).
Scenario example: Add a button (the original size is 40*30) in a large window (for example, 300*300 ).

(3) gridbagconstraints. Anchor
When a component is smaller than its display area, it is used to determine where the component is placed (in the display area ). There are two possible values: relative and absolute. The relative value is relative to the componentorientation attribute of the container, but the absolute value is not. In my opinion, only the absolute value can be used. Valid values:

Absolute Value
Gridbagconstraints. North
Gridbagconstraints. South
Gridbagconstraints. West
Gridbagconstraints. East
Gridbagconstraints. Northwest
Gridbagconstraints. Northeast
Gridbagconstraints. Southwest
Gridbagconstraints. Southeast
Gridbagconstraints. Center (the default)

(4) gridbagconstraints. weightx, gridbagconstraints. weighty (the most important attribute)
This method is used to determine the distribution space, which is important for specifying adjustment behavior. For example, add two buttons (or panels) in a large window (such as 300*300) (the original size is 40*30). The default value is, you will find that the two buttons are in the upper and lower equal size areas, and only occupy a small part. The areas not occupied by the buttons are called additional areas.This additional area will be allocated with the weightx and weighty parameters.

The complete sample code is as follows:

Import javax. Swing .*;
Import java. util .*;
Import java. AWT .*;

Public class example {

Public example (){
}

Public static void main (string ARGs []) {
Jframe F = new jframe ("gridbag layout example ");

Gridbaglayout gridbag = new gridbaglayout ();
Gridbagconstraints c = new gridbagconstraints ();
F. setlayout (gridbag );
// Add button 1
C. Fill = gridbagconstraints. Both;
C. gridheight = 2;
C. gridwidth = 1;
C. weightx = 0.0; // The default value is 0.0.
C. weighty = 0.0; // The default value is 0.0.
C. Anchor = gridbagconstraints. Southwest;
Jbutton jbutton1 = new jbutton ("button 1 ");
Gridbag. setconstraints (jbutton1, C );
F. Add (jbutton1 );
// Add button 2
C. Fill = gridbagconstraints. None;
C. gridwidth = gridbagconstraints. remainder;
C. gridheight = 1;
C. weightx = 1.0; // The default value is 0.0.
C. weighty = 0.8;
Jbutton jbutton2 = new jbutton ("button 2 ");
Gridbag. setconstraints (jbutton2, C );
F. Add (jbutton2 );
// Add button 3
C. Fill = gridbagconstraints. Both;
C. gridwidth = 1;
C. gridheight = 1;
C. weighty = 0.2;
Jbutton jbutton3 = new jbutton ("button 3 ");
Gridbag. setconstraints (jbutton3, C );
F. Add (jbutton3 );

F. setdefaclocloseoperation (jframe. exit_on_close );
F. setsize (500,500 );
F. setvisible (true );
}
}

C. weighty = 0.8, and C. weighty = 0.2, which leads to the height of the area occupied by button 2, which is about 0.8/0.2 = 4 times the height of the area occupied by button 3.

Reprinted: http://sword03.blog.sohu.com/26246269.html

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.