grid bag layout most complex and powerful components can vary in size and can be added in any order...

13
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

Upload: patience-glenn

Post on 19-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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:

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

Page 2: 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:

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

Page 3: 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:

• 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

Page 4: 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:

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).

Page 5: 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:

Variation

• Don’t use gridx and gridy• REMAINDER: Last component in the row

• RELATIVE: Next to last component in the row

Page 6: 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:

/* * 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); }

Page 7: 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:

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);

Page 8: 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:

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);

Page 9: 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:

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); } } }); }

Page 10: 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:

public static void main(String args[]) { GridBagWindow window = new GridBagWindow(); window.inAnApplet = false;

window.setTitle("GridBagWindow Application"); window.pack(); window.setVisible(true); }}

Page 11: 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:

Example Program

Page 12: 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:

No fill set

Page 13: 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:

No weightx set