7. Java graphical interface design-null layout of layout manager (empty layout) and graphical null

Source: Internet
Author: User

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:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.