AWT
Abstract Window Toolkit is an API to develop GUI in Java or window-based applications.
Java AWT components are platform dependent, they are heavy weight and a part of Java Foundation Class
The main purpose of AWT is using for all the components and display it on the screen.
java. awt.*; package contains GUI
Component class such as button, textfield and label GUI container class such as Frame, Panel, dialog and scroll panel. Layout manager such as flow layout, border layout grid layout.
AWT Components used in the following program:
1] Frame
The Frame is the container that contain title bar and border and can have menu bars. It can have other components like button, text field, scrollbar etc.
2] Label
Label is an object type and it contains String. The object of the label class is a component for placing text in a container. It is used to display a single line of read only text.
Label class constructors
1. Label ()
2. Label (string str)
3. Label (string text, int alignment)
3] TextField
TextField is a class which is used to display single line text. TextField class extends TextField components.
TextField class constructors
1. TextField ()
2. TextField (string str)
3. TextField(int columns)
4] TextArea
The TextArea allows us to type as much text as we want. When the text in the text area becomes larger than the viewable area the Scrollbar appears automatically which helps us to Scroll up, down,right or left the text.
TextArea class constructors
1. TextArea ()
2. TextArea (int row, int column)
3. TextArea (string text)
5] Checkbox
The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (False).
Checkbox class constructor
1. Checkbox()
2. Checkbox (string label)
3. Checkbox (string label, boolean state)
6] CheckboxGroup
It is a used to group a set of checkboxes. When checkboxes are grouped then only one box can be checked at a time. The object of CheckboxGroup class is used to group together a set of checkbox. At a time only one checkbox button is allowed to be in "on" state and remaining checkbox button in "off" state. It inherits the object class.
7] Choice
It is used to create a drop-down list of items from which the user may choose any item. Thus, choice class is used. Add method is used to add the item to the list Choice Selected by user is shown on the top of a menu.
Choice class constructor
1. Choice()
8] List
List provide multiple choices, scrolling selection list. Using choice control we can select only one item. List object can be constructed to show any number of choices in the visible window. It allows multiple selection.
List class constructors
1. List ()
2. List (int row_num)
3. List (int row-num, Boolean multipleMode)
9] Button
A button is basically a control component with a label that generates an event when pushed.
Code:
import java.awt.*;
public class p1
{
public static void main(String[] args)
{
Frame f1=new Frame("Student Information Form");
// Label
Label lb1 = new Label("Name :");
lb1.setBounds(50,50,50,30);
f1.add(lb1);
// TextField
TextField tf1 = new TextField("Enter Name");
tf1.setBounds(200, 50, 200, 20);
f1.add(tf1);
// TextArea
Label lb2 = new Label("Address :");
lb2.setBounds(50,90,60,30);
f1.add(lb2);
TextArea ta1 = new TextArea(" ");
ta1.setBounds(200, 90, 200, 50);
f1.add(ta1);
// Checkbox
Label lb3 = new Label("Gender :");
lb3.setBounds(50,150,70,30);
f1.add(lb3);
Checkbox chk1 = new Checkbox("Male", true);
chk1.setBounds(200, 150, 60, 30);
f1.add(chk1);
Checkbox chk2 = new Checkbox("Female");
chk2.setBounds(260, 150, 60, 30);
f1.add(chk2);
// CheckboxGroup
Label lb4 = new Label("Year :");
lb4.setBounds(50,180,50,30);
f1.add(lb4);
CheckboxGroup chk_obj = new CheckboxGroup();
Checkbox chk3 = new Checkbox("1st", chk_obj ,true);
chk3.setBounds(200, 180, 50, 30);
f1.add(chk3);
Checkbox chk4 = new Checkbox("2nd", chk_obj, false);
chk4.setBounds(250, 180, 50, 30);
f1.add(chk4);
Checkbox chk5 = new Checkbox("3rd", chk_obj, false);
chk5.setBounds(300, 180, 50, 30);
f1.add(chk5);
// Choice
Label lb5 = new Label("Languages :");
lb5.setBounds(50,230,70,30);
f1.add(lb5);
Choice c1 = new Choice();
c1.setBounds(200, 230, 80, 40);
c1.add("Java");
c1.add("C");
c1.add("C++");
c1.add("PHP");
c1.add("Python");
f1.add(c1);
// List
Label lb6 = new Label("Branch :");
lb6.setBounds(50,270,70,30);
f1.add(lb6);
List ls1, ls2;
ls1 = new List();
ls1.setBounds(200, 270, 120, 50);
ls1.add("Info. Tech.");
ls1.add("Computer");
ls1.add("Mechanical");
ls1.add("Meta");
f1.add(ls1);
Label lb7 = new Label("Education :");
lb7.setBounds(50,340,70,32);
f1.add(lb7);
ls2 = new List();
ls2.setBounds(200, 340, 120, 20);
ls2.add("Engineering");
ls2.add("Polytechnic");
f1.add(ls2);
// Button
Button b1=new Button("Submit");
b1.setBounds(140,390,50,30); //(x-coordinate -, y-coordinate |, width, height)
f1.add(b1);
f1.setSize(1000,600); // (width , height)
f1.setLayout(null);
f1.setVisible(true);
}
}
Output:
Comments
Post a Comment
If there are specific topics you'd like articles on, please don't hesitate to inform me. I'll gladly publish content tailored to your preferences.