EventDriven Programming and GUIs - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

EventDriven Programming and GUIs

Description:

Use various methods to manipulate window. Second way ... Inner area of GUI window (below title bar, inside border) To access content pane: ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 23
Provided by: briand70
Category:

less

Transcript and Presenter's Notes

Title: EventDriven Programming and GUIs


1
TOPIC 13
Window Interfaces Using Swing Objects
  • Event-Driven Programming and GUIs
  • Swing Basics and a Simple Demo Program
  • Layout Managers
  • Buttons and Action Listeners
  • Container Classes
  • Text I/O for GUIs
  • Inner Classes

2
Event-Driven Programming
  • Programs with GUIs often use Event-Driven
    Programming
  • Program waits for events to occur and then
    responds
  • Examples of events
  • Clicking a mouse button
  • Dragging the mouse
  • Pressing a key on the keyboard
  • Firing an event when an object generates an
    event
  • Listener object that waits for events to occur
  • Event handler method that responds to an event

3
A New Approach to Programming
  • Previous Style of Programming
  • List of instructions performed in order
  • Next thing to happen is next thing in list
  • Program performed by one agentthe computer
  • Event-Driven Style of Programming
  • Objects that can fire events and objects that
    react to events (listener objects)
  • Next thing to happen depends on next event
  • Program is interaction between user and computer

4
Java GUI Components
5
Graphical User Interface (GUI) Components
  • GUI components
  • Windows
  • Labels
  • Text areas
  • Buttons
  • GUI components placed in content pane
  • Added to content pane of window
  • Not added to window itself

6
Two Ways to Create a Window
  • First way
  • Declare object of type Frame
  • Instantiate object
  • Use various methods to manipulate window
  • Second way
  • Create class containing application program by
    extending definition of class Frame
  • Utilizes mechanism of inheritance

7
class JFrame
  • Provides various methods to control window
    attributes
  • JFrame(String title)
  • constructor for creating a JFrame with a title
  • Container getContentPane()
  • returns the content pane of the JFrame, which has
    the add method for adding components
  • void setBackgroundColor(Color c)
  • void setForegroundColor(Color c)
  • void setSize(int width, int height)
  • void setVisible(boolean b)
  • void setTitle (String title)

8
Methods Provided by the class JFrame
9
Methods Provided by the class JFrame
10
(No Transcript)
11
Content Pane
  • Inner area of GUI window (below title bar, inside
    border)
  • To access content pane
  • Declare reference variable of type Container
  • Use method getContentPane of class Frame

12
Container class METHODS
13
class JLabel
  • Labels objects of particular class type
  • Label attributes (i.e. every label has a )
  • title
  • width
  • height
  • To create a label
  • Instantiate object of type JLabel
  • Must be added to the pane
  • Modify attributes to control display of labels

14
Methods Provided by the class JLabel
15
Methods Provided by the class JLabel
16
  • //Java program to create a window and place four
  • // labels.
  • import javax.swing.
  • import java.awt.
  • public class RectangleProgramTwo extends JFrame
  • private static final int WIDTH 400
  • private static final int HEIGHT 300
  • private JLabel lengthL, widthL,
  • areaL, perimeterL

17
  • public RectangleProgramTwo()
  • setTitle("Area and Perimeter of a
    Rectangle")
  • lengthL new JLabel("Enter the length,
    SwingConstants.RIGHT)
  • widthL new JLabel("Enter the width ",
  • SwingConstants.RIGHT)
  • areaL new JLabel("Area ",
  • SwingConstants.RIGHT)
  • perimeterL new JLabel("Perimeter ",
  • SwingConstants.RIGHT)
  • Container pane getContentPane()
  • pane.setLayout(new GridLayout(4,1))

18
  • pane.add(lengthL)
  • pane.add(widthL)
  • pane.add(areaL)
  • pane.add(perimeterL)
  • setSize(WIDTH, HEIGHT)
  • setVisible(true)
  • setDefaultCloseOperation(EXIT_ON_CLOSE)
  • public static void main(String args)
  • RectangleProgramTwo rectProg
  • new RectangleProgramTwo()

19
(No Transcript)
20
class JTextField
  • Text fields objects belonging to class
    JTextField
  • To create text field
  • Declare reference variable of type JTextField
  • Instantiate object

21
Methods Provided by the class JTextField
22
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com