AWT(Abstract Window Tool Kit)
Introduction
Java AWT (Abstract Window Toolkit) is an API (Application programming interface) to develop
Graphical User Interface (GUI) or windows-based applications in Java.
Java AWT contains components these components are platform-dependent i.e. components are
displayed according to the view of operating system these are heavy weight. The java.awt package
provides classes for AWT API.
AWT Components
Button
Label
Checkbox
Choice
List
TextField
All the elements like the button, text fields, label, etc. are called components. In Java AWT there are
classes for each component. In order to place every component in a particular position on a screen, we
need to add them to a container.
Button:
Button b1=new Button();
b1.setLabel(“Submit”);
Submit
Label:
Label L=new Label();
L.setText(“Name: ”);
L.setText(“Address: ”);
Name :
Address :
Checkbox:
Checkbox cb=new Checkbox(“Male”);
Male
Choice:
Choice ch=new Choice();
`1 Rakesh M.Sc.
ch.add(“BCOM”);
ch.add(“BSC”);
ch.add(“BA”);
ch.add(“BBA”);
List:
List Li=new List(number of items(numeric value) , Boolean value true / false);
List Li=new List(3 , true);
Li.add(“BCOM”);
Li.add(“BSC”);
Li.add(“BA”);
TextField:
TextField T1=new TextField(size/ width );
TextField T1=new TextField(10);
Useful Methods of Component Class
Method Description
public void add(Component c) Inserts a component on this component.
public void setSize(int width,int height) Sets the size (width and height) of the component.
public void setLayout(LayoutManager m) Defines the layout manager for the component.
public void setVisible(boolean status) Changes the visibility of the component, by default false.
public void setTitle(“String”) It Will Set the title for the frame
Creating Java AWT
There are two ways to create java AWT
1. By extending Frame class
2. By creating the object of Frame class
Program by extending Frame Class
import java.awt.*; // importing Java AWT class
// extending Frame class to our class AWTExample1
public class AWTEx extends Frame {
// initializing using constructor
AWTEx()
{ // creating a button
Button b = new Button("Click Me!!");
// setting button position on screen
b.setBounds(30,100,80,30);
`2 Rakesh M.Sc.
// adding button into frame
add(b);
// frame size 300 width and 300 height
setSize(300,300);
// setting the title of Frame
setTitle("This is our basic AWT example");
// no layout manager
setLayout(null);
// now frame will be visible, by default it is not visible
setVisible(true);
}
// main method
public static void main(String args[]) {
// creating instance of Frame class
AWTEx f = new AWTEx();
}
}
Program by creating object of Frame Class
import java.awt.*; // importing Java AWT class
public class AWTEx
{
AWTEx() // initializing using constructor
{
Frame f = new Frame(); // creating a Frame
Button b = new Button("Click Me!!"); // creating a button
b.setBounds(30,100,80,30); // setting button position on screen
f.add(b); // adding button into frame
f.setSize(300,300); // frame size 300 width and 300 height
f.setTitle("This is our basic AWT example"); // setting the title of Frame
f.setLayout(null); // no layout manager
f.setVisible(true); // now frame will be visible, by default it is not visible
public static void main(String args[]) // main method
{
AWTEx aw = new AWTEx(); // creating instance of Frame class
}
}
`3 Rakesh M.Sc.