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
Nulllayoutdemo.java
Import java.awt.*;
Import javax.swing.*;
public class nulllayoutdemo{
JFrame fr;
JButton A, B;
Nulllayoutdemo () {
FR = new JFrame ();
Fr.setbounds (100,100,250,150);
Set form to empty layout
Fr.setlayout (NULL);
A=new JButton ("button A");
B=new JButton ("button B");
Fr.getcontentpane (). Add (a);
Set the exact position of button a
A.setbounds (30,30,80,25);
Fr.getcontentpane (). Add (b);
B.setbounds (150,40,80,25);
Fr.settitle ("Nulllayoutdemo");
Fr.setvisible (TRUE);
Fr.setdefaultcloseoperation (Jframe.exit_on_close);
Fr.setlocationrelativeto (NULL); To center the form on the display
}
public static void Main (String args[]) {
New Nulllayoutdemo ();
}
}
The results of the program run as follows:
Java graphical interface design--null layout of layout manager (empty layout)