Unit 1
Unit 1
14]
Frame:
Frame is a class of the java.awt package. A Frame is a resizable and movable
window with a title bar and maximize, minimize and close buttons. A Frame is
a container that can have MenuBars, MenuItems and some other components
like label, button, textfield, radio button and so on. A Frame is the most
important component of the Abstract Window Toolkit (AWT) that is treated as
a complete window for specific software.
public Frame()
Frame(String title): It constructs a Frame with some title as specified
by the programmer.
A Frame can be created in one of two ways. By adapting any of the following
two ways, we will be able to get a Frame. The two ways are:
1. By creating an object of the Frame class, in other words using
association.
2. By following the simple concept of inheritance i.e., by extending the
Frame class.
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
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
Java LayoutManagers
1. Border Layout
2. Flow Layout
3. Grid Layout
4. GridBag Layout
5. Card Layout
1. Border Layout:
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.
Constructors of BorderLayout class:
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.
2. Flow Layout
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
Constructors of FlowLayout class
1. FlowLayout(): creates a flow layout with centered alignment and a
default 5 unit horizontal and vertical gap.
2. FlowLayout(int align): creates a flow layout with the given alignment
and a default 5 unit horizontal and vertical gap.
3. FlowLayout(int align, int hgap, int vgap): creates a flow layout with
the given alignment and the given horizontal and vertical gap.
3. Grid Layout
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.
The components may not be of the same size. Each GridBag Layout object
maintains a dynamic, rectangular grid of cells. Each component occupies one
or more cells known as its display area. Each component associates an
instance of GridBag Constraints. With the help of the constraints object, we
arrange the component's display area on the grid. The GridBag Layout
manages each component's minimum and preferred sizes in order to
determine the component's size. GridBag Layout components are also
arranged in the rectangular grid but can have many different sizes and can
occupy multiple rows or columns.
Constructor
5. Java CardLayout
The Java CardLayout class manages the components in such a manner that
only one component is visible at a time. It treats each component as a card
that is why it is known as CardLayout.
1. import java.awt.*;
2. import java.awt.event.*;
3. public class FLOWExample
4. {
5. public static void main(String[] args)
6. {
7. Frame fm= 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. fm.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. fm.setSize(300,300);
22. fm.setVisible(true);
23. fm.addWindowListener(new WindowAdapter()
24. {
25. public void windowClosing(WindowEvent e)
26. {
27. System.exit(0);
28. }
29. });
30. }
31. }
Output
Output
Example of BorderLayout :
The same as FlowLayout, some changes exist in Line 3, Line 7 & Line 15-21.
Output
Example of Card Layout :
The same as FlowLayout, some changes exist in Line 3, Line 7 and Line 15:
Output:
Output