Ava AWT Tutorial
Ava AWT Tutorial
Ava AWT Tutorial
Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface
(GUI) or windows-based applications in Java.
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.
Method Description
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.
AWTExample1.java
// creating a button
Button b = new Button("Click Me!!");
// no layout manager
setLayout(null);
// main method
public static void main(String args[]) {
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
// 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();
// no layout
f.setLayout(null);
// main method
public static void main(String args[]) {
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.
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.
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.
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:
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.
Output