Professional Documents
Culture Documents
Programming CE-103
• For example an AWT GUI having a button would have a different look and feel across platforms
like windows, Mac OS & Unix,
• this is because these platforms have different look and feel for their native buttons and AWT
directly calls their native subroutine that creates the button.
• In simple, an application build on AWT would look like a windows application when it runs on
Windows, but the same application would look like a Mac application when runs on Mac OS.
Java AWT Hierarchy
Panel
Dialog
All methods of Components class are available in all inherited class
java.lang.Object
| +--java.awt.Component
| +--java.awt.Button
Types of containers:
• As explained above, a container is a place wherein we add components like text field,
button, checkbox etc. There are four types of containers available in AWT: Window,
Frame, Dialog and Panel. As shown in the hierarchy diagram above, Frame and Dialog
are subclasses of Window class.
Window: An instance of the Window class has no border and no title
Dialog: Dialog class has border and title. An instance of the Dialog class cannot
exist without an associated instance of the Frame class.
Panel: Panel does not contain title bar, menu bar or border. It is a generic container
for holding components. An instance of the Panel class provides a container to
which to add components.
Frame: A frame has title, border and menu bars. It can contain several components
like buttons, text fields, scrollbars etc. This is most widely used container while
developing an application in AWT.
• In JAVA there are different Layouts on which you can design your frames and
place components inside the frames according to the GUI presentation you
want. Following are the Layouts which you can use while working on AWT
Package
• FlowLayout
• GridBagLayout
• BorderLayout
• GridLayout
label
F1owL ayou I
One TNo
4GE ST.ART
GP US Bag Lazy Ou £
AGE END
show(); }}
import java.awt.*;
class awtLayouts extends
Frame{ Button b1,b2,b3; TextField t1;
Label l1; public awtLayouts (String
title)
{
super(title);
//setLayout(new FlowLayout());
//setLayout(new BorderLayout());
//setLayout(new GridLayout());
//setLayout(new GridBagLayout());
t1 = new TextField();
l1 = new Label(" label ");
b1 = new Button(" Save ");
b2 = new Button(" Canel ");
b3 = new Button(" Delete ");
add(t1); add(l1); add(b1); add(b2);
add(b3);
class showawtLayout{
public static void main(String arg[]){
I t emL1st ener
value = -15 degree
L
KeyL1st ener
I
HouseL1st ener
E
houseHot ionL1st ener
H
E Text L1st ener
R
V1ndoldL1st ener
L L
Example a
w
i t
m .
p e
o v
r e
t n
t
j s
a .
v *
a ;
. class area
a extends Frame
w implements
t ActionListener
. {
* Label l1,l2;
;
i
m
p
o
r
t
j
a
v
a
.
{
if(e.getSource()==
b2)
public void { System.exit(0)
actionPerformed(Ac ; }
tionEvent e)
TextField t1,t2;
B t Button("Exit");
u ) setLayout(new
t { FlowLayout());
t super(t);
o l1=new
n Label("E
b nter
1 Radius")
, ;
b l2=new
2 Label("
; Area
p is");
u t1=new
b TextFiel
l d(4);
i t2=new
c TextFiel
a d(4);
r t2.setEd
e itable(fa
a lse);
( b1=ne
S w
t Button(
r "Calcul
i ate");
n b2=ne
g w
if(e.getSo *r;
urce()==b t2.setEditab
1) le(true);
{ t2.setText(
fl
b1.addActionL o .
String valu
istener(this); at eOf(a));
b2.addActionL r, t2.setEditab
istener(this); a; le(false);
add(l1); r= show(); }
add(t1); Fl } }
add(l2); oa
add(t2); t.
add( pa
b1); rs
add( e
b2); Fl
setSi oa
ze(4 t(t
00,3
00); 1.
sho ge
w(); tT
} ex
t()
);
a
=(
fl
o
at
)3
.1
4
2
*r
Area of circle