Package Gui1;import Java.awt.flowlayout;import java.awt.gridlayout;import javax.swing.jbutton;import Javax.swing.JFrame; Public classGRIDLAYOUT1 extends jframe{JButton [] a={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; Public Static voidMain (string[] args) {//TODO auto-generated Method StubGRIDLAYOUT1 g1=Newgridlayout1 (); } Publicgridlayout1 () {a[0]=NewJButton ("melon Seeds"); a[1]=NewJButton ("Plum"); a[2]=NewJButton ("Watermelon"); a[3]=NewJButton ("Strawberry"); a[4]=NewJButton ("Banana"); a[5]=NewJButton ("Apple"); a[6]=NewJButton ("Chestnut"); a[7]=NewJButton ("Cucumber"); //a[8]=new JButton ("Grape"); This. setlayout (NewGridLayout (3,3, the, the));//The default is the boundary layout, if you want to change to another layout, plus This. Add (a[0]); This. Add (a[1]); This. Add (a[2]); This. Add (a[3]); This. Add (a[4]); This. Add (a[5]); This. Add (a[6]); This. Add (a[7]); This. Settitle ("Grid Layout GridLayout"); This. SetSize ( -, the); This. setlocation ( -, the); This. setresizable (true); This. Setdefaultcloseoperation (Jframe.exit_on_close); This. setvisible (true); }}
The GridLayout of Swing