11
CPSC 233 Tutorial Xin Apr 6, 2011

CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

Embed Size (px)

Citation preview

Page 1: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

CPSC 233 TutorialXin Apr 6, 2011

Page 2: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

Reading files An example available on my website

pages.cpsc.ucalgary.ca/~liuxin

Page 3: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

Layout manager describe how components are arranged

Add a layout manager to Jframe eg., framWindow.setLayout(new

BorderLayout());

Designate positions in the add() method eg., add(button, BorderLayout.SOUTH);

Java build-in layout mangers BorderLayout, GridLayout, GridBagLayout, etc.

Page 4: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

BorderLayout Place components into five regions

NORTH, SOUTH, EAST, WEST, CENTER

add(button, BorderLayout.SOUTH)

BorderLayout.NORTH

BorderLayout.WEST

BorderLayout.EAST

BorderLayout.SOUTH

Page 5: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

FlowLayout Arranges components one after the other

from left to right in the order in which components are added

Page 6: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

GridLayout Arrages components in a 2D grid.

(0, 0) (0, 1) (0, 2) (0, 3)

(1, 0) (1, 1) (1, 2) (1, 3)

(2, 0) (2, 1) (2, 2) (2, 3)

Page 7: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

Grid Bag Layout A flexible and powerful layout manager.

places components in a 2D grid

Allow specified components to span multiple rows or columns

Uses components’ preferred sizes to determine how big the cells should be

A good online tutorial: http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Page 8: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

An example from Sun http://download.oracle.com/javase/tutorial/ui

swing/layout/gridbag.html

Page 9: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

Work with GridBagLayout

Specifying constraints with GridBagConstraints

frameWindow.setLayout (new GridBagLayout());GridBagConstraints c = new GridBagConstraints();//For each component to be added to this container://...Create the component...//...Set instance variables in the GridBagConstraints instance...pane.add(theComponent, c);

Page 10: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

GridBagConstraints Attributes to set up

gridx, gridy gridwidth, gridheight fill

NONE, HORIZONTAL, VERTICAL, or BOTH ipadx, ipady

internal padding insects

external padding anchor

CENTER, PAGE_START, PAGE_END, … weightx, weighty

determine how to distribute space among columns and rows

Page 11: CPSC 233 Tutorial Xin Apr 6, 2011. Reading files An example available on my website pages.cpsc.ucalgary.ca/~liuxin

James’ Example For your in-class practice