AWT, Swing and GUIs in Java - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

AWT, Swing and GUIs in Java

Description:

Traditional or 'Stop and Wait' Programming. Loops (or lack of) in ... Waits till some event occurs. Then can determine what occurred (e.g., which button pushed) ... – PowerPoint PPT presentation

Number of Views:1311
Avg rating:3.0/5.0
Slides: 17
Provided by: dlb7
Category:
Tags: awt | guis | java | swing | waits

less

Transcript and Presenter's Notes

Title: AWT, Swing and GUIs in Java


1
AWT, Swing and GUIs in Java
2
Overview
  • Applets and Applications
  • Class Hierarchy of AWT and Swing
  • JPanels and Contexts
  • Assembling the GUI
  • Event Generators and Event Listeners
  • Required Methods

3
Event Driven Programming
  • Event Driven Programming
  • Enter data, then generate action event
  • User options available
  • Traditional or Stop and Wait Programming
  • Loops (or lack of) in Event Driven Programming

4
Applets and Applications
  • Applets run in a browser (i.e., IE or Netscape)
  • Applet code can also be run in an appletviewer
  • Applets can not read or write local files, check
    for the existence of files, execute programs on
    the local machine, etc.
  • These restrictions are for security reasons
  • Applications can provide the same interface but
    are not subject to the above security restrictions

5
JApplet Methods
  • init()
  • Describes the GUI
  • You write this
  • start() //can be ignored or removed
  • stop() //ditto
  • destroy() //ditto

6
Class Hierarchy of AWT and Swing
  • See Koffman and Wolz page 443
  • Component //abstract
  • Container //abstract
  • Panel
  • Applet
  • JApplet
  • Jcomponent //abstract

7
Class Hierarchy of AWT and Swing
  • JComponent (an abstract class)
  • Jlabel //for label
  • JOptionPane
  • Jpanel //holds other components
  • AbstractButton
  • Jbutton //a button to push
  • JToggleButton
  • JCheckBox //a box to put a check mark in
    either true or false
  • JRadioButton //used for selecting one from a
    group of options
  • JTextComponent
  • JTextField //for input
  • JTextArea //for output

8
Jpanels and Contexts
9
Assembling the GUI
  • JLabel
  • JTextField
  • JTextArea
  • JButton
  • JPanel
  • Example put a JLabel and a JTextField onto a
    JPanel, and then put the JPanel onto the Applet

10
Event Generators and Event Listeners
  • Need to import java.awt.event.
  • Constitutes the Java "event model"
  • GUI components "generate" events
  • Could be a button click
  • Could be a CR or check box click
  • Could be a mouse movement, etc.
  • Applet or JFrame serves as "listener"
  • Waits till some event occurs
  • Then can determine what occurred (e.g., which
    button pushed)
  • Can then determine what to do

11
Event Generators and Event Listeners
  • init() constructs GUI
  • Within init() registers Applet or JFrame as a
    listener to a particular GUI object
  • myDeleteButton.addActionListener(this)
  • myChangeButton.addActionListener(this)
  • When either myDeleteButton or myChangeButton is
    pushed, the method actionPerformed() is
    automatically executed

12
Writing the actionPerformed() method
  • Determining which button was pressed
  • Public void actionPerformed(Action Event aE)
  • Object sourceOfEvent aE.getSource()
  • If (sourceOfEvent myDeleteButton)
  • code for what you want to do when delete
    pressed
  • Else if (sourceOfEvent myChangeButton)
  • code for what you want to do when change
    pressed

13
Writing the actionPerformed() method
  • Determining values showing in various GUI objects
  • Public void actionPerformed(Action Event aE)
  • String priceStr priceInputJTextField.getText()
    //get value
  • double price Double.parseDouble(priceStr)
    //convert to double

14
Writing the actionPerformed() method
  • Displaying values back to GUI objects
  • Public void actionPerformed(Action Event aE)
  • String priceStr priceInputJTextField.getText()
    //get value
  • double price Double.parseDouble(priceStr)
    //convert to double
  • DecimalFormat df new DecimalFormat(".00")
  • priceInputJTextField.setText(df.format(price))
    //formats output
  • outputJTextArea.setText("num tickets is "
    numTickets)

15
Methods You Will Find in Your GUI
  • init() //creates the layout
  • actionPerformed() //responds to events
  • itemStateChanged() //if check boxes used
  • Look at samples on the course web site and in
    Chapter 7

16
Converting an Applet to an Application
  • Extends JFrame and not JApplet
  • Still implements ActionListener
  • init() contents go into constructor
  • Need a main method to construct an instance of
    the class itself
  • See page 464 for sample code
Write a Comment
User Comments (0)
About PowerShow.com