JAVA Programming - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

JAVA Programming

Description:

Standard for Java 2 Platform ... paint, repaint, update. Most GUI components extend the Component class directly or indirectly ... – PowerPoint PPT presentation

Number of Views:204
Avg rating:3.0/5.0
Slides: 19
Provided by: tem3
Category:

less

Transcript and Presenter's Notes

Title: JAVA Programming


1
JAVA Programming
  • GUI Components

2
GUI
  • Graphical user interface
  • User-friendly design of pictorial depictions for
    use of an application
  • Intuitive user interface components
  • Reduces time required to learn an application
  • Provides basic familiarity and ease of use
    without any instruction

3
GUI Components
  • Controls or widgets
  • Object with which the user interacts via the
    mouse, keyboard, voice recognition
  • Examples
  • Labels
  • Text fields
  • Buttons
  • Checkboxes
  • Comboboxes
  • Lists

4
Swing Package
  • javax.swing
  • Include classes for GUI components
  • Standard for Java 2 Platform
  • Can use classes to create a uniform look and feel
    across all platforms
  • Lightweight components (written completely in
    Java and do not take the complex GUI capabilities
    of an o/s into consideration)

5
Awt package
  • java.awt (Abstract Windowing Toolkit)
  • Focuses on the local platforms graphical user
    interface capabilities
  • Applications are displayed differently based on
    the operating system
  • Heavyweight components Rely on the local
    platforms windowing system to determine
    functionality

6
Component Class
  • Defines the common attributes and behaviors of
    all subclasses of Component
  • paint, repaint, update
  • Most GUI components extend the Component class
    directly or indirectly
  • JComponent is the superclass to most Swing
    components
  • Customizable look and feel on different o/s
  • Shortcut keys (mnemonics)
  • Common event-handling capabilities
  • Tool tips
  • Localization for different languages

7
JLabel
  • Provide text instructions or information on a GUI
  • Subclass of JComponent
  • Displays a single line of read-only text, an
    image or both text and an image
  • setToolTipText specifies a tool tip displayed
    when the mouse is positioned over the label
  • setIcon Supports the display of icons on a label
  • getText retrieves current text displayed on a
    label
  • setText sets the text displayed on the label
  • getIcon retrieves the current icon displayed on
    the label
  • setHorizontalTextPosition or setVerticalTextPositi
    on specifies the text position on the label
  • Fig 13.4

8
Event-Handling
  • GUIs generate events when the user of the program
    interacts with the GUI
  • An event is sent to the program
  • GUI Event information is stored in an object of a
    class that extends AWTEvent

9
Event Handling Parts
  • Event Source
  • GUI component that the user interacts
  • Event object
  • Encapsulates information about the event that
    occurred with a reference to the event source and
    specific event information that may be required
    by the listener
  • Event listener
  • Object notified by the event source when an event
    occurs. Object is used to respond to the event

10
Programmers Work
  • Register an event listener for the GUI component
    expected to generate an event
  • Object of a class that implements one or more of
    the event listener interfaces from package
    java.awt.event and javax.swing.event
  • Implement an event handler method that is called
    in response to a particular type of event
  • Thus, when an event occurs, the GUI component
    with which the user interacted notifies its
    registered listeners by calling each listeners
    appropriate event handling method
  • Fig 13.6

11
JTextField
  • Single-line areas that allow the user to enter
    text from the keyboard
  • When user enters data into JTextField and presses
    the Enter key, an action event occurs.
  • Class JTextField extends class JTextComponent
    (javax.swing.text)
  • setEditable whether or not the user can modify
    the text
  • Fig 13.7

12
JButton
  • Component the user clicks to trigger an action
  • Command buttons generate an ActionEvent when the
    user clicks the button with the mouse
  • Inherits from class AbstractButton
  • Text on face of JButton is considered the button
    label
  • Fig 13.10

13
JCheckBox and JRadioButton
  • Subclasses of JToggleButton
  • JRadioButton several buttons of which only one
    can be true at a time (mutually exclusive)
  • Two states selected and not selected
  • JCheckBox several buttons of which many can be
    true at the same time
  • Fig 13.11
  • Fig 13.12

14
JComboBox
  • Drop-down list of items from which the user can
    make a selection
  • Inherits from class JComponent
  • Elements in the list have a numeric index to
    track the ordering of items in the combobox
  • First item is added at index 0 (similar to
    arrays)
  • setMaximumRowCount set the maximum of elements
    displayed when the user clicks the JComboBox
    (determines when to display the scrollbar)
  • Fig 13.13

15
JList
  • Displays a series of items from which user may
    select one or more items
  • Created with class JList which inherits from
    JComponent
  • Single-selection (allow only one item to be
    selected at a time)
  • Multiple-selection (allow any number of items to
    be selected)
  • Do not provide a scrollbar if there are more
    items in the list than the number of visible rows
    (must use JScrollPane)

16
JList Methods and Attributes
  • setVisibleRowCount determine the of items
    visible in the list
  • setSelectionMode
  • SINGLE_SELECTION (only 1 item can be selected at
    a time)
  • SINGLE_INTERVAL_SELECTION (multiple-selection
    where several items in a contiguous range can be
    selected)
  • MULTIPLE_INTERVAL_SELECTION (multiple-selection
    where no restrictions are placed on the
    selections)

17
Mouse Event-Handling
  • Mouse events can be trapped for any GUI component
    that derives from the java.awt.Component
  • MouseListener methods
  • mousePressed mouse button is pressed with the
    mouse cursor on the component
  • mouseClicked mouse button is pressed and
    released on a component
  • mouseReleased mouse button is released after
    being pressed
  • mouseEntered mouse cursor enters the bounds of a
    component

18
MouseListener Methods
  • mouseDragged mouse button is pressed with the
    mouse cursor on the component and the mouse is
    moved
  • mouseMoved mouse is moved with the cursor on the
    component
  • Fig 13.17
Write a Comment
User Comments (0)
About PowerShow.com