Java most important layout manager GridBagLayout methods of use _java

Source: Internet
Author: User

GridBagLayout is one of the most important Java layout manager, can make a very complicated layout, can say GridBagLayout is must learn,

GridBagLayoutClass is a flexible layout manager that does not require the same size of components to align components vertically, horizontally, or along their baselines.

Each GridBagLayout object maintains a dynamic rectangular cell grid, and each component occupies one or more such units, which are called display areas .

Here is a Notepad case to illustrate the use of gridbaglayout.

Analysis :

A description with an arrowhead can be stretched.

4 occupies 4 squares and 6 occupies 4 squares. If the set 6 can be stretched, then 4 will also be stretched.

However, if you set the 4 stretch, the 7 column can also be stretched, so 4 cannot set the stretch. We should set 4 to follow 6 for stretching.

The Gray line is to see the layout of the approximate, components occupy the number of squares.

Display effects at run time

Copy Code code as follows:

Import java.awt.*;
Import javax.swing.*;

public class Gridbagdemo extends JFrame {
public static void Main (String args[]) {
Gridbagdemo demo = new Gridbagdemo ();
}

Public Gridbagdemo () {
Init ();
This.setsize (600,600);
This.setvisible (TRUE);
}
public void init () {
J1 = new JButton ("open");
J2 = new JButton ("Save");
J3 = new JButton ("Save As");
J4 = new JPanel ();
string[] str = {"Java notes", "C # Notes", "HTML5 Notes"};
J5 = new JComboBox (str);
J6 = new JTextField ();
J7 = new JButton ("Empty");
J8 = new JList (str);
J9 = new JTextArea ();
J9.setbackground (Color.pink)//To see the effect, set the color
GridBagLayout layout = new GridBagLayout ();
This.setlayout (layout);
This.add (J1);//Add component to JFrame
This.add (J2);
This.add (J3);
This.add (J4);
This.add (J5);
This.add (J6);
This.add (J7);
This.add (J8);
This.add (J9);
Gridbagconstraints s= new Gridbagconstraints ();//define a gridbagconstraints,
is used to control the display position of the component being added
S.fill = Gridbagconstraints.both;
The method is to set the display if the area of the component is larger than the component itself
None: The component size is not adjusted.
Horizontal: Widens the assembly so that it fills its display area horizontally, but does not change the height.
VERTICAL: Heightening the component so that it fills its display area vertically, but does not change the width.
BOTH: Causes the component to fully fill its display area.
s.gridwidth=1;//This method is to set the number of squares that the component level occupies, and if 0 indicates that the component is the last of the line
S.WEIGHTX = 0;//This method sets the level of the component to stretch, if 0 is not stretched, not 0, as the window increases to stretch, between 0 to 1
s.weighty=0;//This method sets the vertical stretch of the component, if 0 is not stretched, not 0 is stretched as the window grows, between 0 and 1
Layout.setconstraints (J1, s);//Set component
s.gridwidth=1;
S.WEIGHTX = 0;
s.weighty=0;
Layout.setconstraints (J2, s);
s.gridwidth=1;
S.WEIGHTX = 0;
s.weighty=0;
Layout.setconstraints (J3, s);
s.gridwidth=0;//This method is to set the number of squares that the component level occupies, and if 0 indicates that the component is the last of the line
S.WEIGHTX = 0;//cannot be 1,j4 for 4 cells, and can be stretched horizontally,
However, if 1, the column in the following row will also be stretched, causing the J7 column to stretch
So it's supposed to be stretching along with J6.
s.weighty=0;
Layout.setconstraints (J4, s)
; s.gridwidth=2;
S.WEIGHTX = 0;
s.weighty=0;
Layout.setconstraints (J5, s);
; s.gridwidth=4;
S.WEIGHTX = 1;
s.weighty=0;
Layout.setconstraints (J6, s);
; s.gridwidth=0;
S.WEIGHTX = 0;
s.weighty=0;
Layout.setconstraints (J7, s);
; s.gridwidth=2;
S.WEIGHTX = 0;
S.weighty=1;
Layout.setconstraints (J8, s);
; s.gridwidth=5;
S.WEIGHTX = 0;
S.weighty=1;
Layout.setconstraints (J9, s);
}
JButton J1;
JButton J2;
JButton J3;
JPanel J4;
JComboBox J5;
JTextField J6;
JButton J7;
JList J8;
JTextArea J9;
}
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.