A Simple Application That Uses GridBagConstraints.WEST : GridBagConstraints « Swing « Java Tutorial






A Simple Application That Uses GridBagConstraints.WEST
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GridBagConstraintsSimplePanel extends JPanel {

  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new GridBagConstraintsSimplePanel());
    f.setSize(400, 300);
    f.setVisible(true);
  }

  public GridBagConstraintsSimplePanel() {
    super();
    GridBagConstraints constraints = new GridBagConstraints();
    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);

    constraints.anchor = GridBagConstraints.WEST;

    constraints.gridy = 0;
    JLabel label = new JLabel("First name:");
    add(label, constraints);

    JTextField tf = new JTextField(8);
    add(tf, constraints);
    label = new JLabel("Last name:");
    add(label, constraints);

    tf = new JTextField(8);
    add(tf, constraints);

    constraints.gridy = 1;
    label = new JLabel("Address:");
    add(label, constraints);

    tf = new JTextField(10);
    add(tf, constraints);
  }

}








14.95.GridBagConstraints
14.95.1.Using GridBagConstraintsUsing GridBagConstraints
14.95.2.Adding Components with a Relative X PositionAdding Components with a Relative X Position
14.95.3.Adding Components with a Relative Y PositionAdding Components with a Relative Y Position
14.95.4.Adding Components with Relative X and Y CoordinatesAdding Components with Relative X and Y Coordinates
14.95.5.Effects of the fill ConstraintEffects of the fill Constraint
14.95.6.Effects of the gridwidth ConstraintEffects of the gridwidth Constraint
14.95.7.Filling the Entire ColumnFilling the Entire Column
14.95.8.Using the REMAINDER Value for a WidthUsing the REMAINDER Value for a Width
14.95.9.Effects of the gridheight ConstraintEffects of the gridheight Constraint
14.95.10.A Simple Application That Uses GridBagConstraints.WESTA Simple Application That Uses GridBagConstraints.WEST
14.95.11.Use GridBagLayout to layout RadioButtonsUse GridBagLayout to layout RadioButtons
14.95.12.A GridBagLayout Example: weightx, weighty