GUI building with the AWT - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

GUI building with the AWT

Description:

{ showStatus ('Ouch!'); Inner classes. You can ... { showStatus (ouch); Anonymous inner classes. You can define an anonymous inner class ... { setStatus ('Ouch! ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 25
Provided by: villa81
Category:
Tags: awt | gui | building | ouch

less

Transcript and Presenter's Notes

Title: GUI building with the AWT


1
GUI building with the AWT
  • Lecture 9

2
AWT (Abstract Window Toolkit)
  • Present in all Java implementations
  • Described in (almost) every Java textbook
  • Adequate for many applications
  • Uses the controls defined by your OS
  • Difficult to build an attractive GUI
  • import java.awt.

3
Swing
  • Requires Java 2 or a separate (huge) download
  • More controls, and they are more flexible
  • Gives a choice of look and feel packages
  • Much easier to build an attractive GUI
  • import javax.swing. // Mac or PCimport
    com.sun.java.swing. // UNIX

4
Swing vs. AWT
  • Swing is bigger and slower
  • Swing is more flexible and better looking
  • Swing and AWT are incompatible--you can use
    either, but you cant mix them
  • Learning the AWT is a good start on learning
    Swing
  • AWT Button b new Button (OK)Swing
    Jbutton b new Jbutton(OK)

5
To build a GUI...
  • Make somewhere to display things--a Frame, a
    Window, or an Applet
  • Create controls (buttons, text areas, etc.)
  • Add your controls to your display area
  • Arrange, or lay out, your controls
  • Attach actions to your controls

6
Containers and Components
  • The job of a Container is to hold and display
    Components
  • Some common subclasses of Component are Button,
    Checkbox, Label, Scrollbar, TextField,
    andTextArea
  • A Container is also a Component
  • Some Container subclasses are Panel (and Applet),
    Canvas, Window (and Frame)

7
An Applet is panel is a container
java.lang.Object ----java.awt.Component
----java.awt.Container

----java.awt.Panel
----java.applet.Applet
so you can display things in it
8
To create an Applet
  • class MyApplet extends Applet
  • You can add components to the Applet
  • Its best to add components in init()
  • You can paint directly on the Applet, but
  • its better to paint on a component
  • Do all painting from paint(Graphics g)

9
Some types of components
10
Creating components
  • Label lab new Label (Hi, Dave!")
  • Button but new Button ("Click me!")
  • Checkbox toggle new Checkbox (toggle")
  • TextField tl new TextField (Initial
    text.", 20)
  • Scrollbar scrolly new Scrollbar
    (Scrollbar.HORIZONTAL, initialValue,
    bubbleSize, minValue, maxValue)

11
Adding components to the Applet
  • class MyApplet extends Applet public void
    init ()
  • add (lab) // same as this.add(lab)
  • add (but)
  • add (toggle)
  • add (tl)
  • add (scrolly)

12
Arranging components
  • Every Container has a Layout manager
  • The default Layout for a Panel is FlowLayout
  • An Applet is a Panel
  • The default Layout for a Applet is FlowLayout
  • You could set it explicitly with setLayout
    (new FlowLayout())

13
FlowLayout
  • Components are added left-to-right
  • If no room, a new row is started
  • Components are made as small as possible
  • Use add (component)

14
BorderLayout
  • At most five components can be added
  • If you want more components, add a Panel, then
    add components to it.
  • setLayout (new BorderLayout())

add (BorderLayout.NORTH, new Button(NORTH))
15
BorderLayout with five Buttons
public void init() setLayout (new
BorderLayout ()) add (BorderLayout.NORTH, new
Button ("NORTH")) add (BorderLayout.SOUTH,
new Button ("SOUTH")) add (BorderLayout.EAST,
new Button ("EAST")) add (BorderLayout.WEST,
new Button ("WEST")) add (BorderLayout.CENTER,
new Button ("CENTER"))
16
Using a Panel
  • panel p new Panel()
  • add (BorderLayout.SOUTH, p)
  • Button b1 new Button (Button 1)
  • p.add (b1)
  • p.add (new Button (Button 2))

17
Making components active
  • Most components already appear to do
    something--buttons click, text appears
  • To associate an action with a component, attach a
    listener to it
  • Components send events, listeners listen for
    events
  • Different components may send different events,
    and require different listeners

18
Listeners
  • Listeners are interfaces, not classes
  • class MyButtonListener implements ActionListener
  • An interface is a group of methods that must be
    supplied
  • When you say implements, you are promising to
    supply those methods

19
Writing a Listener
  • For Buttons, you need an ActionListener
  • b1.addActionListener (new MyButtonListener
    ())
  • An ActionListener must have an actionPerformed()
    method
  • public void actionPerformed(ActionEvent e)

20
MyButtonListener
Public void init () ...
b1.addActionListener (new MyButtonListener ())
class MyButtonListener implements ActionListener
public void actionPerformed (ActionEvent e)
showStatus ("Ouch!")
21
Inner classes
  • You can define a class inside a class
  • The inner class can access the outer classs
    variables

Class MyApplet extends Applet String ouch
Ouch! class MyButtonListener implements
ActionListener public void
actionPerformed (ActionEvent e) showStatus
(ouch)
22
Anonymous inner classes
  • You can define an anonymous inner class
  • addActionListener (new MyListener ())
  • addActionListener (newActionListener ()
    public void actionPerformed(ActionEvent e)
    setStatus (Ouch!) )
  • But anonymous inner classes dont have access to
    the variables of the outer class

23
Listeners for TextFields
  • An ActionListener listens for hitting the return
    key
  • An ActionListener demandspublic void
    actionPerformed (ActionEvent e)
  • use getText() to get the text
  • A TextListener listens for any and all keys
  • A TextListener demandspublic void
    textValueChanged(TextEvent e)

24
The End
Write a Comment
User Comments (0)
About PowerShow.com