Even-driven program using AWT - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

Even-driven program using AWT

Description:

GUI's procedure of computing (input, computing, and output are all in windows) ... setTitle('Name Tester'); setLayout(new GridLayout(2, 1)); setSize(WIDTH, HEIGHT) ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 58
Provided by: JunN
Category:
Tags: awt | driven | even | program | tester | using

less

Transcript and Presenter's Notes

Title: Even-driven program using AWT


1
Even-driven program using AWT
  • Graphic User Interface (GUI)
  • GUI's procedure of computing (input, computing,
    and output are all in windows)
  • Window portion of screen has edge and title,
    interactive with users
  • Window has components such as menu and buttons

2
Even-driven program using AWT
  • Event in GUI object representing actions
    (clicking or dragging mouse, selecting menu,
    pressing keys, scrolling bar, )
  • Event object object which can firing an event
  • Listener object object which is associated with
    the event object. It has methods to perform
    actions in responding to event object. The
    methods handling events called event handlers

3
Event object
Firing event
GUI object
Listener objects
Handling event
Source object (register listeners)
4
import java.awt. import java.awt.event. publi
c class FirstWindow extends Frame public
static final int WIDTH 300 public static
final int HEIGHT 200 /
A simple
demonstration of a window constructed with AWT.

/ public static void main(String args)
FirstWindow myWindow new
FirstWindow() myWindow.setSize (WIDTH,
HEIGHT)
import Java predefined classes

Method in Super class Frame to set up window size
5
WindowDestroyer listener new
WindowDestroyer() myWindow.addWindowListe
ner(listener) myWindow.setVisible(true)
public void paint(Graphics g)
//automatically called by AWT
g.drawString("Please, don't click that button!",
75, 100)
Register a listener
paint() is invoked automatically by AWT Object g
is used to perform a graphics in the area of the
window
6
Even-driven program using AWT
  • Window coordinate system

Unit (pixels)
(0, 0)
x
( 75, 100)

y
Note Size of window depend on the screen
resolution
7
import java.awt. import java.awt.event. /

If you register an object of this class as a
listener to any object of the class Frame, then
if the user clicks the close-window button in
the Frame, the object of this class will end the
program and close the Frame. (It will also
respond to other "closing window" events, but
you need not worry about that now.)

/ public class WindowDestroyer extends
WindowAdapter public void
windowClosing(WindowEvent e)
System.exit(0)
8
Even-driven program using AWT
  • Alternative registering a listener

import java.awt. import java.awt.event. publi
c class SecondWindow extends Frame public
static final int WIDTH 550 public static
final int HEIGHT 400 /
Creates
and displays a window of the class SecondWindow.

/
9
public static void main(String args)
SecondWindow myWindow new
SecondWindow() myWindow.setVisible(true)
public SecondWindow()
super() addWindowListener(new
WindowDestroyer()) setTitle("Second
Window") setSize(WIDTH, HEIGHT)
setBackground(Color.blue)
Use new as part of method argument
Use constant of Color class
Omit "this" in all the statements
10
public void paint(Graphics g)
g.drawString("Coming to you in living color!",
10, 100)
Other methods in Frame class
void show() void setForeground(Color c) void
repaint()
11
Even-driven program using AWT
  • Components, Containers, and Layout managers
    construct every GUI system
  • Container class has add() method which is used to
    add objects of Component classes
  • Example

12
import java.awt. import java.awt.event. /

Simple demonstration of putting buttons in a
container class
/ public class ButtonDemo
extends Frame
implements ActionListener private String
theText "Watch me!" public static
final int WIDTH 300 public static final
int HEIGHT 200
13
/
Creates and displays a window of the class
ButtonDemo.
/ public static void
main(String args) ButtonDemo
buttonGui new ButtonDemo()
buttonGui.setVisible(true) public
ButtonDemo() setSize(WIDTH,
HEIGHT) addWindowListener(new
WindowDestroyer()) setTitle("Button
Demonstration") setBackground(Color.blue)

14
setLayout(new FlowLayout())
Button stopButton new Button("Red")
stopButton.addActionListener(this)
add(stopButton) Button goButton new
Button("Green") goButton.addActionListene
r(this) add(goButton)
public void paint(Graphics g)
g.drawString(theText, 75, 100)
Register listeners
15
public void actionPerformed(ActionEvent e)
if ( e.getActionCommand().equals("Red")
) setBackground(Color.red)
theText "STOP"
else if ( e.getActionCommand().equals("Green") )
setBackground(Color.green)
theText "GO" else
theText "Error in button
interface." repaint() //force
color and text change
16
Hierarchy of AWT Classes
Object
Component
MenuComponent
Container
Button
MenuBar
MenuItem
Label
Window
TextComponent
Menu
Panel
Frame
TextArea
TextField
17
Even-driven program using AWT
  • Abstract class class marked by modifier
    "abstract"
  • Abstract class is more generalized class designed
    to be inherited to sub classes.
  • Abstract class doesn't have instance or object
  • Abstract methods (marked by "abstract")in
    abstract class can bed used without the instance
    of the class

18
Even-driven program using AWT
  • Every container has a method called
    add(component), which is used to add component to
    the container.
  • Components added to the container are managed by
    Layout Manager (objects).
  • The commonly used Layout Managers are
    FlowLayout, BorderLayout, and GridLayout

19
Even-driven program using AWT
  • Interface several constant and methods without
    implementation are put together.
  • very similar to abstract class
  • ActionListener (previous example) and
    WindowListener (next example)

20
import java.awt. import java.awt.event. publi
c class SecondWindowRedone extends Frame
implements
WindowListener public static final int
WIDTH 550 public static final int HEIGHT
400 public static void main(String args)
SecondWindowRedone myWindow
new SecondWindowRedone()
myWindow.setVisible(true)
21
public SecondWindowRedone()
super() addWindowListener(this)
setTitle("Second Window") setSize(WIDTH,
HEIGHT) setBackground(Color.blue)
public void paint(Graphics g)
g.drawString("Coming to you in living color!",
10, 100) public void
windowOpened(WindowEvent e)
22
public void windowClosing(WindowEvent e)
this.dispose()
System.exit(0) public void
windowClosed(WindowEvent e) public
void windowIconified(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowActivated(WindowEvent
e) public void windowDeactivated(Window
Event e)
23
Even-driven program using AWT
  • Panels and Text Components
  • Panel is one of the simplest container classes
  • Once you have a Panel, you can add many
    components in it.
  • You can use different layout manager to layout
    the components inside the Panel.
  • A Panel object can be add to Frame object

24
import java.awt. import java.awt.event. /

Simple demonstration of putting buttons in a
panel. If you do not see the colored part of
the window with text in it, use your mouse to
increase the size of the window and it will
appear.
/ public class PanelDemo extends
Frame implements ActionListener public
static final int WIDTH 300 public static
final int HEIGHT 200 public static void
main(String args) PanelDemo
guiWithPanel new PanelDemo()
guiWithPanel.setVisible(true)
25
public PanelDemo()
setTitle("Panel Demonstration")
setSize(WIDTH, HEIGHT)
setBackground(Color.blue)
addWindowListener(new WindowDestroyer())
Panel buttonPanel new Panel()
buttonPanel.setBackground(Color.white)
buttonPanel.setLayout(new FlowLayout())
Button stopButton new Button("Red")
stopButton.setBackground(Color.red)
stopButton.addActionListener(this)
buttonPanel.add(stopButton)
26
Button goButton new Button("Green")
goButton.setBackground(Color.green)
goButton.addActionListener(this)
buttonPanel.add(goButton)
setLayout(new BorderLayout())
add(buttonPanel, "South") public
void paint(Graphics g)
g.drawString(theText, 75, 100)
27
public void actionPerformed(ActionEvent
e) if (e.getActionCommand().equals("
Red")) setBackground(Color.r
ed) theText "STOP"
else if (e.getActionCommand().
equals("Green"))
setBackground(Color.green)
theText "GO" else
theText "Error in button interface."
repaint() //force color and text change
28
private String theText "Watch me!"
29
Even-driven program using AWT
  • Text Areas and Text Fields
  • Let user to input text to your GUI window
  • Text Area can be used to store memos.
  • TextArea object can be added to a container
    object such as object of Panel.
  • Example

30
import java.awt. import java.awt.event. publ
ic class TextAreaDemo extends Frame
implements ActionListener public static
final int WIDTH 400 public static final
int HEIGHT 300 public TextAreaDemo()
setTitle("Memo Saver")
setLayout(new BorderLayout())
setSize(WIDTH, HEIGHT)
addWindowListener(new WindowDestroyer())

31
Panel buttonPanel new Panel()
buttonPanel.setBackground(Color.white)
buttonPanel.setLayout(new FlowLayout())
Button memo1Button new Button("Save Memo
1") memo1Button.addActionListener(this)
buttonPanel.add(memo1Button)
Button memo2Button new Button("Save Memo 2")
memo2Button.addActionListener(this)
buttonPanel.add(memo2Button) Button
clearButton new Button("Clear")
clearButton.addActionListener(this)
buttonPanel.add(clearButton) Button
get1Button new Button("Get Memo 1")
get1Button.addActionListener(this)
buttonPanel.add(get1Button) Button
get2Button new Button("Get Memo 2")
get2Button.addActionListener(this)
buttonPanel.add(get2Button)
32
add(buttonPanel, "South")
textPanel new Panel()
textPanel.setBackground(Color.blue)
theText new TextArea(10, 40)
theText.setBackground(Color.white)
textPanel.add(theText) add(textPanel,
"Center") public void
actionPerformed(ActionEvent e)
String actionCommand e.getActionCommand()
if (actionCommand.equals("Save Memo 1"))
memo1 theText.getText() else if
(actionCommand.equals("Save Memo 2"))
memo2 theText.getText() else if
(actionCommand.equals("Clear"))
theText.setText("")
33
else if (actionCommand.equals("Get Memo
1")) theText.setText(memo1)
else if (actionCommand.equals("Get Memo 2"))
theText.setText(memo2) else
theText.setText("Error in memo
interface") textPanel.repaint()//Shows
changes in textPanel public static void
main(String args) TextAreaDemo
guiMemo new TextAreaDemo()
guiMemo.setVisible(true) private Panel
textPanel private TextArea theText
private String memo1 "No Memo 1." private
String memo2 "No Memo 2."
34
Even-driven program using AWT
  • TextAreas and TextFields are very similar to each
    other. Usually TextArea can be used as a window
    for inputting data in GUI
  • The different between TextArea and TextField is
    the TextArea can set multiple line, while
    TextField has only one line.
  • Label can be used to lable the TextArea or
    TextField
  • Example

35
import java.awt. import java.awt.event. /

Class to demonstrate placing a label on a text
field.
/ public class LabelDemo extends Frame
implements ActionListener public static
final int WIDTH 300 public static final
int HEIGHT 200 public LabelDemo()
setTitle("Name Tester")
setLayout(new GridLayout(2, 1))
setSize(WIDTH, HEIGHT)
addWindowListener(new WindowDestroyer())
36
Panel namePanel new Panel()
namePanel.setLayout(new BorderLayout())
name new TextField(20)
namePanel.add(name, "South") Label
nameLabel new Label("Enter your name here")
namePanel.add(nameLabel, "Center")
add(namePanel) Panel
buttonPanel new Panel()
buttonPanel.setBackground(Color.gray)
Button b new
Button("Test") b.addActionListener(this)
buttonPanel.add(b) b new
Button("Clear") b.addActionListener(this
) buttonPanel.add(b)
37
add(buttonPanel) public void
actionPerformed(ActionEvent e) if
(e.getActionCommand().equals("Test"))
name.setText("A very good name!") else
if (e.getActionCommand().equals("Clear"))
name.setText("") else
name.setText("Error in window interface.")
repaint()
38
public static void main(String
args) LabelDemo w new
LabelDemo() w.setVisible(true)
private TextField name
39
Even-driven program using AWT
  • When you input number using GUI, your input is a
    text, and you have to convert from text to
    number.
  • It is required to convert from a string to a
    number, either integer or floating point numbers

int n Integer.valueOf(inputOutFiled.getText().tri
m)).inValue() double x Double.valueOf(inputOutF
iled.getText().trim)).doubleValue()
40
Even-driven program using AWT
  • Or you can use design a special method to convert
  • If you want to output a number to a GUI, you have
    to convert a number to a string
  • Example

inputOutField.setText(Integer.toString(sum)) inpu
tOutField.setText(Double.toString(total)
41
import java.awt. import java.awt.event. /
GUI for
totaling a series of numbers.
/ public class Adder extends
Frame implements ActionListener public
static final int WIDTH 300 public static
final int HEIGHT 100 public static void
main(String args) Adder guiAdder
new Adder() guiAdder.setVisible(true)

42
public Adder()
setTitle("Adding Machine")
addWindowListener(new WindowDestroyer())
setSize(WIDTH, HEIGHT) setLayout(new
BorderLayout()) Panel buttonPanel new
Panel() buttonPanel.setBackground(Color.g
ray) buttonPanel.setLayout(new
FlowLayout()) Button addButton new
Button("Add In") addButton.addActionList
ener(this) buttonPanel.add(addButton)
Button resetButton new Button("Reset")
resetButton.addActionListener(this)
buttonPanel.add(resetButton)
add(buttonPanel, "South")
43
Panel textPanel new Panel()
textPanel.setBackground(Color.blue)
inputOutputField new TextField("Numbers go
here.", 30) inputOutputField.setBackgroun
d(Color.white) textPanel.add(inputOutputF
ield) add(textPanel, "Center")
public void actionPerformed(ActionEvent e)
if (e.getActionCommand().equals("Add
In")) sum sum
stringToDouble(inputOutputField.getText())
inputOutputField.setText(Double.toStri
ng(sum))
44
else if (e.getActionCommand().equals("Reset"))
sum 0
inputOutputField.setText("0.0")
else inputOutputField.setText("Error
in adder code.") repaint()
private static double stringToDouble(String
stringObject) return
Double.valueOf(stringObject.trim()).doubleValue()
private TextField inputOutputField
private double sum 0
45
Even-driven program using AWT
  • How to add menu in your GUI
  • There is a class called MenuBar.
  • You can add Menu object to MenuaBar using add(0
    method.
  • You can add MenuIterm to Menu object using add()
    method
  • Example

46
import java.awt. import java.awt.event. /

GUI for saving and retrieving memos using a
menu.
/ public class MenuDemo extends Frame
implements ActionListener public
static final int WIDTH 500 public static
final int HEIGHT 500 public MenuDemo()
setTitle("Memo Saver")
setSize(WIDTH, HEIGHT)
setBackground(Color.blue)
addWindowListener(new WindowDestroyer())
47
Menu memoMenu new Menu("Memos")
MenuItem m m new MenuItem("Save Memo
1") m.addActionListener(this)
memoMenu.add(m) m new MenuItem("Save
Memo 2") m.addActionListener(this)
memoMenu.add(m) m new
MenuItem("Get Memo 1")
m.addActionListener(this)
memoMenu.add(m) m new MenuItem("Get
Memo 2") m.addActionListener(this)
memoMenu.add(m)
48
m new MenuItem("Clear")
m.addActionListener(this)
memoMenu.add(m) m new
MenuItem("Exit") m.addActionListener(thi
s) memoMenu.add(m)
MenuBar mBar new MenuBar()
mBar.add(memoMenu) setMenuBar(mBar)
Panel textPanel new Panel()
textPanel.setBackground(Color.blue)
theText new TextArea(20, 50)
theText.setBackground(Color.white)
49
textPanel.add(theText) setLayout(new
FlowLayout()) add(textPanel)
public void actionPerformed(ActionEvent e)
String actionCommand
e.getActionCommand() if
(actionCommand.equals("Save Memo 1"))
memo1 theText.getText() else if
(actionCommand.equals("Save Memo 2"))
memo2 theText.getText() else if
(actionCommand.equals("Clear"))
theText.setText("") else if
(actionCommand.equals("Get Memo 1"))
theText.setText(memo1) else if
(actionCommand.equals("Get Memo 2"))
50
theText.setText(memo2) else if
(actionCommand.equals("Exit"))
System.exit(0) else
theText.setText("Error in memo interface.")
repaint() public static
void main(String args) MenuDemo
menuGUI new MenuDemo()
menuGUI.setVisible(true) private
TextArea theText private String memo1 "No
Memo 1." private String memo2 "No Memo
2."
51
Even-driven program using AWT
  • Inner classes are the classed defined inside
    other classes.
  • Helping class moving window destroyer class to
    inside the class
  • Example

52
import java.awt. import java.awt.event. //Th
is class uses an inner class. /
GUI that demonstrates
the use of buttons.
/ public class InnerDemo extends
Frame implements
ActionListener public static final int
WIDTH 300 public static final int HEIGHT
200 /
Creates and displays a window
of the class InnerDemo.
/
53
public static void main(String args)
InnerDemo buttonGui new InnerDemo()
buttonGui.setVisible(true)
public InnerDemo() setSize(WIDTH,
HEIGHT) addWindowListener(new
InnerDestroyer()) setTitle("Button
Demonstration") setBackground(Color.blue)
setLayout(new FlowLayout())
Button stopButton new Button("Red")
stopButton.addActionListener(this)
add(stopButton)
54
Button goButton new Button("Green")
goButton.addActionListener(this)
add(goButton) public void
paint(Graphics g)
g.drawString(theText, 75, 100) public
void actionPerformed(ActionEvent e)
if (e.getActionCommand().equals("Red"))
setBackground(Color.red)
theText "STOP"
55
else if (e.getActionCommand().equals("Gr
een")) setBackground(Color.gr
een) theText "GO"
else theText "Error in button
interface." repaint() //force color and
text change private class
InnerDestroyer extends WindowAdapter
public void windowClosing(WindowEvent e)
if (! (theText.equals("GO")))
theText "Color must
be green before exiting."
56
repaint()
else System.exit(0)
private String theText
"Color must be green before exiting."
57
Even-driven program using AWT
  • Homework Assignment
  • Read chapter 7 of Walter Savitch's textbook
  • Do self-test questions through 1-28
  • Do Exercises 1 to 2, on Page 401
  • Due date
Write a Comment
User Comments (0)
About PowerShow.com