Professional Documents
Culture Documents
Lecture 13 - AWT
Lecture 13 - AWT
Programming
Lecture # 13: Abstract Windowing Toolkit (AWT)
Muhammad Imran
(Based on Java, The Complete Reference)
http://www.secscourses.tk
1
Outline
• Abstract Windowing Toolkit (AWT)
• Working with Frames
• Creating an AWT-Based Application
• Control Fundamentals
• Labels
• Buttons
• Checkboxes
2
2
Abstract Windowing Toolkit
• The Abstract Windowing Toolkit (AWT) contains numerous
classes and methods that allow us to create and manage
windows
3
3
Working with Frames
• Frame class encapsulates what is commonly thought
of as a “window”
• It creates a standard window that has a title bar,
borders, and resizing corners
• Two Frame constructors are:
Frame() //creates a standard window that does not
contain a title
Frame(String title) //creates a window with the title
specified by title
• There are several methods that are used when
working with Frame windows 4
4
Working with Frames
8
8
Creating an AWT-Based Application
10
10
Creating an AWT-Based Application
class MyWindowsDemo extends Frame {
public MyWindowsDemo(){
addWindowListener(new MyWindowAdapter());
} Window closing
public static void main(String[] args) { button will work
MyWindowsDemo win = new MyWindowsDemo();
win.setLocation(10,10);
win.setSize(250,200);
win.setTitle("My First GUI Program");
win.setVisible(true);
}
} 11
11
Control Fundamentals
• Controls are components that allow a user to interact with your
application in various ways
• Example: A commonly used control is the push button
• The AWT supports the following types of controls:
Labels
Push buttons
Check boxes
Choice Lists
Lists
Scroll bars
Text editing
• These controls are subclasses of Component
12
12
Control Fundamentals
• To include a control in a window, we must add it to the
window/frame
• To do this, we must first create an instance of the desired
control and then add it to a window by calling add(), which
is defined by Container
• The add() method has several forms. One of these is:
Component add(Component compObj)
13
13
Labels
• A label is an object of type Label, and it contains a string,
which it displays
• Labels are passive controls that do not support any
interaction with the user
• Label defines the following Constructors:
Label()
Label(String str)
Label(String str, int how)
• The first version creates a blank label
• By default, the string is left justified
• how specifies alignment of string, it must be one of the three
constants
Label.LEFT, Label.RIGHT, or Label.CENTER 14
14
Labels
• We can also use the following methods on a label:
void setText(String str)
String getText()
int getAlignment()
15
15
Labels
// Demonstrate Labels
import java.awt.*;
import java.awt.event.*;
class MyLabels extends Frame{
MyLabels() {
addWindowListener(new MyWindowAdapter());
Panel panel = new Panel();
Label one = new Label("One");
Label two = new Label("Two");
Label three = new Label("Three");
panel.add(one);
panel.add(two);
panel.add(three);
add(panel);
}
16
}
16
Labels
public class MyLabelsDemo {
public static void main(String args[]) {
MyLabels win=new MyLabels();
win.setSize(300,200);
win.setTitle("Labels");
win.setVisible(true);
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
17
17
Responding to Controls
• Except for labels, which are passive controls, all control
generate events when they are accessed by the user
• Example:
When the user clicks on a push button, an event is sent that
identifies the push button
• Our program must implement the appropriate interface
and then register an event listener for each control that
we need to monitor
• Once a listener is installed, events are automatically sent
to it
18
18
Buttons
• A push button is a component that contains a label
and that generates an event when it is pressed
• Push buttons are objects of type Button
• Button defines these two constructors:
Button()
Button(String str)
• Setter and getter are:
Void setLabel(String str)
String getLabel()
19
19
Buttons
• Each time a button is pressed, an action event is generated
• This is sent to any listeners that previously registered an
interest in receiving action event notifications from that
component
• Each Listener implements the ActionListener interface
• That interface defines the actionPerformed() method, which
is called when an event occurs
• An ActionEvent object is supplied as the argument to this
method
It contains both a reference to the button that generated the event
and a reference to the string that is the label of the button
Usually, either value may be used to identify the button
20
20
Buttons
• If label of the button is to be used to determine which
button has been pressed
the label is obtained by calling the getActionCommand() method
on the ActionEvent object passed to actionPerformed()
21
21
Buttons
// Demonstrate Buttons
import java.awt.*;
import java.awt.event.*;
class MyButtons extends Frame implements ActionListener{
Panel panel;
Label label;
Button yes, no, maybe;
MyButtons() {
addWindowListener(new MyWindowAdapter());
panel = new Panel();
yes = new Button("Yes");
no = new Button("No");
maybe = new Button("Undecided");
label = new Label("Press a button");
22
22
Buttons
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
panel.add(yes);
panel.add(no);
panel.add(maybe);
panel.add(label);
add(panel);
}
23
23
Buttons
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("Yes"))
{label.setText("You pressed Yes.");}
else if(str.equals("No"))
{label.setText("You pressed No.");}
else
{label.setText("You pressed undecided.");}
repaint();
}
} 24
24
Buttons
public class MyButtonsDemo {
public static void main(String args[]) {
MyButtons win=new MyButtons();
win.setSize(250,150);
win.setTitle("Buttons");
win.setVisible(true);
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
25
25
Checkboxes
27
27
Checkboxes
• To retrieve the current state of a checkbox, call getState()
• To set its state, call setState()
• We can obtain the current label associated with a checkbox
by calling getLabel()
• To set the label, call setLabel()
• The methods are:
boolean getState()
void setState(boolean on)
String getLabel()
void setLabel(String str)
28
28
Checkboxes
• Each time a checkbox is checked or unchecked, an item event
is generated
• This is sent to any listeners that previously registered an
interest in receiving item event notifications from that
component
• Each listener implements the ItemListener interface.
• That interface defines the itemStateChanged() method
• An ItemEvent object is supplied as the argument to this
method
• It contains information about the event (for example,
whether it was a selection or de-selection) 29
29
Checkboxes
// Demonstrate Checkboxes
import java.awt.*;
import java.awt.event.*;
class MyCheckboxes extends Frame implements ItemListener{
Panel panel;
Label label;
String msg="";
Checkbox Win98, winNT, solaris, mac;
MyCheckboxes()
{
addWindowListener(new MyWindowAdapter());
label = new Label();
panel = new Panel();
30
30
Checkboxes
Win98 = new Checkbox("Windows 98", null, true);
winNT = new Checkbox("Windows NT/2000");
solaris = new Checkbox("Solaris");
mac = new Checkbox("MacOS");
Win98.addItemListener(this);
winNT.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);
panel.add(Win98);
panel.add(winNT);
panel.add(solaris);
panel.add(mac);
panel.add(label);
add(panel); 31
} 31
Checkboxes
public void itemStateChanged(ItemEvent ie)
{repaint();}
public void paint(Graphics g)
{
msg = "Current state: ";
msg += " Windows 98: " + Win98.getState();
msg += " Windows NT/2000: " + winNT.getState();
msg += " Solaris: " + solaris.getState();
msg += " MacOS: " + mac.getState();
label.setText(msg);
}
} 32
32
Checkboxes
public class MyCheckboxesDemo {
public static void main(String args[]) {
MyCheckboxes win=new MyCheckboxes();
win.setSize(550,150);
win.setTitle("Checkboxes");
win.setVisible(true);
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
33
33
Recommended Readings
• Chapter # 25: Introducing the AWT: Working with
Windows, Graphics, and Text from Herbert Schildt, Java:
The Complete Reference, J2SETM 9th Edition
34
34