AWT
AWT
1. Java AWT
Java AWT (Abstract Window Toolkit) is an API (application Programming
Interface) that contains large number of classes and methods to create and
manage
nage Graphical User Interface (GUI
(GUI) applications.
The AWT was designed to provide a com common
mon set of tools for GUI design
that
hat could work on a variety of platforms.
The tools provided by the AWT are implemented using each platform's
native GUI toolkit, hence preserving the look and feel of each platform. This
is an advantage of using AWT.
But the disadvantage of such an approach is that GUI designed on one
platform may look different when displayed on another platform that means
AWT component are platform dependent.
AWT is heavy weight. i.e. its components are using the resources of
underlying operating system (OS).
The java.awt package provides classes for AWT API such as
o TextField
o Label
o TextArea
o Radio Button
o CheckBox
o Choice
o List, etc.
2.2 Container:
2.3 Panel
2.5 Frame
import java.awt.*;
public class Testawt
{
Testawt()
{
Frame fm=new Frame(); //Creating a frame
Label lb = new Label("welcome to java graphics"); //Creating a label
fm.add(lb); //adding label to the frame
fm.setSize(300, 300); //setting frame size.
fm.setVisible(true); //set frame visibilty true
}
public static void main(String args[])
{
Testawt ta = new Testawt();
}
}
2.5.1.2 Creating Frame Window by extending Frame class
package testawt;
import java.awt.*;
import java.awt.event.*;
public class Testawt extends Frame
{
public Testawt()
{
Button btn=new Button("Hello World");
add(btn); //adding a new Button.
setSize(400, 500); //setting size.
setTitle("StudyTonight"); //setting title.
setLayout(new FlowLayout()); //set default layout for frame.
setVisible(true); //set frame visibility true.
}
public static void main (String[] args)
{
Testawt ta = new Testawt(); //creating a frame.
}
}
2.5.2 Points to Remember:
While creating a frame (either by instantiating or extending Frame class),
following two attributes are must for visibility of the frame:
1. setSize(int width, int height);
2. setVisible(true);
When you create other components like Buttons, TextFields, etc. then you
need to add it to the frame by using the method
add(Component's Object);
You can add the following method also for resizing the frame
setResizable(true);
2.6 AWT Button
In Java, AWT contains a Button Class.
It is used for creating a labeled button which can perform an action.
Example:
Let’s take an example to create a button and add it to the frame by providing
coordinates.
import java.awt.*;
public class ButtonDemo1
{
public static void main(String[] args)
{
Frame f1=new Frame("studytonight ==> Button Demo");
Button b1=new Button("Press Here");
b1.setBounds(80,200,80,50);
f1.add(b1);
f1.setSize(500,500);
f1.setLayout(null);
f1.setVisible(true);
}
}
2.7 AWT Label
In Java, AWT contains a Label Class.
It is used for placing text in a container.
Only Single line text is allowed and the text cannot be changed directly.
Example:
In this example, we are creating two labels to display text to the frame.
import java.awt.*;
class LabelDemo1
{
public static void main(String args[])
{
Frame l_Frame= new Frame("studytonight ==> Label Demo");
Label lab1,lab2;
lab1=new Label("Welcome to studytonight.com");
lab1.setBounds(50,50,200,30);
lab2=new Label("This Tutorial is of Java");
lab2.setBounds(50,100,200,30);
l_Frame.add(lab1);
l_Frame.add(lab2);
l_Frame.setSize(500,500);
l_Frame.setLayout(null);
l_Frame.setVisible(true);
}
}
2.8 AWT TextField
In Java, AWT contains a TextField Class.
It is used for displaying single line text.
2.8.1 TextField Declaration:
public class TextField extends TextComponent
Example:
We are creating two text fields to display single line text string.
This text is editable in nature, see the below example.
import java.awt.*;
class TextFieldDemo1{
public static void main(String args[]){
Frame TextF_f= new Frame("studytonight ==>TextField");
TextField text1,text2;
text1=new TextField("Welcome to studytonight");
text1.setBounds(60,100, 230,40);
text2=new TextField("This tutorial is of Java");
text2.setBounds(60,150, 230,40);
TextF_f.add(text1);
TextF_f.add(text2);
TextF_f.setSize(500,500);
TextF_f.setLayout(null);
TextF_f.setVisible(true);
}
}
Example:
In this example, we are creating a TextArea that is used to display multiple-line
text string and allows text editing as well.
import java.awt.*;
public class TextAreaDemo1
{
TextAreaDemo1()
{
Frame textArea_f= new Frame();
TextArea area=new TextArea("Welcome to studytonight.com");
area.setBounds(30,40, 200,200);
textArea_f.add(area);
textArea_f.setSize(300,300);
textArea_f.setLayout(null);
textArea_f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaDemo1();
}
}
Example:
In this example, we are creating checkbox that are used to get user input.
If checkbox is checked it returns true else returns false.
import java.awt.*;
public class CheckboxDemo1
{
CheckboxDemo1(){
Frame checkB_f= new Frame("studytonight ==>Checkbox Example");
Checkbox ckbox1 = new Checkbox("Yes", true);
ckbox1.setBounds(100,100, 60,60);
Checkbox ckbox2 = new Checkbox("No");
ckbox2.setBounds(100,150, 60,60);
checkB_f.add(ckbox1);
checkB_f.add(ckbox2);
checkB_f.setSize(400,400);
checkB_f.setLayout(null);
checkB_f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxDemo1();
}
}
Example:
This example creates a checkbox group that is used to group multiple
checkbox in a single unit.
It is helpful when we have to select single choice among the multiples.
import java.awt.*;
public class CheckboxGroupDemo
{
CheckboxGroupDemo()
{
Frame ck_groupf= new Frame("studytonight ==>CheckboxGroup");
CheckboxGroupobj = new CheckboxGroup();
Checkbox ckBox1 = new Checkbox("Yes", obj, true);
ckBox1.setBounds(100,100, 50,50);
Checkbox ckBox2 = new Checkbox("No", obj, false);
ckBox2.setBounds(100,150, 50,50);
ck_groupf.add(ckBox1);
ck_groupf.add(ckBox2);
ck_groupf.setSize(400,400);
ck_groupf.setLayout(null);
ck_groupf.setVisible(true);
}
public static void main(String args[])
{
new CheckboxGroupDemo();
}
}
Example:
In this example, we are creating drop-down menu that is used to get user
choice from multiple choices.
import java.awt.*;
public class ChoiceDemo
{
ChoiceDemo()
{
Frame choice_f= new Frame();
Choice obj=new Choice();
obj.setBounds(80,80, 100,100);
obj.add("Red");
obj.add("Blue");
obj.add("Black");
obj.add("Pink");
obj.add("White");
obj.add("Green");
choice_f.add(obj);
choice_f.setSize(400,400);
choice_f.setLayout(null);
choice_f.setVisible(true);
}
public static void main(String args[])
{
new ChoiceDemo();
}
}
2.13 AWT List
In Java, AWT contains a List Class.
It is used to represent a list of items together.
One or more than one item can be selected from the list.
2.13.1 List Declaration:
public class List extends Component implements ItemSelectable, Accessible
Example:
In this example, we are creating a list that is used to list out the items.
import java.awt.*;
public class ListDemo
{
ListDemo()
{
Frame list_f= new Frame();
List obj=new List(6);
obj.setBounds(80,80, 100,100);
obj.add("Red");
obj.add("Blue");
obj.add("Black");
obj.add("Pink");
obj.add("White");
obj.add("Green");
list_f.add(obj);
list_f.setSize(400,400);
list_f.setLayout(null);
list_f.setVisible(true);
}
public static void main(String args[])
{
new ListDemo();
}
}
2.14.1 Declaration:
public class Canvas extends Component implements Accessible
Example:
The canvas is used to provide a place to draw using mouse pointer.
We can use it to get user architectural user input.
import java.awt.*;
public class CanvasDemo1
{
public CanvasDemo1()
{
Frame canvas_f= new Frame("studytonight ==> Canvas");
canvas_f.add(new CanvasDemo());
canvas_f.setLayout(null);
canvas_f.setSize(500, 500);
canvas_f.setVisible(true);
}
public static void main(String args[])
{
new CanvasDemo1();
}
}
class CanvasDemo extends Canvas
{
public CanvasDemo() {
setBackground (Color.WHITE);
setSize(300, 200);
}
public void paint(Graphics g)
{
g.setColor(Color.green);
g.fillOval(80, 80, 150, 75);
}
}