0% found this document useful (0 votes)
7 views

Java AWT

Uploaded by

gautamchandan25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java AWT

Uploaded by

gautamchandan25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Java AWT

Java AWT

• Java AWT (Abstract Window Toolkit) is an API to develop


Graphical User Interface (GUI) or windows-based applications in
Java.
• Java AWT (Abstract Window Toolkit) provides a set of
APIs to create GUI (Graphical User Interface)
applications in Java.
• Java AWT components are platform-dependent i.e. components
are displayed according to the view of operating system.
• AWT is heavy weight i.e. its components are using the
resources of underlying operating system (OS).
JAVA AWT……….

• The java.awt package provides classes for AWT API such as


• Button
• Label
• TextField
• TextArea
• CheckBox
• Choice
• List
• Canvas
• ScorllBar
• Pannel
Java AWT Hierarchy
• 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:

• There are four types of containers in Java AWT


Window
Panel
Frame
Dialog
Window
• The window is the container that have no borders and
menu bars.
• You must use frame, dialog or another window for
creating a window.
• We need to create an instance of Window class to create
this container.
Panel
• The Panel is the container that doesn't contain title bar,
border or menu bar.
• It is generic container for holding the components.
• It can have other components like button, text field etc.
• An instance of Panel class creates a container, in which
we can add components.
Frame

• The Frame is the container that contain title bar and


border and can have menu bars.
• It can have other components like button, text field,
scrollbar etc.
• Frame is most widely used container while developing
an AWT application.
Java AWT Example

• To create simple AWT example, you need a frame. There


are two ways to create a GUI using Frame in AWT.
By extending Frame class (inheritance)
By creating the object of Frame class (association)

AWT Example by Inheritance:


Let's see a simple example of AWT where we are
inheriting Frame class.
Here, we are showing Button component on the Frame.
Java AWT Example

• 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

• Let's see a simple example of AWT where we are


creating instance of Frame class.
• Here, we are creating a TextField, Label and Button
component on the Frame.
Association AWT Example
• import java.awt.*;
• // class AWTExample2 directly creates instance of Frame class
• class AWTExample2
• {
• // initializing using constructor
• AWTExample2()
• {
• // creating a Frame
• Frame f = new Frame();
• // creating a Label
• Label l = new Label("Employee id:");
• // creating a Button
• Button b = new Button("Submit");
• // creating a TextField
• TextField t = new TextField();
• // setting position of above components in the frame
• l.setBounds(20, 80, 80, 30);
• t.setBounds(20, 100, 80, 30);
• b.setBounds(100, 100, 80, 30);
• // adding components into frame
• f.add(b);
• f.add(l);
• f.add(t);
• // frame size 300 width and 300 height
• f.setSize(400,300);

Java AWT Button

• A button is basically a control component with a label


that generates an event when pushed.
• The Button class is used to create a labeled button that
has platform independent implementation.
• The application result in some action when the button is
pushed.
Button Class Declaration:
public class Button extends Component implements Accessible
Button Class Constructors
Button Class Methods
Java AWT Button 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(new ActionListener()
• {
• public void actionPerformed(ActionEvent e)
• {
• System.out.println("Button clicked");
• }
• });

• frame.add(button);
• frame.setSize(200, 200);
• frame.setLayout(null);
• frame.setVisible(true);
• }
Label
•import java.awt.Frame;
•import java.awt.Label;

•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 name");
• l2 = new Label(“Last name");
• // 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.setLayout(null);
• f.setVisible(true);
• }

•}
TextFiled
• A TextField component in AWT is used to create a single-
line text input field.
TextField Example
• import java.awt.*;

• public class TextFieldExample


• {
• public static void main(String[] args)
• {
• Frame frame = new Frame("TextField Example");
• TextField textField = new TextField();
• textField.setBounds(50, 100, 200, 30);
• frame.add(textField);
• frame.setSize(300, 300);
• frame.setLayout(null);
• frame.setVisible(true);
• }

TextArea
• A TextArea component in AWT is used to create a multi-
line text input field.
TextArea Example
• import java.awt.*;

• public class TextAreaExample


• {
• public static void main(String[] args)
• {
• Frame frame = new Frame("TextArea Example");
• TextArea textArea = new TextArea();

• textArea.setBounds(50, 100, 200, 100);

• 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.*;

• public class CheckboxExample


• {
• public static void main(String[] args)
• {
• Frame frame = new Frame("Checkbox Example");
• Checkbox1 checkbox = new Checkbox("Accept");

• checkbox.setBounds(50, 100, 150, 30);

• 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.*;

• public class ChoiceExample


• {
• public static void main(String[] args)
• {
• Frame frame = new Frame("Choice Example");
• Choice choice = new Choice();
• choice.setBounds(50, 100, 150, 30);
• choice.add("Default");
• choice.add("YES");
• choice.add("NO");
• choice.add("NONE");

• 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

• A List component in AWT is used to create a list of


items.
List Example
• import java.awt.*;
• import java.awt.event.*;

• public class ListExample


• {
• public static void main(String[] args)
• {
• Frame frame = new Frame("List Example");
• List list = new List();

• list.setBounds(50, 100, 150, 100);


• list.add("Item 1");
• list.add("Item 2");
• list.add("Item 3");

• 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

• A Canvas component in AWT is used to create a blank


rectangular area where we can draw or trap input
events.
Canvas Example
• import java.awt.*;

• public class CanvasExample


• {
• public static void main(String[] args)
• {
• Frame frame = new Frame("Canvas Example");
• Canvas canvas = new Canvas();

• canvas.setBounds(50, 50, 200, 200);


• canvas.setBackground(Color.GREEN);

• frame.add(canvas);
• frame.setSize(300, 300);
• frame.setLayout(null);
• frame.setVisible(true);
• }
• }
Scrollbar

• A Scrollbar component in AWT is used to create a


scrollbar.
Scrollbar Example
• import java.awt.*;

• public class ScrollbarExample1
• {

• // class constructor
• ScrollbarExample1()
• {

• // creating a frame
• Frame f = new Frame("Scrollbar Example");
• // creating a scroll bar
• Scrollbar s = new Scrollbar();

• // setting the position of scroll bar
• s.setBounds (100, 100, 50, 100);

• // adding scroll bar to the frame
• f.add(s);

• // setting size, layout and visibility of frame
• f.setSize(400, 400);
• f.setLayout(null);
• f.setVisible(true);
• }

• // main method
• public static void main(String args[]) {
Panel
• A Panel component in AWT is used as a container to
organize other components.
Pannel
• import java.awt.*;

• public class PanelExample


• {
• public static void main(String[] args)
• {
• Frame frame = new Frame("Panel Example");
• Panel panel = new Panel();

• panel.setBounds(50, 50, 200, 200);


• panel.setBackground(Color.LIGHT_GRAY);

• Button button = new Button("Click Me");


• panel.add(button);

• frame.add(panel);
• frame.setSize(300, 300);
• frame.setLayout(null);
• frame.setVisible(true);
• }
• }

You might also like