Work GridBagConstraints 3 : GridBagLayout « Swing JFC « Java






Work GridBagConstraints 3

   

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridBag3 extends JPanel {
  GridBagConstraints constraints = new GridBagConstraints();

  public GridBag3() {
    setLayout(new GridBagLayout());
    constraints.weightx = 1.0;
    constraints.weighty = 1.0;
    constraints.fill = GridBagConstraints.BOTH;
    int x, y;
    constraints.gridheight = 2; // span two rows
    addGB(new JButton("one"), x = 0, y = 0);
    constraints.gridheight = 1; // set it back
    addGB(new JButton("two"), x = 1, y = 0);
    addGB(new JButton("three"), x = 2, y = 0);
    constraints.gridwidth = 2; // span two columns
    addGB(new JButton("four"), x = 1, y = 1);
    constraints.gridwidth = 1; // set it back
  }

  void addGB(Component component, int x, int y) {
    constraints.gridx = x;
    constraints.gridy = y;
    add(component, constraints);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("GridBag3");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setLocation(200, 200);
    frame.setContentPane(new GridBag3());
    frame.setVisible(true);
  }
}

   
    
    
  








Related examples in the same category

1.GridBagLayout with weightx and weighty constraintsGridBagLayout with weightx and weighty constraints
2.GridBagLayout with constraintsGridBagLayout with constraints
3.Simplest gridbag layoutSimplest gridbag layout
4.GridBagLayout with weight constraintGridBagLayout with weight constraint
5.GridBagLayout with anchor constraintsGridBagLayout with anchor constraints
6.GridBagLayout with gridwidth and gridheight constraintsGridBagLayout with gridwidth and gridheight constraints
7.GridBagLayout PaneGridBagLayout Pane
8.Work with GridBagConstraints 1
9.Work with GridBagConstraints 2
10.GridBagConstraints.BOTH
11.Create and set a gridbag layout and how to set gridbag constraints
12.Associate the gridbag constraints with the component
13.Place a component at cell location (1,1)
14.Setting the Location of a Component in a GridBagLayout
15.Making a GridBagLayout Fill the Container
16.Getting the Number of Rows and Columns of Cells in a GridBagLayout
17.Setting the Stretchyness of Rows and Columns in a GridBagLayout Using Layout Weights
18.Place a component at (0,1) with a column weight 1 and a row weight of 2
19.Setting Stretchyness of a GridBagLayout Using Fill
20.Make the component only stretch horizontally
21.Make the component only stretch vertically
22.Make the component on stretchable
23.Make the component stretch in both directions
24.Setting the Space around a Component Within the Cell of the GridBagLayout Using Insets
25.A GridBagLayout Example: weightx, weighty
26.Positioning a component in the center of other component using GridbagLayout
27.Work with GridBagConstraints 5
28.A frame that uses a grid bag layout to arrange font selection components