Types of Components in Component Class
The Component class in Java has several types, which include:
1. Container
A Container is a component that can hold other components, such as buttons and labels. It acts as a parent for other GUI elements.
Example:
import java.awt.*;
import javax.swing.*;
public class ContainerExample {
public static void main(String[] args) {
Frame frame = new Frame("Container Example");
Panel panel = new Panel();
panel.add(new Button("Click Me"));
frame.add(panel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Output:
A frame with a button inside a panel.
2. Button
A Button is a clickable component that triggers an action when pressed.
Example:
import java.awt.*;
import java.awt.event.*;
public class ButtonExample {
public static void main(String[] args) {
Frame frame = new Frame("Button Example");
Button button = new Button("Click Me");
button.setBounds(50, 100, 80, 30);
button.addActionListener(e -> System.out.println("Button Clicked"));
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
Output:
A button that prints "Button Clicked" when pressed.
3. Label
A Label is a component used to display text that the user cannot modify.
Example:
import java.awt.*;
public class LabelExample {
public static void main(String[] args) {
Frame frame = new Frame("Label Example");
Label label = new Label("Hello, World!");
label.setBounds(50, 100, 100, 30);
frame.add(label);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
Output:
A label displaying "Hello, World!".
4. Checkbox
A Checkbox allows users to select or deselect an option.
Example:
import java.awt.*;
public class CheckboxExample {
public static void main(String[] args) {
Frame frame = new Frame("Checkbox Example");
Checkbox checkbox = new Checkbox("Accept Terms");
checkbox.setBounds(50, 100, 150, 30);
frame.add(checkbox);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
Output:
A checkbox labeled "Accept Terms".
5. Choice
A Choice (dropdown menu) allows users to select an option from a list.
Example:
import java.awt.*;
public class ChoiceExample {
public static void main(String[] args) {
Frame frame = new Frame("Choice Example");
Choice choice = new Choice();
choice.add("Option 1");
choice.add("Option 2");
choice.add("Option 3");
choice.setBounds(50, 100, 100, 30);
frame.add(choice);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
Output:
A dropdown menu with three options.
6. List
A List component displays multiple selectable items.
Example:
import java.awt.*;
public class ListExample {
public static void main(String[] args) {
Frame frame = new Frame("List Example");
List list = new List();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
list.setBounds(50, 100, 100, 70);
frame.add(list);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
Output:
A list containing "Item 1", "Item 2", and "Item 3".
Frequently Asked Questions
What is the use of the Component class in Java?
The Component class in Java is the base class for all GUI components in AWT, such as buttons, labels, and checkboxes. It provides methods for handling events, layout management, and rendering.
How is a button different from a label in Java?
A button is an interactive component that triggers an action when clicked, whereas a label is a non-editable text display component.
What is the difference between a Checkbox and a Choice in Java?
A Checkbox allows selecting multiple options, while a Choice (dropdown) lets the user pick only one option from a predefined list.
Conclusion
In this article, we learned about the Component class in Java, its role in the AWT framework, and its key properties and methods. It serves as the foundation for creating UI elements and handling events. Understanding this class helps developers build interactive and efficient GUI applications, enabling better customization and enhanced functionality in Java-based applications.