Use GridBagLayout to control the height and width of rows and columns

Source: Internet
Author: User

Excerpted from http://bbs.csdn.net/topics/340189065
Use GridBagLayout to control the height and width of rows and columns

Gridwidth
Specifies the number of cells in a row of a specified component display area. Default value 1, Level one grid
gridheight Specifies the number of cells in a column in the component display area. Default value 1, one grid vertically
weightx Specifies how additional horizontal space is distributed. The default value is 0, and the extra horizontal space is not allocated.
Weighty Specifies how additional vertical space is distributed. The default value is 0, and additional vertical space is not allocated.

After stretching the effect is as follows

1  Packagecom.hw.gridbaglayout;2 3 ImportJava.awt.Button;4 ImportJava.awt.Font;5 Importjava.awt.GridBagConstraints;6 Importjava.awt.GridBagLayout;7 ImportJava.awt.event.WindowAdapter;8 Importjava.awt.event.WindowEvent;9 Ten ImportJavax.swing.JFrame; One ImportJavax.swing.JPanel; A  -  Public classGridBagEx2extendsJPanel - { the     Private Static Final LongSerialversionuid = -5214441555967215113l; -  -     protected voidMakebutton (String name, GridBagLayout gridbag, - gridbagconstraints c) +     { -Button Button =NewButton (name); + gridbag.setconstraints (button, c); A Add (button); at     } -  -      Public voidInit () -     { -GridBagLayout Gridbag =Newgridbaglayout (); -Gridbagconstraints C =Newgridbagconstraints (); in  -SetFont (NewFont ("Sansserif", Font.plain, 14)); to setlayout (gridbag); +  -C.fill =Gridbagconstraints.both; theC.WEIGHTX = 1.0; *Makebutton ("Button1", Gridbag, c); $Makebutton ("Button2", Gridbag, c);Panax NotoginsengMakebutton ("Button3", Gridbag, c); -  theC.gridwidth = Gridbagconstraints.remainder;//End Row +Makebutton ("Button4", Gridbag, c); A  thec.weightx = 0.0;//Reset to the default +Makebutton ("Button5", Gridbag, C);//another row -  $C.gridwidth = gridbagconstraints.relative;//next-to-last in row $Makebutton ("Button6", Gridbag, c); -  -C.gridwidth = Gridbagconstraints.remainder;//End Row theMakebutton ("Button7", Gridbag, c); - WuyiC.gridwidth = 1;//Reset to the default theC.gridheight = 2; -C.weighty = 1.0; WuMakebutton ("Button8", Gridbag, c); -  AboutC.weighty = 0.0;//Reset to the default $C.gridwidth = Gridbagconstraints.remainder;//End Row -C.gridheight = 1;//Reset to the default -Makebutton ("Button9", Gridbag, c); -Makebutton ("Button10", Gridbag, c); A  +SetSize (300, 100); the     } -  $      Public Static voidMain (String args[]) the     { theJFrame f =NewJFrame ("Gridbag Layout Example"); theF.setlocation (400, 200); theGRIDBAGEX2 Ex1 =NewGridBagEx2 (); -  in ex1.init (); the  theF.add ("Center", EX1); About F.pack (); the f.setsize (F.getpreferredsize ()); theF.setvisible (true); theF.addwindowlistener (NewWindowadapter () +         { -  the @OverrideBayi              Public voidwindowclosing (windowevent e) the             { theSystem.exit (0); -             } -              the         }); the     } the  the}

Use GridBagLayout to control the height and width of rows and columns

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.