Chapter 10 Writing Graphical User Interfaces - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

Chapter 10 Writing Graphical User Interfaces

Description:

Understanding Java's GUI Classes. 10. Chapter 10 - Writing ... Java's GUI ... Handling Java Events. Users interact with GUI by entering data and ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 53
Provided by: johnf190
Category:

less

Transcript and Presenter's Notes

Title: Chapter 10 Writing Graphical User Interfaces


1
Chapter 10Writing Graphical User Interfaces
2
Understanding Javas GUI Classes
10
  • Java has two sets of GUI classes
  • Original GUI class AWT Abstract Window
    Toolkit
  • Located in java.awt package
  • Updated GUI class Swing
  • Located in javax.swing
  • Both contain classes to create controls
  • Windows
  • Push buttons
  • Text fields
  • Menus

3
Understanding Javas GUI Classes
10
4
Understanding Javas GUI Classes
5
Understanding Javas GUI Classes
10
  • Style and appearance of GUI components are called
    their look and feel
  • AWT classes adopt look and feel of the local
    platform
  • With Swing you have the option of using local
    platform look and feel or a standard look and
    feel (called metal)
  • Both AWT and Swing take advantage of inheritance

6
Understanding Javas GUI Classes
10
7
Using AWT Classes
  • Class definition of a GUI generally follows the
    structure you used for a problem domain class
  • One difference is that you create an instance of
    GUI and make it visible
  • Write statements to instantiate GUI components
  • Then write statements to add them to the window

8
Creating a Window with a Button
  • Frame inheritance hierarchy is Windows,
    Container, and Component
  • Your GUI inherits from all three super classes
  • Need to import the AWT package import
    java.awt.
  • Class is a subclass of Frame must extend the
    Frame classpublic class AWTFrameWithButton
    extends Frame

9
Creating a Window with a Button
10
  • Constructor must accomplish five tasks
  • Create and instance of Button
  • Add the button to the frame
  • Establish the frame size
  • Place a title on the frame
  • Make the frame visible

10
Creating a Window with a Button
11
Using Layout Managers
  • AWT includes several classes called layout
    managers
  • Used to determine how components are positioned
    on containers such as frames and panels
  • Most frequently used layout managers
  • FlowLayout
  • BorderLayout
  • GridLayout

12
Using Layout Managers
10
  • To use
  • Instantiate the manager you want to use
  • Invoke the setLayout method, passing a reference
    to the layout manager you want to use
  • Components placed on a container using the
    BorderLayout manager expand to fill the space
    available

13
Using Layout Managers
14
Using Layout Managers
  • FlowLayout manager places components on the frame
    as you add them, left to right
  • GridLayout manager arranges the container into a
    grid consisting of rows and columns
  • When you instantiate you need to specify the
    number of rows and columns you want to have

15
Using Layout Managers
16
Handling Java Events
  • Users interact with GUI by entering data and
    clicking on components
  • An event is a signal that the user has taken some
    action
  • Event is also an instance of a class such as
    MouseEvent, WindowEvent, etc.

17
Handling Java Events
10
18
Handling Java Events
  • Event listener registers with event source by
    invoking a method in the event source
  • Button events are called action events
    registration method is named addActionListener
  • Next step is to write a method to handle the
    event for buttons this method is actionPerformed

19
Handling Java Events
  • There are three methods for handling events
  • Implementing interfaces
  • Extending Adapter classes
  • Creating Inner classes

20
Implementing Interfaces
  • When you implement an interface you must override
    all of its methods
  • Implement the appropriate interface for the event
  • Write methods to override the interfaces methods
    and deal with the events
  • Interface for buttons is ActionListener
  • Need to import interface import
    java.awt.event.

21
Event Handling using listener interfaces (i)
22
Event Handling using listener interfaces (ii)
23
Implementing Interfaces
24
Extending Adapter Class
  • Supplied adapter class that implements a listener
    interface, and then overrides all of the
    interface methods with null methods
  • Must override methods you want to respond to
  • Need to create two classes one that extends
    Frame, and another that extends WindowAdapter

25
Extending Adapter Class
26
Event handling using Adapter Class (i)
27
Event handling using Adapter Class (ii)
28
Creating Inner Classes
10
  • Third way of dealing with events is to use an
    anonymous inner class
  • Used to simplify code needed to handle events

29
Creating Inner Classes
30
Creating Inner Classes
31
Creating Inner Classes
10
  • When you compile an inner class the compiler
    produces two class files
  • The class file for the anonymous inner class has
    the same name as the outer class with a number
    beginning with 1 concatenated after the , for
    example, ATWFrame1.java

32
Event handling using an Anonymous Inner class (i)
33
Event handling using an Anonymous Inner class (ii)
34
Multiple AWT components (i)
35
Multiple AWT components (ii)
36
Converting an Application to an Applet
  • An applet is a class you write that extends the
    Applet class
  • Runs under the control of a web browser
  • Can convert AWTFrameAndComponent to an applet by
  • Delete the main method
  • Delete all references to close button
  • Delete the anonymous inner class
  • Delete the shutDown method
  • Delete the setVisible, setSize, and setTitle
    methods

37
AWT Applet (i)
38
AWT Applet (ii)
39
Converting an Application to an Applet
40
Using Swing Classes
  • Swing classes are newer improved versions of the
    original AWT classes
  • Want to use for there improved appearance and
    capability

41
Converting AWT GUI to Swing
10
  • Swing components reside in javax.swing import
    javax.swing.
  • Change class names of Button, Label, TextField,
    and Panel to JButton, JLabel, JTextField, and
    JPanel
  • Use JFrame instead of Frame

42
Swing Components (i)
43
Swing Components (ii)
44
Converting AWT GUI to Swing
10
45
Adding Drop-down Menus
10
  • Swing drop down menus consist of three classes
  • JMenuBar
  • JMenu
  • JMenuItem

46
Adding Drop-down Menus
10
47
Swing Menus (i)
48
Swing Menus (ii)
49
Converting a Swing Application to an Applet
  • Steps for conversion include
  • Extend JApplet instead of JFrame
  • Delete the main method
  • Delete all references to close menu item
  • Delete the anonymous inner class
  • Delete the shutDown method
  • Delete the statements setVisible, setSize, and
    setTitle

50
Swing Applet (i)
51
Swing Applet (ii)
52
Converting a Swing Application to an Applet
Write a Comment
User Comments (0)
About PowerShow.com