Readings
Graphical User Interface - GUI
GUI Elements
GUI Elements defined in java.awt
GUI Event-Driven programs
Thread one, sets up communication & links &
instantiates applet 1-3,4-6 call init A-C
java.applet.Applet Label
^ ,String------.
| extends A------->`--------.---'
Mimic / Mimic_Gui ^ TextField
,-------------./ ,----------2--. ,-----------------.
| gui --------|----1--->| | | | |
| | | label--' | |BlackBox contains|
| |----B----|-> quote --3-|->|String, listener |
| | | | | receives |
| | | | | ActionEvents |
| |<---4----|-- applet | | events |
| | ,--|-- listener | | |
|-------------| | |-------------| |-----------------|
| init() | | | init() | |method that calls|
| update_label| 5 | update_label| | |
| | | | (String) | |addActionListener|
| | | | get_quote() | |(listener) |
`----.--------' | `-------------' `---.-------------'
^ V |
| ,------------------. |
`---6----|-listening_applet |<----C-----'
|------------------|
| actionPerformed |
`------------------'
Mimic_Action_Listener
| implements
V interface
java.awt.event.Action_Listener
A&B) applet.add(Component) places
components in the applet container
C) Registering
quote.addActionListener (listener)
associates the listening object
with the quote TextField (a
component) and a new thread is
started. i.e. connect an
ActionEvent with a listener
TextField: Thread 2 waits for and
responds to an ActionEvent
When the quote object detects the
ActionEvent, a newline in the text
field, all listeners registered using
addActionListener are called. i.e.
specific methods are called.
Listener classes must provide specific
methods i.e. in this case
actionPerformed(ActionEvent) from
ActionListener is implemented.
Event-Driven Programming
java.applet.Applet Label
^ ,-String-----.
| extends .------->`--------.---'
Mimic / Mimic_Gui ^ TextField
,-------------./ ,-------------. ,-----------------.
| gui --------|-------->| | | | |
| | | label--' | |BlackBox contains|
| |---------|-> quote ----|->|String, listener |
| | | | | receives |
| | | | | ActionEvents |
| |<--------|-- applet | | events |
| | ,--|-- listener | | |
|-------------| | |-------------| |-----------------|
| init() | | | init() | |method that calls|
| update_label| | | update_label| | |
| | | | (String) | |addActionListener|
| | | | get_quote() | |(listener) |
`----.--------' | `-------------' `---.-------------'
^ V |
| ,------------------. |
`--------|-listening_applet |<----------'
|------------------|
| actionPerformed |
`------------------'
Mimic_Action_Listener
| implements
V interface
java.awt.event.ActionListener
TextField: Thread 2
When a newline character is entered in TextField quote.actionPerformed(ActionEvent) is called beginning the sequence that updates the label component
Class Object calls Method ----- ------ ------------ TextField [quote] actionPerformed(ActionEvent) Mimic_Action_Listener applet update_label() Mimic gui get_quote() Mimic_GUI quote getText() Mimic gui update_label(String) Mimic_GUI label setText(String)
| Event Class | Listener Interface Methods implemented | Association | Generated by Component Class |
|---|---|---|---|
| ActionEvent | interface ActionListener actionPerformed(ActionEvent) | addActionListener | Button List TextField |
| AdjustmentEvent | interface AdjustmentListener adjustmentValueChanged(AdjustedEvent) | addAdjustmentListener | Scrollbar |
Thread two in Event-Driven Programming
output ,-----.
,---->| GUI |
no / `-----'
input,--------/ _____.<-----------.
o---| applet |<--|queue|<----. |
`--------' `-----' | |
| | ,---------. ,--------. |
| `->|component|-->|listener| |
input | `---------' `--------' |
| ,---------. ,--------. |
`---->|component|-->|listener|-'
`---------' `--------'
input Actions include keystrokes,
mouse movements, mouse keystrokes
Components and Containers
Components provide
Containers
Components Hierachies
GUI Components
Labels
TextFields and TextAreas extend
TextComponent
Lists Selectable list labeled with Strings
Buttons
Scrollbars - See Zoom class
Layout Managers
setLayout(new FlowLayout());
| WIDTH=300 HEIGHT=150 | WIDTH=150 HEIGHT=150 |
|---|---|
| 300 x 150 | 200 x 150 |
|---|---|