Way2Java

Java No Layout Manager setBounds()

We know earlier in "Java Layout Managers" that two styles of placing components in containers – pixel format and position format. Now let us discuss clearly what pixel format is and how to place a component in terms of pixels.

We also know earlier, the default layout manager for Frame is BorderLayout and for Panel and Applet, it is FlowLayout. To use pixel format, first we must set the default layout manager of the container to null. To give the size and location, setBounds() method is used. But, this style is very rarely used (to avoid the complexity of GridBagLayout).

Following is the method signature as defined in java.awt.Component class.

setBounds(int x, int y, int width, int height): First two coordinates x and y give the position in the container and the last two width and height give the size of the component.

Example on Java No Layout Manager setBounds()

In the following program, one label and two buttons are placed using setBounds() method.

import java.awt.*;
public class NullLayoutManager extends Frame
{
  Button agreedBut, denyBut;
  Label sign;
  public NullLayoutManager()
  {
    super("No Layout Manager");
    setLayout(null);

    sign = new Label("Click Your Acceptance");
    agreedBut = new Button("AGREED");
    denyBut = new Button("DENIED");
 
    sign.setBounds(80, 35, 130, 25);
    agreedBut.setBounds(60, 75, 75, 25);
    denyBut.setBounds(150, 75, 75, 25);
    
    add(sign);   add(agreedBut);  add(denyBut);

    setSize(260, 130);
    setVisible(true);
  }
  public static void main(String args[])
  {
    new NullLayoutManager ();
  }
}

super(“No Layout Manager”);

This is another style of using setTitle(). The super class constructor of Frame is called, with super(), and passed the string as parameter and this string is placed on the frame title bar.

setLayout(null);

To use setBounds() method, first the default layout manager BorderLayout of frame should be set to null.

sign.setBounds(80, 35, 130, 25);
agreedBut.setBounds(60, 75, 75, 25);

The label sign is placed at x-coordinate of 80 and y-coodinate 35 pixels. The width of the label is given as 130 pixels and height as 25 pixels. Simialrly, the agreeBut is placed at 60 and 75 coordinates with 75 pixels width and 25 height.

Java No Layout Manager setBounds()

Dimension class

Another example on Java No Layout Manager setBounds()

java.awt.Dimension class two fields height and width that give the height and width of a component. Following program uses setSize() and getSize() methods of Component class to set the size to a component and get the size of the component.

import java.awt.*;
import java.awt.event.*; 
public class TwoSizes extends Frame
{   
  public TwoSizes()   
  {  
    setLayout( null ) ;
    Button btn1 = new Button("OK" );		
    btn1.setBounds(30, 60, 100, 40);    

    Button btn2 = new Button("Cancel");		

    Dimension d1 = btn1.getSize();
    Dimension d2 = new Dimension(d1.width*2, d1.height*2);
    btn2.setBounds(150, 60, 100, 40);
    btn2.setSize(d2);

    add(btn1);    add(btn2);

    setSize(600, 300);  
    setVisible( true ) ;
  } 
  public static void main( String args[])   
  {
    new TwoSizes();
  } 
}

btn1.setBounds(30, 60, 100, 40);

The button btn1 is set to a size of 100 width, 40 height and placed at pixels 30 and 60.

Dimension d1 = btn1.getSize();
Dimension d2 = new Dimension(d1.width*2, d1.height*2);

The getSize() method of Button (inherited from Component class) returns the size of the button btn1 as a Dimension object. Another Dimension object d2 is created with double the size of width and height of btn1. width and height are the fields (variables) of Dimension class that return the width and height of the component wrapped with it.

btn2.setBounds(150, 60, 100, 40);
btn2.setSize(d2);

Initially, a location and size is given to the button btn2. Later the size is changed to the double size of btn1 and placed at x and y coordinates 150 and 60.