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

Source: Internet
Author: User

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)

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.