Java AWT
Java AWT
Java AWT
Java AWT
• Components:
• All the elements like the button, text fields, scroll bars, etc.
are called components.
• Container:
• The Container is a component in AWT that can contain
another components like buttons, textfields, labels etc.
• The classes that extends Container class are known as
container such as
• Frame
• Dialog and
• Panel.
Types of containers:
• import java.awt.*;
• public class AWTExample extends Frame
• {
•
• // initializing using constructor
• AWTExample1()
• {
• // creating a button
• Button b = new Button("Click Me!!");
•
• // setting button position on screen
• b.setBounds(30,100,80,30);
•
• // adding button into frame
• add(b);
•
• // frame size 300 width and 300 height
• setSize(300,300);
•
• // setting the title of Frame
• setTitle("This is our basic AWT example");
•
• // no layout manager
• setLayout(null);
•
AWT Example by Association
• frame.add(button);
• frame.setSize(200, 200);
• frame.setLayout(null);
• frame.setVisible(true);
• }
Label
•import java.awt.Frame;
•import java.awt.Label;
•}
TextFiled
• A TextField component in AWT is used to create a single-
line text input field.
TextField Example
• import java.awt.*;
• frame.add(textArea);
• frame.setSize(300, 300);
• frame.setLayout(null);
• frame.setVisible(true);
• }
• }
Checkbox
• A Checkbox component in AWT is used to create a
checkbox.
Checkbox Example
• import java.awt.*;
• import java.awt.event.*;
• checkbox.addItemListener(new ItemListener()
• {
• public void itemStateChanged(ItemEvent e)
• {
• System.out.println("Checkbox: " + (e.getStateChange() == 1 ? "Checked" : "Unchecked"));
• }
• });
• frame.add(checkbox);
• frame.setSize(300, 300);
• frame.setLayout(null);
• frame.setVisible(true);
Choice
• A Choice component in AWT is used to create a
dropdown list.
Choice
• import java.awt.*;
• import java.awt.event.*;
• choice.addItemListener(new ItemListener()
• {
• public void itemStateChanged(ItemEvent e)
• {
• System.out.println("Selected: " + choice.getSelectedItem());
• }
• });
• frame.add(choice);
• frame.setSize(300, 300);
• frame.setLayout(null);
• frame.setVisible(true);
• }
List
• list.addItemListener(new ItemListener()
• {
• public void itemStateChanged(ItemEvent e)
• {
• System.out.println("Selected: " + list.getSelectedItem());
• }
• });
• frame.add(list);
• frame.setSize(300, 300);
• frame.setLayout(null);
• frame.setVisible(true);
• }
• }
Canvas
• frame.add(canvas);
• frame.setSize(300, 300);
• frame.setLayout(null);
• frame.setVisible(true);
• }
• }
Scrollbar
• frame.add(panel);
• frame.setSize(300, 300);
• frame.setLayout(null);
• frame.setVisible(true);
• }
• }