CIS525 - Lecture#17 - November 1, 2000
CIS 525 11/1/00
Layout managers:
1) flow layout
2) border layout
3) grid layout
4) card layout
5) grid bag layout
see FlowTest.java page 594
'bad way', page 597
Panel p = new Panel();
FlowLayout layout = (FlowLayout)p.getLayout();
layout.setHgap(10);
'good way'
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5));
Border Layout - Page 600
- Divides screen into regions
North, South - regions
full width, minimum height
East, West - regions
full height (minus North, South heights) minimum widths
Center - region
whatever is left
see BorderTest.java on page 601
Grid Layout - Page 603:
- Divides window into equal sized rectangles, based on number of rows and columns
GridTest.java page 603
If you specify 0 columns
- in Java 1.02 Java tries to even things out
1 row, 0 columns has the same effect
it is better to specify rows and columns to avoid discrepencies
Card Layout - Page 607:
like a 'stack of cards' where only top is displayed
see CardDemo.java page 608
1) create card layout instance
2) you need to associate a variable with each component added
3) to display cards, display by name
CardPanel.java page 611
GridBag Layout; page 615
most flexible, hardest to use
=> windows are 'chopped' into 'cells', each component has a starting cell and an ending cell
1) set layout and save a reference to it
GridBagLayout layout = new GridBagLayour();
setLayout(layout);
2) allocate grid bag constraints object
GridBagConstraints constraints = new GridBagConstraints();
3) set constraints
object.gridx = ____
object.gridy = ____
4) report constraints to layout
layout.setConstraints (component1, object)
5) add component to window
add(component1);
REPEAT STEPS 3 THROUGH 5 STEPS FOR EACH COMPONENT
see GridBagTest.java page 618
it is possible to turn off layout manager and 'hand code' the placement, probably on the same order of difficulty as OpenGL
NullTest.java page 623
guidlines for effective use of layout managers
1) use nested containers with their own layout managers, see ButtonCol.java page 625
2) turn off layout manager for some containers/components
3) use custom layout manager, page 628
4) adjust empty space around components,
a) change space allocated by the layout manager
b) override the insets in the container
c) use a canvas as a spacer
end of Chapter 12
Chapter 13, GUI controls inside of Java
- don't process mouse and keyboard events like windows
1) Java will define higher level evnts based on when a control is activated
2) paint is NOT used to display the controls, OS has freedom to preserve its own look/feel
handling GUI events:
=>action events are generated by:
- buttons
- list boxes
- menu items
- text fields
handled by: process actionEvent via add ActionListener
=>item selection events are generated by
- check boxes
- check box menu item
- choice, (pull down menu)
- list
handled by: process itemEvent via add ItemListener
=>text fields and text areas generate both keyboard and text events
handled by textEvent, keyEvent via add textListener or keyListeer
=>adjustment events generated by scroll bars
handled by: process AdjustmentEvents via add adjustmentListener
two basic approaches to handling action events
1) let each component process its own
2) handling events in a central place, like enclosing window
page 644, ActionExample1.java
method 1) (Java 1.02) override action in component
(method2)strategy - override action from enclosing window, using interrupt handlers
method 2) (Java 1.1)
strategy - a) create an object implementing a listener interface
b) define a method actionPerformed, (ActionEvent is arg)
c) associate Listener using add ActionListener
1) (Java 1.1) see SetSizeButton.java