Chapter 10 Writing Graphical User Interfaces - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Chapter 10 Writing Graphical User Interfaces

Description:

Understanding Java's GUI Classes ... Handling Java Events. Users interact with GUI by entering data and clicking on components ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 37
Provided by: JohnFron8
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
Implementing Interfaces
22
Extending Adapter Class
  • Second way of dealing with events is to use an
    adapter class
  • Supplied class that implements a listener
    interface, and then overrides all of the
    interface methods with null methods
  • Most override methods you want to respond to
  • Need to create two classes one that extends
    Frame, and another that extends WindowAdapter

23
Extending Adapter Class
24
Creating Inner Classes
10
  • Third way of dealing with events is to use an
    anonymous inner class
  • Anonymous inner class is an inner class without a
    name
  • Used to simplify code needed to handle events
  • See pp. 336

25
Creating Inner Classes
26
Creating Inner Classes
27
Creating Inner Classes
10
  • When you compile an inner class the compiler
    produces two class files
  • If your outer class is named Outer and your inner
    class is named Inner a filed called
    OuterInner.class is created
  • If the inner class is anonymous, the class file
    for the inner class has the same name as the
    outer class with a number beginning with 1
    concatenated after the

28
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

29
Converting an Application to an Applet
30
Using Swing Classes
  • Swing classes are newer improved versions of the
    original AWT classes
  • Want to use for there improved appearance and
    capability

31
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
  • Compare Figure 10-17 vs. Figure 10-14

32
Converting AWT GUI to Swing
10
AWT
33
Adding Drop-down Menus
10
  • Swing drop down menus consist of three classes
  • JMenuBar
  • JMenu
  • JMenuItem
  • See Figure 10-20 on page 352

34
Adding Drop-down Menus
10
35
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

36
Converting a Swing Application to an Applet
Write a Comment
User Comments (0)
About PowerShow.com