[go: up one dir, main page]

0% found this document useful (0 votes)
7 views10 pages

Unit 1

The document provides an overview of the Frame class in Java's AWT package, detailing its constructors, methods, and how to create a Frame. It also explains various Java LayoutManagers, including BorderLayout, FlowLayout, GridLayout, GridBagLayout, and CardLayout, along with their constructors and usage examples. Additionally, it includes sample code for creating Frames using both inheritance and association, demonstrating the implementation of different layouts.

Uploaded by

dncvpssmcbca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Unit 1

The document provides an overview of the Frame class in Java's AWT package, detailing its constructors, methods, and how to create a Frame. It also explains various Java LayoutManagers, including BorderLayout, FlowLayout, GridLayout, GridBagLayout, and CardLayout, along with their constructors and usage examples. Additionally, it includes sample code for creating Frames using both inheritance and association, demonstrating the implementation of different layouts.

Uploaded by

dncvpssmcbca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit-1GRAPHICSProgramming [L-08 M-

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.

Constructor of Frame class


 Frame(): It constructs a new Frame with no title that was initially
invisible.

public Frame()
 Frame(String title): It constructs a Frame with some title as specified
by the programmer.

public Frame(String title)


 Frame(String title,image icon): It constructs a frame with a title as
specified by the programmer as well as an image icon, as shown in
following screenshot.

public Frame(String title,image icon)


Methods of the Frame class
 setTitle(String value): This method provides a title to the Frame if not
specified at the time of object declaration of the Frame.

Frame object.setTitle("String value");


 String getTitle(): This method returns the title of the Frame.

String variable=Frame object.getTitle();


 setSize(int row,int col): This method sets the size of the Frame, row
and column wise.

void setSize(int row,int col);


 setVisible(boolean mode): It makes the Frame visible to us. If the
parameter ed is "true" then it will be visible otherwise if it is "False" then
the Frame will not be visible.
void setVisible(boolean mode);
How to Create a Frame in Java

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

// 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();
}
}
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();
}
}

Java LayoutManagers

The LayoutManagers are used to arrange components in a particular manner.


The Java LayoutManagers facilitates us to control the positioning and size
of the components in GUI forms. LayoutManager is an interface that is
implemented by all the classes of layout managers.

The 5 layouts available in the java.awt library are:

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

The Java GridLayout class is used to arrange the components in a rectangular


grid. One component is displayed in each rectangle.

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.

Constructors of GridLayout class


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.
4. GridBag Layout

The Java GridBagLayout class is used to align components vertically,


horizontally or along their baseline.

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

GridBag Layout(): The parameter less constructor is used to create a grid


bag layout manager.

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.

Constructors of CardLayout Class


1. CardLayout(): creates a card layout with zero horizontal and vertical
gap.
2. CardLayout(int hgap, int vgap): creates a card layout with the given
horizontal and vertical gap.
6. Box Layout

The Java BoxLayout class is used to arrange the components either


vertically or horizontally. For this purpose, the BoxLayout class provides four
constants. They are as follows:

Note: The BoxLayout class is found in javax.swing package.

Constructor of BoxLayout class


1. BoxLayout(Container c, int axis): creates a box layout that arranges
the components with the given axis.

Example of flow layout:

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

Example of Grid Layout :


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

Example of BorderLayout :
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
Example of Card Layout :
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:

Example of GridBag Layout :


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