7. Java graphical interface design-null layout of layout manager (empty layout) and graphical null
Reprinted: http://blog.csdn.net/liujun13579/article/details/7774267
Generally, containers use the default Layout mode. However, if you need to specify the size and position of each component accurately, an empty layout is required.
Operation Method:
1) First, use the setLayout (null) statement to set the container layout to null (empty layout ).
2) Call the setBounds (int x, int y, int width, int height) method of the component to set the size and position of the component in the container. The unit is pixel.
X represents the distance between the left edge of the control and the left edge of the form.
Y indicates the distance between the upper edge of the control and the upper edge of the form.
Width indicates the control width.
Height is the height of the control.
Example: use an empty layout to precisely locate the 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 the form to empty Layout
Fr. setLayout (null );
A = new JButton ("button ");
B = new JButton ("button B ");
Fr. getContentPane (). add ();
// Set the exact position of button
A. setBounds (30, 30, 80, 25 );
Fr. getContentPane (). add (B );
B. setBounds );
Fr. setTitle ("NullLayoutDemo ");
Fr. setVisible (true );
Fr. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
Fr. setLocationRelativeTo (null); // center the form
}
Public static void main (String args []) {
New NullLayoutDemo ();
}
}
The program running result is as follows: