Bbit2205-01 Awt 2024
Bbit2205-01 Awt 2024
creating
Frame by
creating
instance of
Frame class
The code in Listing 1 creates an empty frame
• The title of the frame ("Example 1") is set in
the call to the constructor. A frame is initially
invisible and must be made visible by invoking
its show() method.
AWT Controls
• Every user interface considers the following
three main aspects:
– UI elements
– Layouts
– Behavior
UI elements
• These are the core visual elements the user
eventually sees and interacts with.
• AWT provides a huge list of widely used and
common elements varying from basic to
complex
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).
• This part will be covered in Layout
Behavior
• These are events which occur when the user
interacts with UI elements.
• This part will be covered in Event Handling
Java AWT Hierarchy
Java Swing Hierarchy
Layout Managers
• The layout of the elements in a container is
handled by the layout manager associated
with the container.
• Relative positions of the elements are
specified, not their absolute coordinates.
• The positions and sizes of the element will be
automatically adjusted when the window is
resized
Layout Managers
Since the Component class defines the setSize()
and setLocation() methods, all Components can
be sized and positioned with those methods.
Problem:
the parameters provided to those methods are
defined in terms of pixels.
Pixel sizes may be different (depending on the
platform) so the use of those methods tends to
produce GUIs which will not display properly on all
platforms.
Layout Managers
Solution:
Layout Managers.
Layout managers are assigned to Containers.
When a Component is added to a Container, its
Layout Manager is consulted in order to
determine the size and placement of the
Component.
NOTE: If you use a Layout Manager, you can no longer
change the size and location of a Component through
the setSize and setLocation methods.
Layout Managers
There are several different LayoutManagers,
each of which sizes and positions its
Components based on an algorithm:
FlowLayout
BorderLayout
GridLayout
f.setVisible(true);
}
}
Border Layout
The BorderLayout Manager breaks the
Container up into 5 regions (North, South,
East, West, and Center).
When Components are added, their region
is also specified:
Border Layout (cont)
North
South
Border Layout
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame f = new Frame("TesBorderLayout");
f.setSize(200,200);
f.add("North", new Button("North"));
f.add("South", new Button("South"));
f.add("East", new Button("East"));
f.add("West", new Button("West"));
f.add("Center", new Button("Center"));
f.setVisible(true);
}
}
Grid Layout
The GridLayout class divides the region
into a grid of equally sized rows and
columns.
Components are added left-to-right, top-to-
bottom.
The number of rows and columns is
specified in the constructor for the
LayoutManager.
Grid Layout
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame f = new Frame("Test Grid Layout");
f.setSize(200,200);
GridLayout theLayout = new GridLayout(2,3);
f.setLayout(theLayout);
f.add(new Button("Button 1"));
f.add(new Button("Button 2"));
f.add(new Button("Button 3"));
f.add(new Button("Button 4"));
f.add(new Button("Button 5"));
f.setVisible(true);
}
}
What if I do not want a
LayoutManager?
LayoutManagers have
proved to be difficult
and frustrating to deal
with.
The LayoutManager
can be removed from a
Container by invoking
its setLayout method
with a null parameter.
What if I do not want a
LayoutManager? -Output
AWT Component Classes
AWT Component Classes
• AWT provides many ready-made and reusable
GUI components.
• The frequently-used are: Button, TextField,
Label, Checkbox, CheckboxGroup (radio
buttons), List, and Choice, as illustrated below.
AWT Component Classes
AWT GUI
Component: java.awt.Label
Adding Label Component
• Label is a passive control because it does not
create any event when accessed by the user.
• The label control is an object of Label.
• A label displays a single line of read-only text.
• However the text can be changed by the
application programmer but cannot be
changed by the end user in any way.
Adding Label Component
• A java.awt.Label provides a text description
message.
• Take note that System.out.println() prints to the
system console, not to the graphics screen.
• You could use a Label to label another
component (such as text field) or provide a text
description.
• Check the JDK API specification
for java.awt.Label.
Label constructors
• The Label class has three constructors:
–.
Field/Constants
• Following are the fields for
java.awt.Component class:
– static int CENTER -- Indicates that the label should be
centered.
– static int LEFT -- Indicates that the label should be left
justified.
– static int RIGHT -- Indicates that the label should be right
justified.