Gridbaglayout can be used to implement a complex and controllable layout.
Take an input box as an example:
<Textarea readonly name = "code" class = "Java"> final jtextfield username = new jtextfield (); </P> <p> final jtextfield Password = new jtextfield (); </P> <p> jpanel regist = new jpanel (); </P> <p> regist. setlayout (New gridbaglayout (); <br/> regist. add (New jlabel ("username:"), <br/> New gridbagconstraints (0.0, 0.0, <br/>, <br/> gridbagconstraints. center, <br/> gridbagconstraints. none, <br/> New insets (,), <br/> )); </P> <PRE name = "code" class = "Java"> </PRE> <p> </textarea><Textarea readonly name = "code" class = "Java"> regist. add (username, <br/> New gridbagconstraints (0.0, 0.0, <br/>, <br/> gridbagconstraints. center, <br/> gridbagconstraints. none, <br/> New insets (,), <br/> )); </P> <PRE name = "code" class = "Java"> <PRE name = "code" class = "Java"> regist. add (New jlabel ("password"), new gridbagconstraints (0.0, 0.0, gridbagconstraints. center, gridbagconstraints. none, new insets (,),); </PRE> <p> </textarea>
<PRE name = "code" class = "Java"> regist. add (password, new gridbagconstraints (0.0, 0.0, gridbagconstraints. center, gridbagconstraints. none, new insets (,),); </PRE> <p>
Gridbagconstraints is used to define the position arrangement information of components.
The final effect is as follows:
You can change the length of the input box to 200.
For details about gridbagconstraints, see gridbagconstraints