110 likes | 332 Views
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 ());
E N D
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(newBorderLayout()); • Designate positions in the add() method • eg., add(button, BorderLayout.SOUTH); • Java build-in layout mangers • BorderLayout, GridLayout, GridBagLayout, etc.
BorderLayout • Place components into five regions • NORTH, SOUTH, EAST, WEST, CENTER • add(button, BorderLayout.SOUTH)
FlowLayout • Arranges components one after the other • from left to right • in the order in which components are added
GridLayout • Arrages components in a 2D grid.
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
An example from Sun • http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
Work with GridBagLayout • Specifying constraints with GridBagConstraints frameWindow.setLayout (new GridBagLayout()); GridBagConstraintsc = 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);
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
James’ Example • For your in-class practice