Labels, Buttons and Event-Handling by sleepnow


									Labels, Buttons and
   Labels are non-interactive text objects most
    commonly used as prompts.
   They are created using the JLabel() constructor
    with the required text as the first parameter.
   Another parameter can be added using a
    SwingConstant value to set horizontal
   Vertical alignment is through the
    setVerticalAlignment() method.
   The contents of a label can be changed with the
    setText() method.
   Icons can be easily added to labels or other
    controls either to brand, dress up, or aid
   Icons are constructed from the ImageIcon class
    and then added as a parameter to the label (or
    other) control.
   An extra parameter can be used to control the
    position of the text relative to the icon. It must
    use one of the SwingConstants values.
Label Example
ImageIcon icon = new ImageIcon("smile.gif");
JLabel label = new
   Simple buttons are used to start operations.
    They are created with the JButton()
   They can be deactivated with the
    setEnabled(false) method and tested with the
    isEnabled() method.
   One useful button method is setMnemonic(char)
    which allows a hot key to be associated with
    the button.
   Simple buttons require an ActionEvent event
    listener that reacts to the button click.
JButton Constructor

Basic Event Listeners
   GUIs are event-based.
   That is they respond to buttons, keyboard input
    or mouse activities.
   Java uses event listeners to monitor activity on
    specified objects and react to specific
   Some common interactions are:
       Moving the mouse
       Clicking the mouse
       Clicking a button
       Typing in a text field
       Selecting an item from a menu
       Closing a window
       …
Event-Handling Model
   When is user interaction occurs, an event is
    sent to the program.
   The event types from package java.awt.event
    are used with both AWT and Swing components.
   New Swing-component event types are defined
    in a packaged called javax.swing.event.
Event-Handling Model (cont.)
   There are three parts to the event-handling
       The event source
       The event object
       The event listener
The Event Source
   The event source is a particular GUI component
    with which the user interacts.
   GUI Components such as: JLabel, JButton, etc.
The Event Object
   The event object encapsulates information
    about the event.
   This information includes a reference to the
    event source and any specific information that
    may be required by the event listener to handle
    the event.
   This an object of a class that extends
The Event Listener
   The event listener is an object that is notified
    by the event source when an event occurs.
   The event listener receives an event when it is
    notified of an event, then uses the object to
    respond the event.
   The event source is required to provide
    methods that enable listeners to be registered
    and unregistered.
   The first step in adding a an event handler to a GUI
    control is to import awt.event.*
   This package contains all of the event classes.
   Next add the phrase implements ActionListener to the
    class header.
   Register event listeners for each GUI control using the
    addActionListener(this) method.
   The reserved word this indicates that the required (by
    implements ActionListener) handler method called
    actionPerformed() will be included in the current class.
public class Beeper ... implements ActionListener {
  //where initialization occurs:
  public void actionPerformed(ActionEvent e) { ...
  //Make a beep sound
  ... }
Example (cont.)
   Some of the slides are adapted from, GUI Swing
    Widgets, John W. M. Russell,

To top