Swing-setbounds () Usage-Getting Started

Source: Internet
Author: User

Look at the API first:

public void SetBounds (Rectangle R)

Move the component and resize it to conform to the new bounded rectangle R. The new location of the component is specified by R.x and R.y, and the new size of the component is specified by R.width and r.height

Parameter: R-New bounded rectangle for this component

Judging from the API, this method is equivalent to the sum of setlocation () and SetSize (). In practice, you set the layout of the container to NULL, because the location and dimensions of the control are assigned by the layout manager when using the layout manager. Note that you must manually specify the dimensions of the container at this point, because the empty layout manager will clear the preferredsize of the container itself, causing the container to not display on the GUI. Therefore, if the container is arranged in the parent container using the layout manager, then use Setpreferredsize () and if the container is still manually arranged in the parent container, use SetBounds () for the container. Here is the test demo:

Importjava.awt.Dimension;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;/** 2015-06-14*/ Public classSetboundsdemo { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//set layout and sieze for panelJPanel JPanel =NewJPanel (); System.out.println ("Default preferredsize is" +jpanel.getpreferredsize ()); System.out.println ("Default Size is" +jpanel.getsize ()); Jpanel.setlayout (NULL); System.out.println ("In null layout, the preferredsize is" +jpanel.getpreferredsize ()); System.out.println ("In null layout, the Size is" +jpanel.getsize ()); Jpanel.setpreferredsize (NewDimension (400, 400)); //Add buttonJButton button11 =NewJButton ("SetBounds"); JButton Button12=NewJButton ("Setlocationandsetsize"); Button11.setbounds (20, 20, 100, 100); Button12.setlocation (250, 250); Button12.setsize (100, 100);        Jpanel.add (BUTTON11);                Jpanel.add (BUTTON12); //Set Form PropertiesJFrame frame =NewJFrame ("Setboundsdemo");        Frame.setdefaultcloseoperation (Jframe.exit_on_close);        Frame.add (JPanel);        Frame.pack (); Frame.setlocationrelativeto (NULL); Frame.setvisible (true); }}

The results are as follows:

Run

The program output is as follows:

Default preferredsize is java.awt.dimension[width=10,height=10]
Default Size is java.awt.dimension[width=0,height=0]
In null layout, the preferredsize is java.awt.dimension[width=0,height=0]
In null layout, the Size is java.awt.dimension[width=0,height=0]

Swing-setbounds () Usage-Getting Started

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.