Software Construction Lecture 11 GUI Programming in Java - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Software Construction Lecture 11 GUI Programming in Java

Description:

Software Construction Lecture 11 GUI Programming in Java * GUI Programming Concepts conventional programming: sequence of operations is determined by the program what ... – PowerPoint PPT presentation

Number of Views:159
Avg rating:3.0/5.0
Slides: 21
Provided by: Peter1672
Category:

less

Transcript and Presenter's Notes

Title: Software Construction Lecture 11 GUI Programming in Java


1
Software ConstructionLecture 11GUI Programming
in Java
2
GUI Programming Concepts
  • conventional programming
  • sequence of operations is determined by the
    program
  • what you want to happen, happens when you want it
  • event-driven programming
  • sequence of operations is determined by the
    users interaction with the applications
    interface
  • anything that can happen, happens at any time

3
GUI Design Concepts
  • Principles of good GUI Design
  • IBM's Design concepts
  • Saul Greenberg's HCI pages
  • Tim's HCI notes

4
GUI Programming Concepts in Java
  • Java GUI has components
  • Windows GUI has controls
  • Unix GUI has widgets
  • examples labels, buttons, check boxes, radio
    buttons, text input boxes, pull down lists
  • Swing components JLabel, JButton, JCheckBox,
    JRadioButton, JTextField, JTextArea, JComboBox

5
Java GUI history the AWT
  • AWT(JDK 1.0, 1.1) Abstract Window Toolkit
  • package java.awt, java.awt.event
  • heavyweight components using native GUI system
    elements
  • used for applets until most browsers supported
    JRE 1.2

6
Swing in Java
  • Swing(Java 2, JDK 1.2)
  • lightweight components that do not rely on the
    native GUI or OS
  • look and feel of Swing components
  • are identical on different platforms
  • can be customized
  • Swing inherits from AWT
  • AWT still used for events, layouts

7
Swing Components in Java
  • advanced GUI support. e.g. drag-and-drop
  • package names javax.swing, javax.swing.event
  • components inherit from JComponent
  • components are added to a top-level container
    JFrame, JDialog, or JApplet.

8
running a Swing application
  • java -Dswing.aatexttrue MySwingClass
  • the option sets the system property
    "swing.aatext" to "true" to enable anti-aliasing
    for every JComponent
  • javaw runs a GUI without the console window

9
Basic GUI Programming Stepsin Java
  • declare a container and components
  • add components to one or more containers using a
    layout manager
  • register event listener(s) with the components
  • create event listener method(s)

10
Basic GUI Programming Concepts in Java
  • Example JFrameDemo.java, JFrameDemoTM.java
  • container a screen window/applet window/panel
    that groups and arranges GUI components
  • GUI component an object with visual
    representation
  • Swing containers JFrame, JApplet, JPanel
  • AWT containers Frame, Applet, Panel

11
GUI Programming The Java Approach
  • event-driven programming
  • a piece of code (i.e. event handler) is attached
    to a GUI component
  • an event handler is called when an event (e.g. a
    mouse click) is activated / fired
  • The Delegation Event Model in Java
  • processing of an event is delegated to an object
    (the listener) in the program

12
Event-driven Programming in Java
  • event source a GUI component that generates /
    fires an event
  • event a user interaction (e.g. a click on
    the button)
  • event listener an object that has encapsulated
    event handlers to react to an event

13
Event Handling in Java
  • the delegation event model
  • - a GUI element delegates the processing of
    an event to another piece of code (i.e. an event
    handler)
  • - the event source generates/fires an event and
    sends it to event listeners

14
Event Handling in Java
  • the delegation event model
  • - event listeners must be registered with an
    event source in order to receive
    notification
  • Example JButtonDemo.java,
    JButtonDemo2.java

15
Event Handling in Java
  • registration of an event listener
  • - write a class that implements an ltevent
    namegtListener interface - create an
    instance of that class (i.e. an event
    listener) - register the listener with a
    GUI component addltevent namegtListener
    ( ltan event listenergt )

16
Event Handling in Java
  • a listener interface has a list of standard event
    handlers (i.e. methods)
  • API documentation - java.awt.event -
    event classes - listener interfaces -
    adapter classes

17
Event Handling in Java
  • different ways of coding the event listeners
  • - use of another top-level class JButtonDemo
  • - anonymous inner classes JButtonDemo2,
    JFrameDemo, JFrameDemo2
  • - use of an adapter class JFrameDemo3
  • JFrameDemoTM2.java uses named inner classes and
    shows how to consolidate window closing from two
    different events.

18
Handling of Mouse Events
  • MouseListener mousePressed( ), mouseClicked(
    ) mouseReleased( ), mouseEntered(
    ) mouseExited( )
  • MouseMotionListener mouseDragged( ),
    mouseMoved( )
  • MouseAdapter, MouseMotionAdapter
  • Example MouseDemo.java

19
Multiple Event Sources
  • Example ActionApplet2.java .html
  • one listener for multiple event sources
  • GUI and graphics programming
  • http//cs.senecac.on.ca/pliu/appletdemo.html

20
Review
  • GUI
  • Java GUI
  • Components
  • Event Listeners
  • Delegates
  • Event Sources
Write a Comment
User Comments (0)
About PowerShow.com