Introduction to GUIs - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Introduction to GUIs

Description:

... dialog boxes, scroll bars, buttons, ... typically accessed ... Components include buttons, textfields, scrollbars, windows, pop-up menus, menu items, etc ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 17
Provided by: xxx3108
Category:

less

Transcript and Presenter's Notes

Title: Introduction to GUIs


1
Introduction to GUIs
  • what is a GUI?
  • the basic event-driven model
  • a (very) simple GUI

2
What is a GUI?
  • a user interface is the set of facilities that
    allow the user to interact with the system
  • command line or terminal in Linux
  • command prompt in Windows
  • a graphical user interface is a user interface
    which represents different aspects of the
    interface graphically on the screen
  • windows, dialog boxes, scroll bars, buttons, ...
  • typically accessed with a mouse
  • normally based on some metaphor
  • e.g. a simulated "desktop"

3
Java GUI facilities
  • to write a GUI, we normally need a toolkit a
    set of classes which allow us to specify how
    things are displayed on the screen, and allow
    easy control of io devices
  • Java provides two libraries
  • AWT (abstract windowing toolkit) low level
    functionality, native look-and-feel
  • Swing high-level functionality, pluggable
    look-and-feel, uses facilities of AWT underneath
  • this module introduces Swing (plus necessary
    parts of AWT)

4
Event-driven programming
  • GUIs are event-driven programs external events
    cause methods to be executed
  • Components objects we create which have a screen
    presence (e.g. controls, window panes), and with
    which the user interacts (e.g. buttons).
  • Events when a user interacts with a component,
    java creates an event object (which records the
    source)
  • Listeners Objects we create which respond to
    events. A listener has a handler method, which
    java invokes when the event occurs.

5
Components
  • Components include buttons, textfields,
    scrollbars, windows, pop-up menus, menu items,
    etc
  • Example javax.swing.JButton
  • constructor, e.g.
  • JButton btn new JButton("Click here!")
  • instance methods, e.g.
  • String str btn.getText()
  • btn.setText("Click again!")
  • Containers are components that contain others
  • instance methods allowing you to add (and
    position) components (e.g. button) to the
    container (e.g. window)

6
Event Objects
  • different user actions correspond to different
    event classes (both low level and high level)
  • Example java.awt.event.ActionEvent
  • created by java when (e.g.) a user clicks a
    button
  • instance methods
  • getSource() returns a reference to the source
    of the event (i.e.the button, textfield etc that
    generated it)
  • getActionCommand() returns a String associated
    with the source (e.g. the button's text)
  • constructor methods exist, but it is unlikely
    that we would call them

7
Listener Objects
  • we delegate to Listener objects the task of
    responding to certain events for certain
    components
  • objects contain handler methods that run in
    response we do not normally invoke the handlers
    ourselves Java does it for us when the events
    occur
  • Listeners can be any class of object we like, but
    must implement certain interfaces to guarantee
    inclusion of appropriately named handlers
  • e.g. "listening" for button clicks
  • implements ActionListener
  • must include a definition of actionPerformed
    method

8
Registering Listeners
  • how does Java know which listener object will
    respond to a component's events?
  • we must register one or more listeners with the
    component
  • e.g. if btn is a JButton and lstnr1 and lstnr2
    are listeners (both implements ActionListener)
  • btn.addActionListener(lstnr1)
  • btn.addActionListener(lstnr2)
  • ...
  • btn.removeActionListener(lstnr2)
  • When the user interacts with the button, Java
    invokes the handler methods in each registered
    listener.

9
All this for one button click ...
10
... which requires
  • In the program
  • write a class that creates a window and a
    JButton, adds the JButton to the window, makes
    the window visible
  • write listener classes (implements
    ActionListener) with handler method
    (actionPerformed)
  • create listener objects and register them with
    the JButton
  • At runtime
  • user clicks the button
  • Java creates the ActionEvent object
  • Java adds object to a queue of events
  • Java repeatedly
  • takes object from queue
  • identifies their listeners
  • invokes each listener's handler with event as
    input

11
A first GUI ...
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.
  • public class SimplePanel extends JPanel
  • private JButton btn
  • public SimplePanel()
  • super()
  • btn new JButton("Click me!")
  • add(btn)
  • ActionListener lstnr new
    SimpleButtonListener()
  • btn.addActionListener(lstnr)

12
SimplePanel continued
  • public static void main(String args)
  • JFrame frm new JFrame("A simple GUI")
  • Container contentPane frm.getContentPane()
  • JPanel pnl new SimplePanel()
  • contentPane.add(pnl)
  • frm.setBounds(100, 300, 400, 200)
  • frm.setVisible(true)

13
SimpleButtonListener
  • import java.awt.event
  • public class SimpleButtonListener
  • implements ActionListener
  • private int count
  • public SimpleButtonListener()
  • public void actionPerformed(ActionEvent ae)
  • count
  • System.out.println(count)

14
(No Transcript)
15
Observations
  • no low level details (trapping the mouse click,
    queuing the event object, etc.)
  • a component may have any number of listeners
    attached to it, including zero
  • we did not need to invoke the handler
    actionPerformed Java does it for us
  • Handlers should not involve a lot of computation
  • why?

16
Next lecture ...
  • developing a simple GUI
Write a Comment
User Comments (0)
About PowerShow.com