AWT Program in Java
-Rupali Shinde
AWT stands for Abstract window toolkit is an Application programming interface (API) for
creating Graphical User Interface (GUI) in Java. It allows Java programmers to develop window-
based applications.
AWT provides various components like button, label, checkbox, etc. used as objects inside a
java Program.
The following image represents the hierarchy for Java AWT.
Component Class
The component class stands at the top of the AWT hierarchy, is an abstract class that contains
all the properties of the component visible on the screen. The Component object contains
information about the currently selected foreground and background color. It also has
information about the currently selected text color.
Container
The container is a component that contains other components like button, textfield, label, etc.
However, it is a subclass of the Component class.
Panel
The Panel can be defined as a container that can be used to hold other components. However,
it doesn't contain the title bar, menu bar, or border.
Window
A window can be defined as a container that doesn't contain any border or menu bar. It creates
a top-level view. However, we must have a frame, dialog, or another window for creating a
window.
Frame
The frame is a subclass of Window. It can be defined as a container with components like
button, textfield, label, etc. In other words, AWT applications are mostly created using frame
container.
Java AWT Example
Consider the following simple example of Java AWT in which we have shown an awt
component button by setting its placement and window frame size.
1. import [Link].*;
2.
3.
4. public class AwtProgram1 {
5. public AwtProgram1()
6. {
7. Frame f = new Frame();
8. Button btn=new Button("Hello World");
9. [Link](80, 80, 100, 50);
10. [Link](btn); //adding a new Button.
11. [Link](300, 250); //setting size.
12. [Link]("JavaTPoint"); //setting title.
13. [Link](null); //set default layout for frame.
14. [Link](true); //set frame visibility true.
15. }
16.
17.
18. public static void main(String[] args) {
19. // TODO Auto-generated method stub
20.
21. AwtProgram1 awt = new AwtProgram1(); //creating a frame.
22. }
23. }
Output:
Java awt Example (extending Frame Class)
Consider the following program in which we have created a user's form GUI, which has
three fields, i.e., first name, last name, and date of birth.
1. import [Link].*;
2. public class AwtApp extends Frame {
3.
4. AwtApp(){
5. Label firstName = new Label("First Name");
6. [Link](20, 50, 80, 20);
7.
8. Label lastName = new Label("Last Name");
9. [Link](20, 80, 80, 20);
10.
11. Label dob = new Label("Date of Birth");
12. [Link](20, 110, 80, 20);
13.
14. TextField firstNameTF = new TextField();
15. [Link](120, 50, 100, 20);
16.
17. TextField lastNameTF = new TextField();
18. [Link](120, 80, 100, 20);
19.
20. TextField dobTF = new TextField();
21. [Link](120, 110, 100, 20);
22.
23. Button sbmt = new Button("Submit");
24. [Link](20, 160, 100, 30);
25.
26. Button reset = new Button("Reset");
27. [Link](120,160,100,30);
28.
29. add(firstName);
30. add(lastName);
31. add(dob);
32. add(firstNameTF);
33. add(lastNameTF);
34. add(dobTF);
35. add(sbmt);
36. add(reset);
37.
38. setSize(300,300);
39. setLayout(null);
40. setVisible(true);
41. }
42. public static void main(String[] args) {
43. // TODO Auto-generated method stub
44. AwtApp awt = new AwtApp();
45. }
46. }
Output: