grid bag layout most complex and powerful components can vary in size and can be added in any order...
TRANSCRIPT
Grid Bag Layout
• Most Complex and Powerful
• Components can vary in size and can be added in any order
• Draw out on piece of paper first
• Columns and Rows: Components placed in a grid, and preferred sized used to determine how big
• Grig Bag Constraints determine how the object is placed in the layout
Grid Bag Constraints
• gridx: the column in which the component is placed (start at 0)
• gridy: the row in which the column is placed (start at 0)
• gridwidth: the number of columns the component occupies
• gridheight: the number of rows the component occupies
• weightx: Portion of extra space in x when available (resize, 0-1). When space available, grows wider. 0 means doesn’t grow.
• weighty: Portion of extra space in y when available (resize, 0-1) When space available, grows taller. 0 means doesn’t grow.
• fill: How much of components area is occupied. – NONE:component will not grow
– BOTH: component grows in both directions.
– HORIZONTAL
– VERTICAL
fill
Used when the component's display area is larger than the component's requested size to determine whether and how to resize the component. Valid values are GridBagConstraints.NONE (the default), GridBagConstraints.HORIZONTAL (make the component wide enough to fill its display area horizontally, but don't change its height), GridBagConstraints.VERTICAL (make the component tall enough to fill its display area vertically, but don't change its width), and GridBagConstraints.BOTH (make the component fill its display area entirely).
Variation
• Don’t use gridx and gridy• REMAINDER: Last component in the row
• RELATIVE: Next to last component in the row
/* * 1.1 version. */
import java.awt.*;import java.awt.event.*;
public class GridBagWindow extends Frame { boolean inAnApplet = true;
protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) { Button button = new Button(name); gridbag.setConstraints(button, c); add(button); }
public GridBagWindow() { GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints();
setFont(new Font("SansSerif", Font.PLAIN, 14)); setLayout(gridbag);
c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; makebutton("Button1", gridbag, c); makebutton("Button2", gridbag, c); makebutton("Button3", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end of row makebutton("Button4", gridbag, c);
c.weightx = 0.0; //reset to the default makebutton("Button5", gridbag, c); //another row
c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last makebutton("Button6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end of row makebutton("Button7", gridbag, c);
c.gridwidth = 1; //reset to the default c.gridheight = 2; c.weighty = 1.0; makebutton("Button8", gridbag, c);
c.weighty = 0.0; //reset to the default c.gridwidth = GridBagConstraints.REMAINDER; //end of row c.gridheight = 1; //reset to the default makebutton("Button9", gridbag, c); makebutton("Button10", gridbag, c);
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (inAnApplet) { dispose(); } else { System.exit(0); } } }); }
public static void main(String args[]) { GridBagWindow window = new GridBagWindow(); window.inAnApplet = false;
window.setTitle("GridBagWindow Application"); window.pack(); window.setVisible(true); }}
Example Program
No fill set
No weightx set