Labels, Buttons and Event-Handling by sleepnow

VIEWS: 47 PAGES: 20

									Labels, Buttons and
  Event-Handling
Labels
   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
    alignment.
   Vertical alignment is through the
    setVerticalAlignment() method.
   The contents of a label can be changed with the
    setText() method.
Icons
   Icons can be easily added to labels or other
    controls either to brand, dress up, or aid
    accessibility.
   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
  JLabel("hello",icon,SwingConstants.RIGHT);
Buttons
   Simple buttons are used to start operations.
    They are created with the JButton()
    constructor.
   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




 Source: java.sun.com
Example
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
    conditions.
Events
   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
    mechanism:
       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
    AWTEvent.
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.
Event-Handling
   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.
Example
public class Beeper ... implements ActionListener {
  ...
  //where initialization occurs:
  button.addActionListener(this);
  ...
  public void actionPerformed(ActionEvent e) { ...
  //Make a beep sound
  ... }
}
Example
Example (cont.)
Acknowledgement
   Some of the slides are adapted from, GUI Swing
    Widgets, John W. M. Russell,
    http://home.cogeco.ca/~ve3ll/

								
To top