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