A GridBagLayout Example: weightx, weighty : GridBagConstraints « Swing « Java Tutorial






import java.awt.Button;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;

public class Main extends JPanel {
  protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) {
    Button button = new Button(name);
    gridbag.setConstraints(button, c);
    add(button);
  }
  public Main() {
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(gridbag);
    c.weightx = 1.0;
    c.weighty = 1.0;
    makebutton("Button 1", gridbag, c);
    c.fill = GridBagConstraints.BOTH;
    makebutton("Button 2", gridbag, c);
  }

  public static void main(String args[]) {
    Frame f = new Frame();
    JPanel mgb = new Main();
    f.add("Center", mgb);
    f.pack();
    f.setSize(300, 300);
    f.setVisible(true);
  }
}








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