AWT Attributes in Java
In Java AWT (Abstract Window Toolkit), attributes generally refer to the properties and
features of AWT components (like Button, Label, Frame, etc.) and classes. These attributes
define how components look, behave, and interact with users.
1. Component Attributes (Common to all AWT components)
- Size and Position
• setSize(int width, int height) → sets width and height.
• setBounds(int x, int y, int width, int height) → sets position and size.
• getX(), getY(), getWidth(), getHeight() → retrieve values.
- Visibility
• setVisible(true/false) → shows or hides a component.
• isVisible() → checks visibility.
- Enable/Disable
• setEnabled(true/false) → enables or disables component interaction.
• isEnabled() → checks if enabled.
- Background and Foreground Colors
• setBackground(Color c) → sets background color.
• setForeground(Color c) → sets text/drawing color.
- Font
• setFont(Font f) → sets font style.
• getFont() → retrieves font.
- Name and Tooltip
• setName(String name) → assigns a name to the component.
• getName() → retrieves it.
2. Container Attributes
- add(Component c) → adds a component.
- remove(Component c) → removes a component.
- setLayout(LayoutManager lm) → sets layout manager.
- getComponents() → retrieves all components inside.
3. Frame Attributes
- setTitle(String title) → sets window title.
- setSize(int w, int h) → sets window size.
- setResizable(boolean b) → allows/disallows resizing.
- setIconImage(Image img) → sets window icon.
- setVisible(true) → shows the frame.
4. Button Attributes
- setLabel(String label) → sets text on button.
- getLabel() → retrieves text.
5. Label Attributes
- setText(String text) → changes label text.
- setAlignment(int alignment) → [Link], [Link], [Link].
6. TextField Attributes
- setText(String text) → sets text inside.
- getText() → retrieves entered text.
- setColumns(int n) → sets number of columns.
- setEchoChar(char c) → masks input (e.g., password field).
7. Checkbox Attributes
- setState(boolean state) → checked or unchecked.
- getState() → returns true or false.
- setLabel(String label) → sets text.
8. Choice (Dropdown) Attributes
- add(String item) → adds item.
- getSelectedItem() → gets selected item.
- getItemCount() → number of items.
Summary
Common AWT attributes come from Component (size, position, color, font, visibility).
Specific attributes belong to individual UI components like Button, Label, TextField,
Checkbox, etc.
Examples with Attributes
1. Frame Example (Frame Attributes)
import [Link].*;
public class FrameExample {
public static void main(String[] args) {
Frame f = new Frame("AWT Frame Example"); // setTitle
[Link](400, 300); // setSize
[Link](100, 100); // setBounds / position
[Link](Color.LIGHT_GRAY); // setBackground
[Link](false); // setResizable
[Link](true); // setVisible
}
}
2. Button Example (Button Attributes)
import [Link].*;
public class ButtonExample {
public static void main(String[] args) {
Frame f = new Frame("Button Example");
Button b = new Button("Click Me"); // setLabel
[Link](100, 100, 80, 30); // setBounds
[Link]([Link]); // setBackground
[Link]([Link]); // setForeground
[Link](b);
[Link](300, 200);
[Link](null); // No layout manager
[Link](true);
}
}
3. Label Example (Label Attributes)
import [Link].*;
public class LabelExample {
public static void main(String[] args) {
Frame f = new Frame("Label Example");
Label l = new Label("Hello AWT!", [Link]); // setText + alignment
[Link](50, 50, 200, 30);
[Link](new Font("Serif", [Link], 16)); // setFont
[Link]([Link]); // setForeground
[Link](l);
[Link](300, 200);
[Link](null);
[Link](true);
}
}
4. TextField Example (TextField Attributes)
import [Link].*;
public class TextFieldExample {
public static void main(String[] args) {
Frame f = new Frame("TextField Example");
TextField tf = new TextField("Enter Name"); // setText
[Link](50, 50, 150, 30); // setBounds
[Link](20); // setColumns
[Link]('*'); // setEchoChar (like password)
[Link](tf);
[Link](300, 200);
[Link](null);
[Link](true);
}
}
5. Checkbox Example (Checkbox Attributes)
import [Link].*;
public class CheckboxExample {
public static void main(String[] args) {
Frame f = new Frame("Checkbox Example");
Checkbox cb1 = new Checkbox("Java", true); // setState
[Link](50, 50, 100, 30);
Checkbox cb2 = new Checkbox("Python"); // unchecked by default
[Link](50, 100, 100, 30);
[Link](cb1);
[Link](cb2);
[Link](300, 200);
[Link](null);
[Link](true);
}
}
6. Choice Example (Dropdown Attributes)
import [Link].*;
public class ChoiceExample {
public static void main(String[] args) {
Frame f = new Frame("Choice Example");
Choice c = new Choice();
[Link]("Java"); // add item
[Link]("Python");
[Link]("C++");
[Link]("JavaScript");
[Link](50, 50, 100, 30);
[Link](c);
[Link](300, 200);
[Link](null);
[Link](true);
}
}