PackageCN. Douzi.graphics;Importjava.awt.*;Importjavax.swing.*;/*** BorderLayout Demo * 1. Inherit JFrame * 2. Define the individual components you need * 3. Create a component (constructor) *@authorDouzi **/ Public classDemo_layoutextendsJFrame {//Defining ComponentsJButton jb1, JB2, Jb3, JB4, jb5; Public Static voidMain (string[] args) {demo_layout test1=Newdemo_layout (); } Publicdemo_layout () {//Creating ComponentsJB1 =NewJButton ("East"); JB2=NewJButton ("North"); JB3=NewJButton ("Middle"); JB4=NewJButton ("South"); JB5=NewJButton ("West"); //Adding individual components /*** 1. Do not add all components * 2. The middle piece will automatically adjust the size*/ This. Add (Jb1, borderlayout.east); This. Add (jb2, Borderlayout.north); This. Add (Jb3, borderlayout.center); This. Add (Jb4, Borderlayout.south); This. Add (Jb5, borderlayout.west); //Set Form Properties This. Settitle ("Border layout case"); This. setSize (300, 300); This. setlocation (300, 300); This. Setdefaultcloseoperation (Jframe.exit_on_close); This. setvisible (true); } }
PackageCN. Douzi.graphics;Importjava.awt.*;Importjavax.swing.*; Public classDemo_flowlayoutextendsJFrame {JButton jb1, jb2, Jb3, Jb4, Jb5, Jb6; Public Static voidMain (string[] args) {//TODO auto-generated Method StubDemo_flowlayout test =Newdemo_flowlayout (); } Publicdemo_flowlayout () {//Creating ComponentsJB1 =NewJButton ("Guan Yu"); JB2=NewJButton ("Zhang Fei")); JB3=NewJButton ("Zhao Yun"); JB4=NewJButton ("Jack Huang"); JB5=NewJButton ("Ma Chao"); Jb6=NewJButton ("Wei Yan"); //Adding Components This. Add (JB1); This. Add (JB2); This. Add (JB3); This. Add (JB4); This. Add (JB5); This. Add (JB6); //setting up the layout manager//Default Center Alignment This. setlayout (NewFlowLayout (flowlayout.left)); //Set Form Properties This. Settitle ("Border layout case"); This. setSize (300, 300); This. setlocation (300, 300); //disable resizing of Windows This. setresizable (false); //when off, exit This. Setdefaultcloseoperation (Jframe.exit_on_close); This. setvisible (true); }}
PackageCN. Douzi.graphics;Importjava.awt.*;Importjavax.swing.*;/*** Grid Layout * The relative position of the component does not vary with the container scaling * All components are the same size * can be GridLayout (int rows, int cols, int hgap, int vgap) * Specify grid row/column, horizontal gap/Vertical Room Gap *@authorDouzi*//*** Develop GUI program steps * Inherit JFrame * Define Required Components * Create components * Set Layout Manager * Add components * Show Form*/ Public classDemo_gridlayoutextendsJFrame {intSize = 9; JButton jbs[]=NewJbutton[size]; Publicdemo_gridlayout () { for(inti = 0; i < size; i++) {Jbs[i]=NewJButton (string.valueof (i)); } //Set Grid Layout This. setlayout (NewGridLayout (3, 3, 10, 10)); //Adding Components for(inti = 0; i < size; i++) { This. Add (Jbs[i]); } //Set Form Properties This. Settitle ("Grid layout case"); This. setSize (300, 300); //Off then off This. Setdefaultcloseoperation (Jframe.exit_on_close); This. setlocation (300, 300); //no change in size This. setresizable (false); //Show This. setvisible (true); } Public Static voidMain (string[] args) {demo_gridlayout grid=Newdemo_gridlayout (); }}
Swing layout Manager