The Abstract Window Toolkit (AWT) was Java’s first GUI framework to work with
windows, graphics and text. It contains many classes and methods that allow you to create
windows and simple controls. In a window we can manage fonts, output text, and utilize
graphics.
Layouts: They define how UI elements should be organized on the screen and provide
a final look and feel to the GUI (Graphical User Interface).
A Component is an abstract super class for GUI controls and it represents an object
with graphical representation.
A Container is a subclass of Component that can contain other components. Which
contains Panel, Window, Frame, Dialog
Window and panel are use to design the GUI components.
AWT Label
Label is used to display a single line of read only text. The text can be changed by a
programmer but a user cannot edit it directly.
Label Class Declaration
public class Label extends Component implements Accessible
The java.awt.Component class has following fields:
1. static int LEFT: It specifies that the label should be left justified.
2. static int RIGHT: It specifies that the label should be right justified.
3. static int CENTER: It specifies that the label should be placed in center.
Code
import java.awt.*;
/*
<applet code="LabelExample" width=300 height=50>
</applet>
*/
public class LabelExample {
public static void main(String args[]){
// creating the object of Frame class and Label class
Frame f = new Frame ("Label example");
Label l1, l2;
// initializing the labels
l1 = new Label ("First Label.");
l2 = new Label ("Second Label.");
// set the location of label
l1.setBounds(50, 100, 100, 30);
l2.setBounds(50, 150, 100, 30);
// adding labels to the frame
f.add(l1);
f.add(l2);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setVisible(true);
}
}
Button
import java.awt.*;
public class ButtonExample {
public static void main (String[] args) {
// create instance of frame with the label
Frame f = new Frame("Button Example");
// create instance of button with label
Button b = new Button("Click Here");
// set the position for the button in frame
b.setBounds(50,100,80,30);
// add button to the frame
f.add(b);
// set size, layout and visibility of frame
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Text Field
import java.awt.*;
/*
<applet code=" TextFieldExample " width=300 height=50>
</applet>
*/
public class TextFieldExample {
// main method
public static void main(String args[]) {
// creating a frame
Frame f = new Frame("TextField Example");
// creating objects of textfield
TextField t1, t2;
// instantiating the textfield objects
// setting the location of those objects in the frame
t1 = new TextField("Welcome to Java.");
t1.setBounds(50, 100, 200, 30);
t2 = new TextField("AWT Test");
t2.setBounds(50, 150, 200, 30);
// adding the components to frame
f.add(t1);
f.add(t2);
// setting size, layout and visibility of frame
f.setSize(400,400);
f.setVisible(true);
}
}
Check box
// Demonstrate check boxes.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="CheckboxDemo" width=240 height=200>
</applet>*/
public class CheckboxDemo extends Applet implements ItemListener
{
String msg = "";
Checkbox windows, android, solaris, mac;
public void init()
{
windows = new Checkbox("Windows", null, true);
android = new Checkbox("Android");
solaris = new Checkbox("Solaris");
mac = new Checkbox("Mac OS");
add(windows);
add(android);
add(solaris);
add(mac);
windows.addItemListener(this);
android.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
// Display current state of the check boxes.
public void paint(Graphics g)
{
msg = "Current state: ";
g.drawString(msg, 6, 80);
msg = " Windows: " + windows.getState();
g.drawString(msg, 6, 100);
msg = " Android: " + android.getState();
g.drawString(msg, 6, 120);
msg = " Solaris: " + solaris.getState();
g.drawString(msg, 6, 140);
msg = " Mac OS: " + mac.getState();
g.drawString(msg, 6, 160);
}
}