No Layout Manager

While the GridBagLayout manager provides you with considerable flexibility, it is also rather Byzantine in its complexity. It's main advantage is flexibility where resizing is needed. If, however, you do not require resizing, you may find it preferable to provide explicit coordinates, width, and height for each component in the interface. If this is practical, you may use the No Layout manger option.

To provide your own explicit layout, you must do two things. First, setLayout ( null ) for the component or container. Then, use a reshape statement for each included component, providing four parameters: the x and y coordinates for the upper left corner of the component, followed by the width and height for the component. See the example code, below.

Below is code for a No Layout applet. The applet duplicates much of the code discussed with earlier Layout Managers; however, since the GridBagLayout discussion branched off into new territory, the complete program is shown.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;


public class step6 extends Applet{

// Illustrates No Layout

MyFrame outerBox;

Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;

  public void init ( ){

    outerBox = new MyFrame ( );
    outerBox.addWindowListener ( outerBox );

    button1 = new Button ( "Button 1" );
    button2 = new Button ( "Button 2" );
    button3 = new Button ( "Button 3" );
    button4 = new Button ( "Button 4" );
    button5 = new Button ( "Button 5" );
    button6 = new Button ( "Button 6" );

    button1.setBackground ( Color.red );
    button2.setBackground ( Color.lightGray );
    button3.setBackground ( Color.cyan );
    button4.setBackground ( Color.yellow );
    button5.setBackground ( Color.green );
    button6.setBackground ( Color.orange );

    outerBox.setLayout ( null );
    outerBox.setBackground ( Color.white );
    outerBox. setFont (new Font ("Helvetica", Font.PLAIN, 18) );
    outerBox.resize (600, 500);

    outerBox.add ( button1 );
    outerBox.add ( button2 );
    outerBox.add ( button3 );
    outerBox.add ( button4 );
    outerBox.add ( button5 );
    outerBox.add ( button6 );

    button1.setBounds ( 40, 80, 90, 40 );
    button2.setBounds ( 150, 200, 200, 80 );
    button3.setBounds ( 450, 150, 100, 200 );
    button4.setBounds ( 80, 400, 90, 40 );
    button5.setBounds ( 190, 400, 200, 40 );
    button6.setBounds ( 410, 400, 120, 40 );

    outerBox.show( );

  } // end init

}// end step6

Run the applet