Core AWT Control Components: Buttons, Labels, TextFields, Checkboxes, and More
AWT Control Components in Java – Buttons, Labels, TextFields, Checkboxes और अन्य Controls
Java के AWT (Abstract Window Toolkit) का use GUI (Graphical User Interface) बनाने के लिए किया जाता है। अगर आप Java में Window-based Application बनाना चाहते हैं, तो AWT Controls आपके लिए सबसे important part हैं। इस blog में हम AWT के Core Control Components जैसे कि Button, Label, TextField, Checkbox और अन्य Components को detail में समझेंगे — बिल्कुल exam point of view से।
AWT Controls Introduction
AWT (Abstract Window Toolkit) Java का एक toolkit है जो user interface elements जैसे buttons, labels, textfields, scrollbars आदि provide करता है। ये controls platform-dependent होते हैं, यानी ये उस operating system के look & feel को follow करते हैं जिस पर program run हो रहा है।
हर control Component class से derived होता है, और AWT का हर GUI element एक object के रूप में represent किया जाता है।
AWT Control Hierarchy
AWT के सारे controls एक class hierarchy में organized होते हैं। इसका root class Component है, जो basic GUI behavior define करता है। नीचे उसकी simplified hierarchy दी गई है:
Component
├── Container
│ ├── Panel
│ │ ├── Applet
│ │ └── Canvas
│ └── Window
│ ├── Frame
│ └── Dialog
└── Button, Label, TextField, Checkbox, List, Choice आदि
Button Control
Button एक ऐसा control होता है जो किसी action को perform करने के लिए use किया जाता है, जैसे form submit करना या कोई event trigger करना। जब user किसी button को click करता है, तो event generate होता है जिसे ActionListener handle करता है।
Button Class Declaration
Button b = new Button("Click Me");
Button Methods
- setLabel(String text) – Button का label बदलने के लिए।
- getLabel() – Current label प्राप्त करने के लिए।
- addActionListener(ActionListener l) – Event handler जोड़ने के लिए।
Example of Button
import java.awt.*;
import java.awt.event.*;
class ButtonExample extends Frame implements ActionListener {
Button b;
ButtonExample() {
b = new Button("Click Me");
b.setBounds(100, 100, 80, 30);
b.addActionListener(this);
add(b);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
b.setLabel("Clicked!");
}
public static void main(String[] args) {
new ButtonExample();
}
}
Label Control
Label एक read-only text display करने के लिए use किया जाता है। यह किसी static information या message दिखाने के लिए perfect होता है। Label user input नहीं ले सकता।
Label Class Declaration
Label l = new Label("Welcome to Java AWT");
Label Methods
- setText(String text) – Text बदलने के लिए।
- getText() – Current text प्राप्त करने के लिए।
- setAlignment(int alignment) – Alignment सेट करने के लिए (LEFT, CENTER, RIGHT)।
Example of Label
import java.awt.*;
class LabelExample extends Frame {
Label l;
LabelExample() {
l = new Label("Welcome to AWT Label Example");
l.setBounds(50, 100, 250, 30);
add(l);
setSize(400, 300);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new LabelExample();
}
}
TextField Control
TextField का use single-line user input लेने के लिए किया जाता है। जैसे username, email या कोई short entry field।
TextField Class Declaration
TextField t = new TextField("Default Text");
TextField Methods
- setText(String text) – Field में text डालने के लिए।
- getText() – Field से text प्राप्त करने के लिए।
- setEchoChar(char c) – Password field जैसा behavior देने के लिए।
Example of TextField
import java.awt.*;
import java.awt.event.*;
class TextFieldExample extends Frame implements ActionListener {
TextField t1, t2;
Button b;
TextFieldExample() {
t1 = new TextField();
t1.setBounds(50, 100, 150, 20);
t2 = new TextField();
t2.setBounds(50, 150, 150, 20);
b = new Button("Show");
b.setBounds(100, 200, 60, 30);
b.addActionListener(this);
add(t1); add(t2); add(b);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
t2.setText(t1.getText());
}
public static void main(String[] args) {
new TextFieldExample();
}
}
Checkbox Control
Checkbox multiple options में से एक या अधिक को select करने के लिए use होता है। Checkbox का state checked या unchecked हो सकता है।
Checkbox Class Declaration
Checkbox c = new Checkbox("Java", true);
Checkbox Methods
- getState() – Returns true अगर checkbox checked है।
- setState(boolean state) – Checkbox की state बदलने के लिए।
- getLabel() – Label प्राप्त करने के लिए।
Example of Checkbox
import java.awt.*;
import java.awt.event.*;
class CheckboxExample extends Frame implements ItemListener {
Checkbox c1, c2;
Label l;
CheckboxExample() {
l = new Label("Select your course:");
l.setBounds(50, 50, 150, 20);
c1 = new Checkbox("Java");
c1.setBounds(100, 100, 80, 20);
c2 = new Checkbox("Python");
c2.setBounds(100, 130, 80, 20);
add(l); add(c1); add(c2);
c1.addItemListener(this);
c2.addItemListener(this);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
String msg = "Selected: ";
if (c1.getState()) msg += "Java ";
if (c2.getState()) msg += "Python ";
l.setText(msg);
}
public static void main(String[] args) {
new CheckboxExample();
}
}
CheckboxGroup (Radio Buttons)
CheckboxGroup का use तब होता है जब आपको single selection allow करनी हो, जैसे gender selection में (Male/Female)। इसे Radio Buttons भी कहते हैं।
CheckboxGroup Example
import java.awt.*;
import java.awt.event.*;
class RadioExample extends Frame implements ItemListener {
CheckboxGroup cg;
Checkbox male, female;
Label l;
RadioExample() {
cg = new CheckboxGroup();
male = new Checkbox("Male", cg, true);
female = new Checkbox("Female", cg, false);
l = new Label("Gender: Male");
male.setBounds(100, 100, 80, 20);
female.setBounds(100, 130, 80, 20);
l.setBounds(100, 160, 120, 20);
add(male); add(female); add(l);
male.addItemListener(this);
female.addItemListener(this);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
l.setText("Gender: " + cg.getSelectedCheckbox().getLabel());
}
public static void main(String[] args) {
new RadioExample();
}
}
Choice Control (Drop-Down List)
Choice control एक drop-down list provide करता है जिससे user एक option select कर सकता है। यह space-efficient UI element है।
Choice Class Declaration
Choice ch = new Choice();
Example of Choice
import java.awt.*;
import java.awt.event.*;
class ChoiceExample extends Frame implements ItemListener {
Choice ch;
Label l;
ChoiceExample() {
l = new Label("Select your language:");
l.setBounds(50, 50, 150, 20);
ch = new Choice();
ch.setBounds(100, 100, 100, 20);
ch.add("Java");
ch.add("Python");
ch.add("C++");
ch.add("C#");
add(l); add(ch);
ch.addItemListener(this);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
l.setText("Selected: " + ch.getSelectedItem());
}
public static void main(String[] args) {
new ChoiceExample();
}
}
List Control
List control multiple item selection allow करता है। यह drop-down की तरह नहीं बल्कि एक scrollable box में items दिखाता है।
List Example
import java.awt.*;
import java.awt.event.*;
class ListExample extends Frame implements ActionListener {
List l;
Button b;
ListExample() {
l = new List(4, true);
l.add("Java");
l.add("Python");
l.add("C++");
l.add("C#");
b = new Button("Show Selected");
b.addActionListener(this);
l.setBounds(50, 50, 100, 100);
b.setBounds(50, 170, 100, 30);
add(l); add(b);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String items[] = l.getSelectedItems();
for (String s : items)
System.out.println(s);
}
public static void main(String[] args) {
new ListExample();
}
}
Scrollbar Control
Scrollbar का use scrolling functionality देने के लिए किया जाता है। यह vertical या horizontal दोनों प्रकार का हो सकता है।
Scrollbar Example
import java.awt.*;
import java.awt.event.*;
class ScrollbarExample extends Frame implements AdjustmentListener {
Scrollbar s;
Label l;
ScrollbarExample() {
s = new Scrollbar();
s.setBounds(100, 100, 20, 100);
l = new Label();
l.setBounds(150, 100, 150, 20);
add(s); add(l);
s.addAdjustmentListener(this);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void adjustmentValueChanged(AdjustmentEvent e) {
l.setText("Value: " + s.getValue());
}
public static void main(String[] args) {
new ScrollbarExample();
}
}
AWT Core Components Summary Table
| Component | Purpose | Main Methods |
|---|---|---|
| Button | Event trigger करने के लिए | setLabel(), addActionListener() |
| Label | Static text दिखाने के लिए | setText(), setAlignment() |
| TextField | User input लेने के लिए | setText(), getText() |
| Checkbox | Multiple options select करने के लिए | getState(), setState() |
| Choice | Single option drop-down list | add(), getSelectedItem() |
| List | Multiple selection list box | add(), getSelectedItems() |
| Scrollbar | Scrolling control देने के लिए | getValue(), setValue() |
AWT Exam-Oriented Notes
- AWT का full form है Abstract Window Toolkit।
- AWT के सारे controls platform-dependent होते हैं।
- हर AWT component Component class से inherit होता है।
- Button click करने पर ActionEvent generate होता है।
- Checkbox selection बदलने पर ItemEvent generate होता है।
- Choice और List दोनों item selection controls हैं, लेकिन Choice single selection के लिए और List multiple selection के लिए होता है।
- TextField default में single-line text input के लिए होता है।
- AWT controls का layout manually set किया जा सकता है using setLayout(null)।