GUIs 2
GUIs 2
EXTENDED JAVA
GUIs Part 2: Overview
THIS WEEK’S LECTURE
• Review of last week
• JCheckBoxes component
• JList Component
• Working with JPanels
• Drawing primitives
LAST WEEK
-Basics of Swing as an API for creating desktop GUIs in JAVA
- Potted history of Java on the desktop
-JFrame
-Layout Managers
-JLabel
-JButton
-ActionListener
LAST WEEK: LAYOUT MANAGERS
LAST WEEK
LAST WEEK
FRAME
LAST WEEK
FRAME
LABELS
FRAME
LAST WEEK
FRAME
LABELS
FRAME
BUTTONS
LAST WEEK
FRAME
LABELS
FRAME
BUTTONS
TEXT EDIT
THIS WEEK:
THIS WEEK:
COMPLEX
LAYOUT WITH MULTIPLE
PANELS
THIS WEEK:
JLIST
COMPLEX
LAYOUT WITH MULTIPLE
PANELS
THIS WEEK:
JLIST
COMPLEX
LAYOUT WITH MULTIPLE
PANELS
JCHECKBOX JCHECKBOX
THIS WEEK:
JLIST
COMPLEX
LAYOUT WITH MULTIPLE
PANELS
CUSTOM
DRAWING
JCHECKBOX JCHECKBOX
DR EDWARD ANSTEAD
EXTENDED JAVA
GUIs Part 2:
JCheckBox
JCHECKBOX
2 states: checked or unchecked
JCHECKBOX
- A di erent type of button
- Inherits from AbstractButton (as does JButton)
- and JToggleButton
- De ned Using:
JCheckBox(String str)
fi
ff
JCHECKBOX: ITEMLISTENER
- When an item event occurs (box is checked or unchecked) an item event is generated
- Item events are handled by the classes that implement the ItemListener interface
- Interface speci es only one method itemStateChanged(ItemEvent ie)
fi
JCHECKBOX: ITEMLISTENER
- When an item event occurs (box is checked or unchecked) an item event is generated
- Item events are handled by the classes that implement the ItemListener interface
- Interface speci es only one method itemStateChanged(ItemEvent ie)
EXTENDED JAVA
GUIs Part 2:
JList
JLIST
JLIST
- Component for laying out textual list of items.
- One or more items can be selected
- Often wrapped inside a JScrollPane, to allow for lists longer than the selection.
- Can display an array of Objects or a special collection (GenericListModel which
implements ListModel.)
- JList is a Generic class (see next Lecture) de ned as follows:
EXTENDED JAVA
GUIs Part 2:
JPanel
JPANEL
- JPanels are container objects.
- We can use them in much the same way as frames
- Add components to them
- Set a layoutManager
- Using Arrangements of panels allows us to create complex interfaces
EXTENDED JAVA
GUIs Part 2:
Vector Drawing
DRAWING SHAPES
- We can do basic vector drawing using Java AWT.
- Create a new class that extends JPanel.
- Overriding the PaintComponent method we can use the Graphics content to draw
- primitive shapes (rectangles, ovals, arcs and lines)
- Filled and hollow shapes
- Draw raster images from les
- set colours and fonts.
- Not really designed for animation and complex graphics.
fi
DRAWING SHAPES EXAMPLE
BRINGING IT ALL TOGETHER:
PHONE BOOK EXAMPLE