Example usage for java.awt GridBagLayout setConstraints

List of usage examples for java.awt GridBagLayout setConstraints

Introduction

In this page you can find the example usage for java.awt GridBagLayout setConstraints.

Prototype

public void setConstraints(Component comp, GridBagConstraints constraints) 

Source Link

Document

Sets the constraints for the specified component in this layout.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JFrame demo = new JFrame("GridBag demo, to center a component");
    JPanel parentPanel = new JPanel();
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.CENTER;
    gridbag.setConstraints(parentPanel, constraints);
    parentPanel.setLayout(gridbag);/*from  w ww  .j a  v  a 2s.c om*/
    Label centerLabel = new Label(" AAA...");
    parentPanel.add(centerLabel);
    demo.add(parentPanel);
    demo.setSize(500, 500);
    demo.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();

    GridBagLayout gbl = new GridBagLayout();

    container.setLayout(gbl);/*www.j a  va2 s . c o m*/

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;

    JButton component = new JButton("a");

    gbl.setConstraints(component, gbc);

    container.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();

    GridBagLayout gbl = new GridBagLayout();

    container.setLayout(gbl);/*from   w  w w. ja va  2 s.c om*/

    GridBagConstraints gbc = new GridBagConstraints();
    JButton component1 = new JButton("a");
    JButton component2 = new JButton("b");

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbl.setConstraints(component1, gbc);
    container.add(component1);

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbl.setConstraints(component2, gbc);
    container.add(component2);

    container.add(component1);
    container.add(component2);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();

    GridBagLayout gbl = new GridBagLayout();

    container.setLayout(gbl);//from w  ww  .  ja v a  2 s  . co m

    GridBagConstraints gbc = new GridBagConstraints();
    JButton component1 = new JButton("a");
    JButton component2 = new JButton("b");

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbl.setConstraints(component1, gbc);
    container.add(component1);

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbl.setConstraints(component2, gbc);
    container.add(component2);

    container.add(component1);
    container.add(component2);
    frame.pack();
    frame.setVisible(true);

    gbl.layoutContainer(container);

    int[][] dim = gbl.getLayoutDimensions();
    int cols = dim[0].length;
    int rows = dim[1].length;

    System.out.println(cols);
    System.out.println(rows);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();

    frame.setLayout(gbl);/*w  w w  . ja  v a2  s .c  om*/
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));

    gbl.layoutContainer(frame);

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.fill = GridBagConstraints.NONE;

    gbl.setConstraints(component, gbc);
    frame.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();

    frame.setLayout(gbl);/*from w  ww  . ja  v  a  2  s.  c  o  m*/
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));

    gbl.layoutContainer(frame);

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.fill = GridBagConstraints.BOTH;

    gbl.setConstraints(component, gbc);
    frame.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();

    frame.setLayout(gbl);/*from   w w w . j a va  2 s.c om*/
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));

    gbl.layoutContainer(frame);

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.fill = GridBagConstraints.VERTICAL;

    gbl.setConstraints(component, gbc);
    frame.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();

    frame.setLayout(gbl);// w ww  .ja v a  2s . c om
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));

    gbl.layoutContainer(frame);

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.fill = GridBagConstraints.HORIZONTAL;

    gbl.setConstraints(component, gbc);
    frame.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();

    GridBagLayout gbl = new GridBagLayout();

    container.setLayout(gbl);/*from  w  w w . java2  s  .c  om*/

    // Place a component at cell location (1,1)
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;

    JButton component = new JButton("a");

    // Associate the gridbag constraints with the component
    gbl.setConstraints(component, gbc);

    container.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();

    frame.setLayout(gbl);/*from  w w  w .j a v  a2  s  .c o m*/
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));

    gbl.layoutContainer(frame);

    GridBagConstraints gbc = new GridBagConstraints();

    // Make the component on stretchable
    gbc.fill = GridBagConstraints.NONE;

    gbl.setConstraints(component, gbc);
    frame.add(component);

    frame.pack();
    frame.setVisible(true);
}