4.1 Component
AWT - Abstract Window Toolkit
यह एक API है जिसकी सहायता से GUI (Window Based) Applications का निर्माण किया जा सकता है |
AWT प्लेटफार्म-आश्रित (Platform-dependent) है अर्थात इसके GUI अवयव का चित्रण ऑपरेटिंग सिस्टम के अनुसार होता है | जो भिन्न ऑपरेटिंग सिस्टम के लिए भिन्न हो सकता है |
AWT भारी-भरकम (heavyweight) है अर्थात इसके GUI अवयवों द्वारा ऑपरेटिंग सिस्टम के अत्यधिक संसाधनों (OS resources) का उपयोग होता है | इसके द्वारा किसी अवयव को चित्रित करने के लिए ऑपरेटिंग सिस्टम की native sub-routines का उपयोग किया जाता है |
चूँकि AWT platform-dependent and heavyweight है अतः वर्तमान में GUI डिजाईन में इसका उपयोग न के बराबर किया जाता है | इसकी जगह Swing API का उपयोग किया जाता है जो platform independent तथा lightweight है |
java.awt - AWT की समस्त classes (जैसे - TextField, Label, Button, RadioButton, CheckBox, Choice, List, Menu आदि ) java.awt
नाम के पैकेज (package) में अवस्थित है | अतः AWT का प्रयोग अपने एप्लीकेशन में करने के लिए हमें इस पैकेज से विभिन्न classes को इम्पोर्ट करना ही होगा |
Java AWT Class Hierarchy

Component (अवयव)
सभी अवयव जैसे button, text fields, scrollbars, list, choice आदि को component के नाम से जाना जाता है |
AWT में प्रत्येक अवयव (Component) के लिए एक class होती है |
प्रत्येक अवयव को screen पर दर्शाने के लिए उसे Container पर add करना पड़ता है | अर्थात Container एक ऐसी जगह है जिस पर दुसरे components को रखा जा सकता है |
एक Container अवयवों के समूह को धारण करता है तथा यह उनके layout को भी नियंत्रित करता है |
Container स्वयं भी एक Component है अतः एक container के अन्दर दुसरे container को रखा जा सकता है |
AWT में चार प्रकार के Containers उपलब्ध है -
1. Window
2. Frame
3. Dialog
4. Panel
Frame और Dialog दोनों Window class के subclass है |
| Border | Title | Menubar | Remark | |
|---|---|---|---|---|
| Window | No | No | No | |
| Dialog | Yes | Yes | No | इसका स्वतंत्र अस्तित्व नहीं होता है | प्रत्येक Dialog class के object के साथ सम्बंधित Frame class का object होना आवश्यक है | |
| Panel | No | No | No | यह एक प्रकार का generic container है जिस पर components को रखा जा सकता है | |
| Frame | Yes | Yes | Yes | GUI application के निर्माण में अधिकतर इसी class का उपयोग होता है | |
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. |
Frame class की सहायता से GUI application का निर्माण दो प्रकार से किया जा सकता है -
1. Frame class को extend करके (Inheritance)
2. Frame class का object लेकर (Composition / association)
|
|---|
Example - 2 By creating instance / object of frame class (association)
import java.awt.*;
public class Example2 {
Example2()
{
//Creating Frame
Frame fr=new Frame();
//Creating a label
Label lb = new Label("UserId: ");
//adding label to the frame
fr.add(lb);
//Creating Text Field
TextField t = new TextField();
//adding text field to the frame
fr.add(t);
//setting frame size
fr.setSize(500, 300);
//Setting the layout for the Frame
fr.setLayout(new FlowLayout());
fr.setVisible(true);
}
public static void main(String args[])
{
Example2 ex = new Example2();
}
}