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

Source: Internet
Author: User

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)

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.