Object%20Oriented%20Programming%20in%20Java%20(95-707)%20Advanced%20Topics - PowerPoint PPT Presentation

About This Presentation
Title:

Object%20Oriented%20Programming%20in%20Java%20(95-707)%20Advanced%20Topics

Description:

Abstract Windowing Toolkit (AWT) June 1, 2000. Object Oriented Programming in Java (95-707) ... windows events. Event handlers often implement ActionListener. example: ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 24
Provided by: alaink
Category:

less

Transcript and Presenter's Notes

Title: Object%20Oriented%20Programming%20in%20Java%20(95-707)%20Advanced%20Topics


1
Lecture 9Object Oriented Programming in Java
  • Advanced Topics
  • Abstract Windowing Toolkit (AWT)

2
Todays Lecture
  • AWT Fundamentals
  • http//developer.java.sun.com/developer/onlineTrai
    ning/

3
History
  • AWT in JDK 1.0
  • AWT JDK 1.1
  • JFC/Swing

4
Purpose of AWT
  • Build Graphical User Interfaces
  • Keep native platform look
  • Uses native platform API

5
Hierarchy of GUI Components
  • superclass to all classes is Component
  • Component
  • Button
  • Canvas
  • Checkbox
  • Choice
  • Label
  • TextField
  • Container
  • Panel
  • SCrollPan
  • Window
  • Frame
  • List
  • ScrollBar

6
Containers
  • containers group widgets or other containers
  • Containers can embed other containers
  • containers can have a particular layout
  • examples
  • Panel
  • Applet
  • Frame

7
Widgets
  • Button
  • Canvas
  • Label
  • Checkbox
  • Choice
  • List
  • TextField
  • TextArea
  • examples
  • Button b new Button()
  • Label l new Label(OK)

8
Event Handlers
  • Event handlers are registered with Components and
    handle events that are generated
  • mouse clicks
  • keyboard keys
  • windows events
  • Event handlers often implement ActionListener
  • example
  • ActionListener theListener new MyListener()
  • Button b new Button()
  • b.addActionListener(theListener)

9
How to build a GUI
  • Add components to a Container
  • Create Event handler classes
  • Setup event handlers to respond to clicks,...
  • Display the GUI
  • Therefore you always need
  • one or more containers
  • one or more Components
  • one or more Event Handler

10
Extend the Applet class
  • import java.awt.Button
  • import java.applet.Applet
  • public class TheApplet extends Applet
  • public void init()
  • // add() one or more widgets to the applet
    itself
  • // create an event handler for a widget

11
Add a widget to the Applet
  • Example
  • //Create the widget
  • Button aButton new Button(some text)
  • // add(Component c) is a method of Container
  • add(aButton)

12
Create an ActionListener
  • import java.awt.event.ActionListener
  • import java.awt.event.ActionEvent
  • import java.awt.Component
  • public class Beeper implements ActionListener
  • public void actionPerformed(ActionEvent
    event)
  • // get the conponent which initiated the event
  • Component c (Component)event.getSource()
  • // beep
  • c.getToolkit().beep()

13
Create HTML
  • lthtmlgt
  • ltapplet codeAButton.class width100 height100gt
  • lt/appletgt
  • lt/htmlgt

14
View the file
  • Use the appletviewer
  • or use your browser to bring up the html file

15
Standalone GUI
  • Create a class with a main method
  • Create a Frame f new Frame(frame name)
  • Create a Button and add it to the frame
  • Create an ActionListener class (reuse Beeper)
  • Register the event handlers with the button
  • Bring up the frame f.pack() f.show()

16
Enhanced standalone GUI
  • Use Button class setActionCommand(String s)
    method
  • This method gives a name to the click action of
    the button
  • example
  • b.setActionCommand(QUIT)
  • Add a few more buttons to your frame and give
    them various actions

17
Enhanced Action Listener
  • Within the actionPerformed(ActionEvent event)
    method, check for which action command is
    performed
  • 1) Use the ActionEvent method getActionCommand()
  • 2) check to see which event was raised
  • 3) when event QUIT perform a System.exit(0)

18
Layouts
  • Layouts define how the components are laid within
    the containers
  • You have used a default layout all along
  • You can assign a layout explicitly
  • example
  • Frame f new frame(frame name)
  • f.setLayout(new GridLayout(3,2))

19
Types of layouts
  • example
  • GridLayout
  • FlowLayout
  • CardLayout
  • BorderLayout
  • GridBagLayout

20
Nested Classes
  • Nested Classes are a powerful feature of Java
    starting with JDK 1.1
  • with nested classes, Java lets you define a class
    as a member of another class. Such a class is a
    nested class
  • Nested class can see all of the class members of
    its enclosing class, even the private ones

21
Nested Class
  • Class EnclosingClass
  • class InnerClass
  • ...
  • An inner class is a nested class whose instance
    exists within an instance of its enclosing class
    and has direct access to the instance members of
    its enclosing instance

EnclosingClass
InnerClass
22
Use Inner Classes for Listeners
  • Create a class that extends Frame
  • Add Components to the frame in the constructor
  • Define an inner class which extends
    ActionListener
  • Instantiate the inner class within the frames
    constructor the inner class now has access to
    the Frames variables

23
Example of Inner Class
  • import java.awt.
  • import java.awt.event.
  • public class AppFrame extends Frame
  • Button b1, b2
  • public AppFrame(String title)
  • super(title)
  • // instantiate buttons
  • // instantiate inner class
  • ActionListener al new MyHandler()
  • b1.addActionListener(al)
  • class MyHandler extends ActionListener
  • public void actionPerformed(ActionEvent event)
  • // get the conponent which initiated the event
  • Component c (Component)event.getSource()
  • if (cb1) //do this
  • if (cb2) // do that...
Write a Comment
User Comments (0)
About PowerShow.com