How to use the layout manager gridbaglayout of Java "graphic description"

Source: Internet
Author: User

Https://www.cnblogs.com/taoweiji/archive/2012/12/14/2818787.html

GridBagLayout is one of the most important layout managers in Java, can make a very complex layout, can say that gridbaglayout is necessary to learn,

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

Each GridBagLayout object maintains a dynamic rectangular cell grid, each of which occupies one or more of these units, which is called the display area .

Here is a Notepad case to illustrate how gridbaglayout is used.

Analysis:

A description with arrows can be stretched.

4 occupies 4 compartments and 6 occupies 4 compartments. If the setting 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 extrusion. We should set 4 to be followed by 6 for stretching.

The Gray line is to see the approximate layout, the number of squares occupied by the component.

Run-time display effect

Import Java.awt.*;import javax.swing.*;p ublic 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 components into 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 ();//defines a gridbagconstraints,//is used to control the display position of the added component S.fill = Gridbag Constraints.both; This method is intended to set the display condition if the component's region is larger than the component itself//none: The component size is not resized. Horizontal: Widens the component so that it fills its display area horizontally, but does not change the height. VERTICAL: The heightening component, which fills its display area vertically, but does not change the width. BOTH: Causes the component to fully fill its display area. s.gridwidth=1;//the method is to set the number of squares occupied by the component level, if 0, it means that the component is the last s.weightx of the line = 0;//The method sets the horizontal stretch of the component, if 0 does not stretch, 0 is stretched as the window grows, Between 0 and 1 s.weighty=0;//the method sets the vertical stretch amplitude of the component, if 0 means no stretch, 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;//the method is to set the number of squares occupied by the component level, if 0, it means that the component is the last s.weightx of the row = 0;//cannot be 4 for 1,J4, and can be stretched horizontally,//But if 1, the following row of columns The lattice will also follow the stretching, resulting in the J7 column can also be stretched//so should be followed by J6 stretching 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;}

How to use the layout manager for Java gridbaglayout "graphics description"

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.