Ava AWT Tutorial

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

ava AWT Tutorial

Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface
(GUI) or windows-based 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).

The java.awt package provides classes for AWT API such


as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

Java AWT Hierarchy


The hierarchy of Java AWT classes are given below.

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.

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.

Useful Methods of Component Class

Method Description

public void add(Component c) Inserts a component on this component.

public void setSize(int width,int height) Sets the size (width and height) of the component.

public void setLayout(LayoutManager Defines the layout manager for the component.
m)

public void setVisible(boolean status) Changes the visibility of the component, by default
false.

Java AWT Example


To create simple AWT example, you need a frame. There are two ways to create a
GUI using Frame in AWT.

1. By extending Frame class (inheritance)


2. 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.

AWTExample1.java

// importing Java AWT class


import java.awt.*;

// extending Frame class to our class AWTExample1


public class AWTExample1 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);

// now frame will be visible, by default it is not visible


setVisible(true);
}

// main method
public static void main(String args[]) {

// creating instance of Frame class


AWTExample1 f = new AWTExample1();

Output:
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.

AWTExample2.java

// importing Java AWT class


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);

// setting the title of frame


f.setTitle("Employee info");

// no layout
f.setLayout(null);

// setting visibility of frame


f.setVisible(true);
}

// main method
public static void main(String args[]) {

// creating instance of Frame class


AWTExample2 awt_obj = new AWTExample2();

Output:
Layouts
 Layouts allow you to format components on the screen in a platform-independent
way. Without layouts, you would be forced to place components at explicit locations
on the screen, creating obvious problems for programs that need to run on multiple
platforms.
 Layout managers try to give programs a consistent and reasonable appearance,
regardless of the platform, the screen size, or actions the user might take.
 Every container has a LayoutManager that is responsible for positioning the
component objects within it, regardless of the platform or the screen size.
 Layout managers eliminate the need to compute component placement on your own,
which would be a losing proposition since the size required for any component
depends on the platform on which it is displayed.
The standard JDK provides 5 classes that implement the LayoutManager interface. They are
FlowLayout, GridLayout, BorderLayout, CardLayout, and GridBagLayout.

1. FlowLayout

The FlowLayout is the default layout for the Panel class, that includes its most famous
subclass, Applet. When there are too many components to put, they "wrap" to a new row,
similar to a word processor with word wrap enabled. When you add components to the
screen, they move left to right (centred within the applet) based upon the order added and the
width of the applet. If you resize an applet then the component's move will change based
upon the new width and height. The following shows an example of both before and after
resizing.

Here is the code of this program:

1. import java.awt.*;
2. import java.awt.event.*;
3. public class FLExample
4. {
5. public static void main(String[] args)
6. {
7. Frame frame= new Frame("FlowLayout Frame");
8. Panel pa= new Panel();
9. Button ba1= new Button();
10. Button ba2=new Button();
11. Button ba3=new Button();
12. Button ba4=new Button();
13. Button ba5=new Button();
14. frame.add(pa);
15. pa.setLayout(new FlowLayout());
16. pa.add(new Button("India"));
17. pa.add(new Button("Pakistan"));
18. pa.add(new Button("Japan"));
19. pa.add(new Button("China"));
20. pa.add(new Button("Countries"));
21. frame.setSize(300,300);
22. frame.setVisible(true);
23. frame.addWindowListener(new WindowAdapter()
24. {
25. public void windowClosing(WindowEvent e)
26. {
27. System.exit(0);
28. }
29. });
30. }
31. }

Output
2. GridLayout

You start at row one, column one, then move across the row until it's full, then continue on to
the next row. The GridLayout is widely used for arranging components in rows and columns.
GridLayout can reposition or resize objects after adding or removing components. As with
FlowLayout, the order in which you add components is relevant. However, unlike
FlowLayout, the underlying components are resized to adjust the row-column area, if
possible. Whenever the area is resized, the components within it are resized. The following
shows an example of both before and after resizing.

The same as FlowLayout, some changes exist in Line 3, Line 7 & Line 15.

Line 3: public class GLExample


Line 7: Frame frame= new Frame("GridLayout Frame");
Line 15: pa.setLayout(new GridLayout());

Output
3. BorderLayout

When you add a component to the layout, you must specify which area to place it
in. BorderLayout is one of the more unusual layouts provided. It is the default layout for
Window, along with its children, Frame and Dialog. BorderLayout provides 5 areas to hold
components. These areas are named after the four different borders of the screen, North,
South, East, and West, with any remaining space going into the Center area. The order in
which components are added to the screen is not important, although you can have only one
component in each area. The following shows a BorderLayout that has one button in each
area, before and after resizing.
Here is the code of this program:

The same as FlowLayout, some changes exist in Line 3, Line 7 & Line 15-21.

Line 3-public class BLExample


Line 7- Frame frame= new Frame("BorderLayout Frame");
Line 15-21
pa.setLayout(new BorderLayout());
pa.add(new Button("India"), BorderLayout.NORTH);
pa.add(new Button("Pakistan"), BorderLayout.SOUTH);
pa.add(new Button("Japan"), BorderLayout.EAST);
pa.add(new Button("China"), BorderLayout.WEST);
pa.add(new Button("Countries"), BorderLayout.CENTER);

Output
4. CardLayout

A CardLayout usually manages several components, displaying one of them at a time and
hiding the rest. CardLayout lets you assign names to the components it manages and lets
you jump to a component by name. With a little work, you can use the CardLayout to create
tabbed dialogue boxes or property sheets, that are not currently part of AWT. The
CardLayout is a bit on the strange side. All the components are given the same size.
Usually, the CardLayout manages a group of Panels (or some other container), and each
Panel contains several components of its own. You can also cycle through components in
order.
Here is the code of this program:

The same as FlowLayout, some changes exist in Line 3, Line 7 and Line 15:

Line 3-public class CLExample


Line 7: Frame frame= new Frame("CardLayout Frame");
Line 15: pa.setLayout(new CardLayout());

Output:
5. GridBagLayout

You provide all the details of each component through instances of the GridBagConstraints
class. GridBagLayout is the most sophisticated and complex of the layouts provided in the
development kit. With the GridBagLayout, you can organize components in multiple rows
and columns, stretch specific rows or columns when space is available, and anchor objects
in different corners. The following shows an example of a GridBagLayout.

The same as FlowLayout, some changes exist in Line3, Line 7 & Line 15.

Line 3-public class CBLExample


Line 7- Frame frame= new Frame("GridBagLayout Frame");
Line 15- pa.setLayout(new GridBagLayout());

Output

You might also like