COP 4331 – OOD&P
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
COP 4331 – OOD&P
E N D
Presentation Transcript
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 • FlowLayout.LEFT, CENTER, RIGHT as parameter to constructor sets alignment • See Alphabet.java
GridLayout • Grid Layout divides panel into rectangular grid and places components in each cell • Uniform spacing • class GridLayout(r, c)
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
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
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
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
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
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
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
Setting Up Listeners • addKeyListener() • All • addMouseListener() • All • addMouseMotionListener() • All • addWindowListener() • JWindow, JFrame
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
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
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)
Event-Handling Methods.4 • Window Events • windowActivated(WindowEvent), Closed, Closing, Deactivated, Deiconified, Iconified, Opened • Complete sample application • See SwingColorTest.java