Unit 4 Chapter1
Unit 4 Chapter1
AWT Classes
The AWT classes are contained in the java.awt package.
The java.awt package provides classes for AWT API such as TextField, Label, TextArea, RadioButton,
CheckBox, Choice, List etc.
Components
All the elements like the button, text fields, scroll bars, etc. are called components.
In Java AWT, there are classes for each component as shown in above diagram.
In order to place every component in a particular position on a screen, we need to add them to
a container.
Component class is at the top of AWT hierarchy.
Types of containers:
There are four types of containers in Java AWT:
1. Window
2. Panel
3. Frame
4. Dialog
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.
It uses BorderLayout as default layout manager.
There are some methods associated with a frame class that are used to set its properties.
1. public void setLayout(LayoutManager lm) – It is used to set the layout that a frame
uses. A layout basically dictates the position of various components that are placed in it.
2. public void setSize(int width, int height) – It is used to set the intial size of the frame.
3. public void setVisible(boolean visibility) – It is used to set the visibility of the frame. It
must be set to true for the frame to be visible on the screen. By default, visiblity is false.
4. public void add(Component c) – It is used to place/add the components in the frame.
Example 2:
import java.awt.*;
class Sample extends Frame
{
Sample() //default
{
setVisible(true);
setSize(300,300);
setTitle("Welcome to Arrow");
}
public static void main(String args[])
{
// Creating the instance of Frame
Sample fr=new Sample();
}
}
Frame(String title)
Constructs a new, initially invisible Frame object with the specified title.
Example
import java.awt.*;
class Sample
{
public static void main(String args[])
{
// Creating the instance of Frame
Frame fr=new Frame();
fr.setVisible(true);
fr.setSize(300,300);
fr.setTitle("Welcome to Arrow");
}
}
3. Label(String text, int It constructs a label with the specified string and the specified alignment.
alignement)
1. void setText(String text) It sets the texts for label with the specified text.
2. void setAlignment(int alignment) It sets the alignment for label with the specified
alignment.
In the absence of a layout manager, the position and size of the components have to be set manually.
The setBounds() method is used in such a situation to set the position and size.
To specify the position and size of the components manually, the layout manager of the frame can
be null.
The setBounds(int x-axis, int y-axis, int width, int height) method is used in the above example that
sets the position of the awt label.
import java.awt.*;
class Sample
{
public static void main(String args[])
{
f.add(l1);
f.add(l2);
f.add(l3);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
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.
1. Button( ) It constructs a new button with an empty string i.e. it has no label.
2. Button (String text) It constructs a new button with given string as its label.
1. void setLabel (String label) It sets the label of button with the specified string.
Example:
import java.awt.*;
class ButtonExample
{
public static void main (String[] args)
{
Frame f = new Frame("Button Example");
b1.setBounds(100,100,100,50);
b2.setBounds(250,100,100,50);
f.add(b1);
f.add(b2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
2. TextField(String text) It constructs a new text field initialized with the given string text
to be displayed.
import java.awt.*;
class Sample
{
public static void main(String args[])
{
Frame f = new Frame("TextField Example");
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
Constructors of TextArea
Constructor Description
public TextArea() Creates a new TextArea.
public TextArea(String text) Creates a new TextArea with specified text.
Creates a new TextArea with specified number of rows and
public TextArea(int rows, int columns)
columns.
public TextArea(String text, int rows, int Creates a new TextArea with a text and a number of rows
columns) and columns.
import java.awt.*;
public class TextAreaExample
{
public static void main(String args[])
{
Frame f = new Frame();
TextArea area = new TextArea();
Label l1 = new Label("Enter Address");
area.setBounds(100, 30, 200, 100);
l1.setBounds(20, 50, 80, 30);
f.add(l1);
f.add(area);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
Constructors of Checkbox
Constructor Description
Checkbox(String text) Creates a checkbox with a text, this checkbox is unchecked by default..
import java.awt.*;
public class CheckboxExample1
{
public static void main (String args[])
{
Frame f = new Frame("Checkbox Example");
Checkbox checkbox1 = new Checkbox("C++",true);
checkbox1.setBounds(100, 100, 50, 50);
Checkbox checkbox2 = new Checkbox("Java");
checkbox2.setBounds(150, 100, 50, 50);
Checkbox checkbox3 = new Checkbox("Python");
checkbox3.setBounds(200, 100, 50, 50);
f.add(checkbox1);
f.add(checkbox2);
f.add(checkbox3);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
import java.awt.*;
class ChoiceExample1
{
public static void main(String args[])
{
Frame f = new Frame();
c.add("Item 1");
c.add("Item 2");
c.add("Item 3");
c.add("Item 4");
c.add("Item 5");
f.add(c);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
}
}
2. List(int row_num) It constructs a new scrolling list initialized with the given
number of rows visible.
import java.awt.*;
public class ListExample1 {
public static void main(String args[]) {
Frame f = new Frame();
List l1 = new List(3);
List l2 = new List(4,true);
l1.setBounds(100, 100, 75, 75);
l2.setBounds(200, 100, 75, 75);
l1.add("C");
l1.add("C++");
l1.add("Java");
l1.add("DCC");
l1.add("SEN");
l2.add("CSS");
l2.add("AJP");
l2.add("OSY");
l2.add("STE");
l2.add("EST");
f.add(l1);
f.add(l2);
public MenuBar() Creates a menu bar to which one or many menus are added.
//Creating Menu
Menu m1=new Menu("File");
m1.add(i1);
m1.add(i2);
m1.add(i3);
mb.add(m1);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
import java.awt.*;
class MenuExample
{
public static void main(String args[])
{
Frame f = new Frame("MenuBar, Menu and MenuItems");
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Layout Manager
The layout manager automatically positions all the components within the container.
If we do not use layout manager then also the components are positioned by the default layout
manager.
It is possible to layout the controls by hand but it becomes very difficult because of the following
two reasons.
It is very tedious to handle a large number of controls within the container.
Oftenly the width and height information of a component is not given when we need to
arrange them.
Java provide us with various layout manager to position the controls.
The properties like size,shape and arrangement varies from one layout manager to other layout
manager.
When the size of the applet or the application window changes, the size, shape and arrangement
of the components also changes in response i.e. the layout managers adapt to the dimensions
of appletviewer or the application window.
The layout manager is associated with every Container object.
- To apply Layout Manager, we use setLayout().
void setLayout(LayoutManager object);
- If you want to use setBounds() then please make sure that you have set null to setLayout() method.
Java BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south, east, west, and
center. Each region (area) may contain one component only. It is the default layout of a frame or
window. The BorderLayout provides five constants for each region:
o BorderLayout(): creates a border layout but with no gaps between the components.
o BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and
vertical gaps between the components.
import java.awt.*;
class BorderLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("BorderLayout Manager");
BorderLayout bl=new BorderLayout();
f1.setLayout(bl);
f1.add(b1,BorderLayout.NORTH);
f1.add(b2,BorderLayout.SOUTH);
f1.add(b3,BorderLayout.EAST);
f1.add(b4,BorderLayout.WEST);
f1.add(b5,BorderLayout.CENTER);
f1.setVisible(true);
f1.setSize(500,500);
}
}
import java.awt.*;
class BorderLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("BorderLayout Manager");
BorderLayout bl=new BorderLayout(20,20);
f1.add(b1,BorderLayout.NORTH);
f1.add(b2,BorderLayout.SOUTH);
f1.add(b3,BorderLayout.EAST);
f1.add(b4,BorderLayout.WEST);
f1.add(b5,BorderLayout.CENTER);
f1.setVisible(true);
f1.setSize(500,500);
}
}
import java.awt.*;
class BorderLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("BorderLayout Manager");
BorderLayout bl=new BorderLayout(20,20);
f1.setLayout(bl);
f1.setVisible(true);
f1.setSize(500,500);
}
}
FlowLayout
The Java FlowLayout class is used to arrange the components in a line, one after another (in a
flow). It is the default layout of the applet or panel.
Class constructors
1
FlowLayout()
Constructs a new FlowLayout with a centered alignment and a default 5-unit
horizontal and vertical gap.
2
FlowLayout(int align)
Constructs a new FlowLayout with the specified alignment and a default 5-unit
horizontal and vertical gap.
3
FlowLayout(int align, int hgap, int vgap)
Creates a new flow layout manager with the indicated alignment and the
indicated horizontal and vertical gaps.
f1.add(b1);
f1.add(b2);
f1.add(b3);
f1.setVisible(true);
f1.setSize(500,500);
}
}
Example 2
import java.awt.*;
class FlowLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("FlowLayout Manager");
// parameterized constructor is used
// where alignment is left
// horizontal gap is 20 units and vertical gap is 20 units.
f1.add(b1);
f1.add(b2);
f1.add(b3);
f1.add(b4);
f1.add(b5);
f1.add(b6);
f1.add(b7);
f1.add(b8);
f1.add(b9);
f1.add(b10);
f1.setVisible(true);
f1.setSize(500,500);
}
}
Java GridLayout
The Java GridLayout class is used to arrange the components in a rectangular grid. One component
is displayed in each rectangle.
1. GridLayout(): creates a grid layout with one column per component in a row.
2. GridLayout(int rows, int columns): creates a grid layout with the given rows and columns
but no gaps between the components.
3. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given
rows and columns along with given horizontal and vertical gaps.
The GridLayout() constructor creates only one row. The following example shows the usage of the
default constructor.
import java.awt.*;
class GridLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("GridLayout Manager");
GridLayout gl=new GridLayout();
f1.add(b1);
f1.add(b2);
f1.add(b3);
f1.add(b4);
f1.add(b5);
f1.add(b6);
f1.add(b7);
f1.add(b8);
f1.add(b9);
f1.add(b10);
f1.setVisible(true);
f1.setSize(500,500);
}
}
import java.awt.*;
class GridLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("GridLayout Manager");
GridLayout gl=new GridLayout(3,3);
f1.setLayout(gl);
Button b1=new Button("B1");
Button b2=new Button("B2");
Button b3=new Button("B3");
f1.add(b1);
f1.add(b2);
f1.add(b3);
f1.add(b4);
f1.add(b5);
f1.add(b6);
f1.add(b7);
f1.add(b8);
f1.add(b9);
f1.setVisible(true);
f1.setSize(500,500);
}
}
Example of GridLayout class: Using GridLayout(int rows, int columns, int hgap,
int vgap) Constructor
import java.awt.*;
class GridLayoutDemo
{
public static void main(String args[])
{
Frame f1 = new Frame("GridLayout Manager");
GridLayout gl=new GridLayout(3,3,20,30);
f1.setLayout(gl);
Button b1=new Button("B1");
Button b2=new Button("B2");
Button b3=new Button("B3");
Button b4=new Button("B4");
Button b5=new Button("B5");
Button b6=new Button("B6");
Button b7=new Button("B7");
Button b8=new Button("B8");
f1.add(b1);
f1.add(b2);
f1.add(b3);
f1.add(b4);
f1.add(b5);
f1.add(b6);
f1.add(b7);
f1.add(b8);
f1.add(b9);
f1.setVisible(true);
f1.setSize(500,500);
}
}
GridBagLayout
The java.awt.GridBagLayout layout manager is the most powerful and flexible of all the predefined
layout managers but more complicated to use. Unlike GridLayout where the component are
arranged in a rectangular grid and each component in the container is forced to be the same
size, in GridBagLayout, components are also arranged in rectangular grid but can have different
sizes and can occupy multiple rows or columns.
In order to create GridBagLayout, we first instantiate the GridBagLayout class by using its only
no-arg constructor and defining it as the current layout manager.
gridwidth, gridheight
Specify the number of columns (for gridwidth) or rows (for gridheight) in the component's
display area. These constraints specify the number of cells the component uses, not the
number of pixels it uses. The default value is 1.
ipadx, ipady
Specifies the internal padding: how much to add to the size of the component.
fill
Used when the component's display area is larger than the component's requested size to
determine whether and how to resize the component.
Valid values include NONE (the default), HORIZONTAL (make the component wide enough
to fill its display area horizontally, but do not change its height), VERTICAL (make the
component tall enough to fill its display area vertically, but do not change its width),
and BOTH (make the component fill its display area entirely).
import java.awt.*;
public class Sample extends Frame
{
public static void main(String[] args)
{
Frame fr = new Frame();
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
fr.setLayout(grid);
fr.setTitle("GridBag Layout Example");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
fr.add(new Button("Button One"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
fr.add(new Button("Button two"), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 1;
fr.add(new Button("Button Four"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
fr.add(new Button("Button Five"), gbc);
fr.setSize(300, 300);
fr.setVisible(true);
}
}
import java.awt.*;
class Sample
{
public static void main(String[] args)
{
Frame f1 = new Frame("Panels in Java Example");
FlowLayout fl = new FlowLayout();
f1.setLayout(fl);
f1.setSize(500,500);
f1.setBackground(Color.YELLOW);
f1.setVisible(true);
}
}
Class constructors
1 Dialog(Dialog owner)
Constructs an initially invisible, modeless Dialog with the specified owner
Dialog and an empty title.
Example
import java.awt.*;
public class DialogExample1
{
public static void main(String args[])
{
Frame f = new Frame();
Dialog d=new Dialog(f,"Dialog Box",true);
d.setLayout(new FlowLayout());
Button b = new Button ("OK");
Label l = new Label("Click button to continue.");
import java.awt.*;
import java.awt.event.*;
public class Sample extends Frame implements ActionListener
{
Dialog d;
Sample f;
Sample ()
{
d = new Dialog(f,"Dialog Example", true);
d.setLayout( new FlowLayout());
Button b = new Button ("OK");
b.addActionListener (this);
d.add( new Label ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public void actionPerformed( ActionEvent e )
{
d.setVisible(false);
}
public static void main(String args[])
{
Sample f = new Sample ();
}
}
The FileDialog is a subclass of Dialog class that displays a dialog window from which the user can
select a file.
Since it is a modal dialog, it blocks the rest of the application until the user has chosen a file.
There is no Layout Mangaer for FileDialog.
1 FileDialog(Frame parent)
Creates a file dialog for loading a file.
Field
Window
The window is the container that have no borders and menu bars.
It uses BorderLayout as default layout manager.
A top-level window is not contained within any other object; it sits directly on the desktop.
Generally, we won‘t create Window objects directly.
Instead, we will use a subclass of Window called frame or dialog for creating a window.