cop 4331 – ood&p

16
COP 4331 – OOD&P Lecture 5

Upload: yuri-lancaster

Post on 01-Jan-2016

34 views

Category:

Documents


4 download

DESCRIPTION

COP 4331 – OOD&P. Lecture 5. TYJ Day 11 – Layout. A Layout Manager determines placement of components Flow Layout Grid Layout Border Layout Card Layout Grid Bag Layout. Flow Layout. class FlowLayout Data flows across rows depending on what will fit - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COP 4331 – OOD&P

COP 4331 – OOD&P

Lecture 5

Page 2: COP 4331 – OOD&P

TYJ Day 11 – Layout

• A Layout Manager determines placement of components– Flow Layout– Grid Layout– Border Layout– Card Layout– Grid Bag Layout

Page 3: COP 4331 – OOD&P

Flow Layout

• class FlowLayout– Data flows across rows depending on what

will fit– FlowLayout.LEFT, CENTER, RIGHT as

parameter to constructor sets alignment– See Alphabet.java

Page 4: COP 4331 – OOD&P

GridLayout

• Grid Layout divides panel into rectangular grid and places components in each cell– Uniform spacing

• class GridLayout(r, c)

Page 5: COP 4331 – OOD&P

Border Layout

• Border Layout creates 5 positions, north and south span area, west, center, and east fill space between

• class BorderLayout– Component positioning specified with

parameters “North”, “South”, etc.– See Border.java

Page 6: COP 4331 – OOD&P

Mixing Layouts

• A panel can be entered as a pane to produce a hierarchical layout– Add items to the subpanel and then add it to

the next level at the desired place

Page 7: COP 4331 – OOD&P

Card Layout

• Each component acts like a card in a stack of cards– First component added is on top, etc.– Can navigate up and down stack

• class CardLayout

Page 8: COP 4331 – OOD&P

Grid Bag Layout

• Gives precise and detailed control of placement– Size and layout determined by

GridBagConstraints object for each component

• Specify position and cells spanned, relative size, and other characteristics

• class GridBagLayout

Page 9: COP 4331 – OOD&P

Grid Bag Layout.2

• Plan component layout and draw grid that bounds all component areas, with some components spanning several cells

• class GridBagConstraints– gridx, gridy– gridwitth, gridheight– weightx, weighty

• Utility function buildConstraints() from text• See NamePass.java

Page 10: COP 4331 – OOD&P

TYJ Day 12 – User Input

• Event Listeners are used to detect user interaction with a frame– ActionListener - KeyListener– AdjustmentListener - MouseListener– FocusListener -MouseMotionListener– ItemListener - WindowListener

• Each type listens for a particular class of events

Page 11: COP 4331 – OOD&P

Setting Up Listeners

• One must add listeners to components– addActionListener()

• JButton, JCheckBox, JComboBox, JtextField, JRadioButton, JMenuItem

– addAdjustmentListener()• JScrollBar

– addFocusListener()• All

– addItemListener()• JButton, JCheckBox, JComboBox, JRadioButton

Page 12: COP 4331 – OOD&P

Setting Up Listeners

– addKeyListener()• All

– addMouseListener()• All

– addMouseMotionListener()• All

– addWindowListener()• JWindow, JFrame

Page 13: COP 4331 – OOD&P

Event-Handling Methods

• Each Listener interface requires certain event-handling methods– Action Listener users

• actionPerformed(ActionEvent evt)• The event object contains information about the

particular action

– See TitleChange.java

Page 14: COP 4331 – OOD&P

Event-Handling Methods.2

• Adjustment Events– adjustmentValueChanged(evt)

• Determine type of change and current value

– See WellAdjusted.java

• Focus Events– focusGained() and focusLost()

• Item Events– itemStateChanged()– See SelectItem.java

Page 15: COP 4331 – OOD&P

Event-Handling Methods.3

• Key Events– keyPressed(evt), keyReleased(evt),

keyTyped(evt)

• Mouse Events– mouseClicked(evt), Entered, Exited, Pressed,

Released

• Mouse Motion Events– mouseDragged(evt), mouseMoved(evt)

Page 16: COP 4331 – OOD&P

Event-Handling Methods.4

• Window Events– windowActivated(WindowEvent), Closed,

Closing, Deactivated, Deiconified, Iconified, Opened

• Complete sample application– See SwingColorTest.java