import java.awt.*;

        // Layout a number of buttons to illustrate the weight
        // attributes of a component's constraints.
class GridBagWeightFrame extends Frame {
    public GridBagWeightFrame(){
        final int width = 150, height = 150;
        setSize(width,height);
        setBackground(Color.white);

        // Use a slightly larger than normal font.
        setFont(new Font("SansSerif",Font.BOLD,20));

        // Create the layout manager.
        GridBagLayout layout = new GridBagLayout();
        setLayout(layout);
        setupComponents();
    }

        // Setup the required set of components in this container.
    protected void setupComponents(){
        // Create the constraints. Reuse this object with each button.
        GridBagConstraints constraints = new GridBagConstraints();

        // Create each button, set up its constraints and
        // add it to the frame. Reduce the weight attributes
        // of some of the buttons.
        final int singleCell = 1;
        final double shorter = 0.5, narrower = 0.25;

        initBasicConstraints(constraints,0,0,singleCell,singleCell);
        constraints.weighty = shorter;
        addButton("1",constraints);

        initBasicConstraints(constraints,1,0,singleCell,singleCell);
        constraints.weighty = shorter;
        constraints.weightx = narrower;
        addButton("2",constraints);

        initBasicConstraints(constraints,0,1,singleCell,singleCell);
        addButton("3",constraints);

        initBasicConstraints(constraints,1,1,singleCell,singleCell);
        constraints.weightx = narrower;
        addButton("4",constraints);
    }

        // Initialise the x,y width,height values for the constraints.
        // Each component has an equal non-zero weight.
    protected void initBasicConstraints(GridBagConstraints constraints,
                                int x, int y, int width, int height){
        constraints.gridx = x;
        constraints.gridy = y;
        constraints.gridwidth = width;
        constraints.gridheight = height;
        constraints.weightx = 1.0;
        constraints.weighty = 1.0;
        constraints.fill = GridBagConstraints.BOTH;
    }

        // Add a button to the Frame with the given label and constraints.
    protected void addButton(String label, GridBagConstraints constraints){
        Button b = new Button(label);
        add(b,constraints);
    }
}
