Labels
Buttons
TextField
TextFieldArea
Checkbox
Choice
List
ScollBar
This class is a Component which displays a single line of text.
Labels are read-only. That is, the user cannot click on a label to edit the text it displays.
Text can be aligned within the label
Label myLabel = new Label("Enter your name:");
Example- LabelExample.java
import java.awt.*;
public class LabelExample {
public static void main(String[] args) {
// Create a Frame
Frame frame = new Frame("Label Example");
// Create two Label objects
Label label1, label2;
// Initialize the labels
label1 = new Label("First Label");
label2 = new Label("Second Label");
// Set the position and size for the labels
label1.setBounds(50, 100, 100, 30);
label2.setBounds(50, 150, 100, 30);
// Add the labels to the frame
frame.add(label1);
frame.add(label2);
// Set the size, layout, and make the frame visible
frame.setSize(400, 400);
frame.setLayout(null); // Using null layout for manual positioning
frame.setVisible(true);
}
}
Output
This class represents a push-button which displays some specified text.
When a button is pressed, it notifies its Listeners. (More about Listeners in the next chapter).
To be a Listener for a button, an object must implement the ActionListener Interface.
Button myButton = new Button("Click me!");
This class displays a single line of optionally editable text.
This class inherits several methods from TextComponent.
This is one of the most commonly used Components in the AWT
TextField t1 = new TextField();
TextField Class constructors
This class displays multiple lines of optionally editable text.
This class inherits several methods from TextComponent.
TextArea also provides the methods: appendText(), insertText() and replaceText()
Checkbox box1 = new Checkbox("Check me!");
This class represents a dropdown list of Strings.
Similar to a list in terms of functionality, but displayed differently.
Only one item from the list can be selected at one time and the currently selected element is displayed.
Choice myChoice1 = new Choice();
This class is a Component which displays a list of Strings.
The list is scrollable, if necessary.
Sometimes called Listbox in other languages.
Lists can be set up to allow single or multiple selections.
The list will return an array indicating which Strings are selected
The Scrollbar class is a component in Java's AWT package that allows users to scroll through a range of values. It provides constructors, methods to get/set properties, and event listeners. It is useful for navigating through large amounts of content in Java GUIs.
It is a part of AWT, an older GUI framework in Java, but still widely used in legacy systems and simple applications.