Excerpted from http://blog.csdn.net/liujun13579/article/details/7774267
33. Java graphical interface design--null layout of layout manager (empty layout)
The general container has a default layout, but sometimes you need to specify the size and location of each build, and you need to use an empty layout.
Operation Method:
1) First Use the setlayout (NULL) statement to set the container's layout to a null layout (empty layout).
2) re-call the component's setbounds (int x, int y, int width,int height) method to set the size and position of the component in the container, in pixels.
- X is the distance of the left edge of the control from the left edge of the form
- Y is the distance of the top edge of the control from the top edge of the form
- Width for control widths
- Height for control Heights
Example: Using an empty layout to pinpoint the position of a component
1 //Nulllayoutdemo.java2 3 Importjava.awt.*;4 5 Importjavax.swing.*;6 7 Public classnulllayoutdemo{8 9 JFrame fr;Ten One JButton A, b; A - Nulllayoutdemo () { - theFR =NewJFrame (); - -Fr.setbounds (100,100,250,150); - + //set form to empty layout - +Fr.setlayout (NULL); A atA=NewJButton ("button a"); - -b=NewJButton ("button B"); - - Fr.getcontentpane (). Add (a); - in //set the exact position of button a - toA.setbounds (30,30,80,25); + - Fr.getcontentpane (). Add (b); the *B.setbounds (150,40,80,25); $ Panax NotoginsengFr.settitle ("Nulllayoutdemo"); - theFr.setvisible (true); + A fr.setdefaultcloseoperation (jframe.exit_on_close); the +Fr.setlocationrelativeto (NULL);//to center the form on the display - $ } $ - Public Static voidMain (String args[]) { - the NewNulllayoutdemo (); - Wuyi } the -}
The results of the program run as follows:
33. Java graphical interface design--null layout of layout manager (empty layout)