Module 4 AWT
Module 4 AWT
ABSTRACT WINDOWING
TOOLKIT (AWT)
By: Dr. Dolly Sharma
Dept of CSE, ASET, AUUP
Department of Computer
Objectives Science and Engineering
➢AWT – The Abstract Window Toolkit provides basic graphics tools (tools for
putting information on the screen)
➢Swing – A much better set of graphics tools.
➢Container – a graphic element that can hold other graphic elements (and is itself
a Component)
➢Component – a graphic element (such as a Button or a TextArea) provided by a
graphics toolkit
➢listener – A piece of code that is activated when a particular kind of event occurs
➢layout manager – An object whose job it is to arrange Components in a
Container.
3
Department of Computer
Abstract Windowing Toolkit Science and Engineering
Component
Button Panel
List
Checkbox
Choice
Label
TextComponent TextField
TextArea
Department of Computer
How to build a GUI... Science and Engineering
6
Department of Computer
Component Science and Engineering
➢ Component is the superclass of most of the displayable classes defined within the
AWT. Note: it is abstract.
➢ MenuComponent is another class which is similar to Component except it is the
superclass for all GUI items which can be displayed within a drop-down menu.
➢ The Component class defines data and methods which are relevant to all
Components:
setBounds
setSize
setLocation
setFont
setEnabled
setVisible
setForeground -- colour
setBackground -- colour
Some types of Components Department of Computer
Science and Engineering
Button Checkbox
Label
Scrollbar
Choice
TextField List
TextArea
Button
Checkbox CheckboxGroup
8
Department of Computer
Science and Engineering
import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visible
}
public static void main(String args[]){
First f=new First();
}}
Department of Computer
Creating Components Science and Engineering
10
Department of Computer
Container Science and Engineering
➢ When writing a GUI application, the GUI portion can become quite complex.
➢ To manage the complexity, GUIs are broken down into groups of components. Each
group generally provides a unit of functionality.
➢ A Panel is a rectangular Container whose sole purpose is to hold and manage
components within a GUI.
Panel aPanel = new Panel();
aPanel.add(new Button("Ok"));
aPanel.add(new Button("Cancel"));
aFrame.add(aPanel);
Department of Computer
Buttons Science and Engineering
aPanel.add(okButton));
aPanel.add(cancelButton));
okButton.addActionListener(controller2);
cancelButton.addActionListener(controller1);
Department of Computer
Labels Science and Engineering
aPanel.add(aLabel);
Department of Computer
List Science and Engineering
// 5 rows, 80 columns
TextArea fullAddressTextArea = new TextArea(5, 80);
[…]
➢ Since the Component class defines the setSize() and setLocation() methods, all
Components can be sized and positioned with those methods.
➢ Problem: the parameters provided to those methods are defined in terms of pixels.
Pixel sizes may be different (depending on the platform) so the use of those methods
tends to produce GUIs which will not display properly on all platforms.
➢ There are several different LayoutManagers, each of which sizes and positions its
Components based on an algorithm:
▪ FlowLayout
▪ BorderLayout
▪ GridLayout
➢ For Windows and Frames, the default LayoutManager is BorderLayout. For Panels, the
default LayoutManager is FlowLayout.
Department of Computer
Flow Layout Science and Engineering
➢ The algorithm used by the FlowLayout is to lay out Components like words on a page:
Left to right, top to bottom.
➢ It fits as many Components into a given row before moving to the next row.
➢ The BorderLayout Manager breaks the Container up into 5 regions (North, South,
East, West, and Center).
➢ When Components are added, their region is also specified:
North
South
Department of Computer
Grid Layout Science and Engineering
➢ The GridLayout class divides the region into a grid of equally sized rows and columns.
➢ Components are added left-to-right, top-to-bottom.
➢ The number of rows and columns is specified in the constructor for the LayoutManager.
aPanel.add(new Button("Ok"));
aPanel.add(new Button("Add"));
aPanel.add(new Button("Delete"));
aPanel.add(new Button("Cancel"));
Department of Computer
What if I don’t want a LayoutManager? Science and Engineering
Ch. VIII - 27
Department of Computer
Graphics Science and Engineering
➢ It is possible to draw lines and various shapes within a Panel under the AWT.
➢ Each Component contains a Graphics object which defines a Graphics Context which
can be obtained by a call to getGraphics().
➢ Common methods used in Graphics include:
drawLine
drawOval
drawPolygon fillOval
drawPolyLine fillPolygon
drawRect fillRect
drawRoundRect fillRoundRect
drawString setColor
draw3DRect setFont
fill3DRect setPaintMode
fillArc drawImage
Department of Computer
Science and Engineering
import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by default not visible
}
public static void main(String args[]){
First f=new First();
}}
Department of Computer
Science and Engineering
import java.awt.*;
class First2{
First2(){
Frame f=new Frame();
Button b=new Button("click me");
b.setBounds(30,50,80,30);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true); }
public static void main(String args[]){
First2 f=new First2(); }}