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

Java Frame and Event Handling Guide

The document provides an overview of the Frame class and its constructors, methods, and associated interfaces such as ActionListener, ItemListener, and WindowListener in Java's AWT. It includes examples demonstrating the use of these interfaces with components like buttons, checkboxes, and text fields, as well as layout managers like BorderLayout, GridLayout, FlowLayout, and CardLayout. Each layout manager is described with its constructors and example implementations for arranging components in a GUI application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

Java Frame and Event Handling Guide

The document provides an overview of the Frame class and its constructors, methods, and associated interfaces such as ActionListener, ItemListener, and WindowListener in Java's AWT. It includes examples demonstrating the use of these interfaces with components like buttons, checkboxes, and text fields, as well as layout managers like BorderLayout, GridLayout, FlowLayout, and CardLayout. Each layout manager is described with its constructors and example implementations for arranging components in a GUI application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Frame

Frame is a top-level container which is used to hold components in it. Components such as a
button, checkbox, radio button, menu, list, tableetc.
Constructors of Frame

Constructor Description
public Frame() Creates a Frame window with no name.
public Frame(String Creates a Frame window with a name.
name)

Frame methods

Methods Description
public void add(Component This method adds the component, comp,
comp) to the container Frame.
public void This method sets the layout of the
setLayout(LayoutManager components in a container, Frame.
object)
public void This method removes a component,
remove(Component comp, from the container, Frame.
comp)
public void setSize(int
This method sets the size of a Frame in
widthPixel, intheightPixel)
terms of pixels.

ActionListener Interface
ActionListener events notify when the user click the button. This button created with the help of
Button class.
Method of ActionListener interface

actionPerformed(ActionEvent ae) Called at button click event and ActionEventclass holds the
information of button source and can be recognised by getSource()
method.

Example of ActionListener in Frame

import [Link].*;
import [Link].*;
public class TestFrame extends Frame implements ActionListener
{
TextField tf1;
Button b1;
TestFrame()
{
[Link](new FlowLayout());
b1=new Button("ok");
tf1 = new TextField();
[Link](this);
add(tf1);
add(b1);
}

public void actionPerformed(ActionEvent ae)


{
if([Link]()==b1)
[Link]("hello");

public static void main(String s[])


{
TestFrame ob = new TestFrame();
[Link](true);
[Link](200,200);
}
}

ItemListener Interface
ItemListener class can be used for Checkbox, Choice and List controls. ItemListener events Notify
on selection of list or choice element and on checked or checked event of radio buttons. Method of
ItemListener interface:

itemStateChanged(ItemEvent) Called at checked or unchecked event of


Checkbox or on the selection of items of Choice or
List

Example of ItemListener in Frame with radio buttons


import [Link].*;
import [Link].*;
public class TestFrame1 extends Frame implements ItemListener
{
TextField tf1;
Checkbox cb1,cb2,cb3;
CheckboxGroup grp;
TestFrame1()
{
[Link](new FlowLayout());
grp = new CheckboxGroup();
cb1 = new Checkbox("car",grp,true);
cb2 = new Checkbox("bike",grp,false);
cb3 = new Checkbox("cycle",grp,false);
tf1 = new TextField(30);
[Link](this);
[Link](this);
[Link](this);
add(tf1);
add(cb1);
add(cb2);
add(cb3);
}
public void itemStateChanged(ItemEvent ae)
{
Checkbox cb = (Checkbox)[Link]();
if([Link]())
{
[Link]([Link]());
}
}

public static void main(String s[])


{
TestFrame1 ob = new TestFrame1();
[Link](true);
[Link](600,200);
}
}

Example of ItemListener in Frame with drop down

import [Link].*;
import [Link].*;
public class TestFrame2 extends Frame implements ItemListener
{
TextField tf1;
Choice ch;
TestFrame2()
{
[Link](new FlowLayout());
ch = new Choice();
[Link]("tea");
[Link]("coffee");
[Link]("cold drink");
tf1 = new TextField(30);
[Link](this);
add(tf1);
add(ch);
}
public void itemStateChanged(ItemEvent ae)
{
if([Link]()==ch)
{
[Link]([Link]());
// getSelectedIndex() method for index no.
}
}

public static void main(String s[])


{
TestFrame2 ob = new TestFrame2();
[Link](true);
[Link](600,200);
}
}

Window Listener Interface


Window event occurs when window related activities such as closing, activating or deactivating a
window are performed. Objects representing window events arecreated from WindowEvent class.

The class which processes the WindowEvent should implement this [Link] object of that
class must be registered with a component. The object can be registered using the
addWindowListener() method.

S.N. Method & Description

1 void windowActivated(WindowEvent e)
Invoked when the Window is set to be the active Window.

2
void windowClosed(WindowEvent e)
Invoked when a window has been closed as the result of calling dispose on thewindow.

3
void windowClosing(WindowEvent e)
Invoked when the user attempts to close the window from the window's systemmenu.

4
void windowDeactivated(WindowEvent e)
Invoked when a Window is no longer the active Window.
5
void windowDeiconified(WindowEvent e)
Invoked when a window is changed from a minimized to a normal state.

6
void windowIconified(WindowEvent e)
Invoked when a window is changed from a normal to a minimized state.

7
void windowOpened(WindowEvent e)
Invoked the first time a window is made visible.

Example on window Listener:

import [Link].*;
import [Link].*;
public class Frame1 extends Frame implements WindowListener
{
Label lb1;
Frame1()
{
lb1 = new Label("Welcome");
add(lb1);
[Link](this);
}
public void windowActivated(WindowEvent we)
{
[Link]("window activated");
}
public void windowDeactivated(WindowEvent we)
{
[Link]("window deactivated");
}
public void windowOpened(WindowEvent we)
{
[Link]("window opened");
}
public void windowClosed(WindowEvent we)
{
[Link]("window closed");
}
public void windowIconified(WindowEvent we)
{
[Link]("window iconifed");
}
public void windowDeiconified(WindowEvent we)
{
[Link]("window deiconified");
}
public void windowClosing(WindowEvent we)
{
[Link](); //..................for closing window
}
public static void main(String s[])
{
Frame1 ob = new Frame1();
[Link](true);
[Link](200,200);
}
}
Layout Manager
The LayoutManagers are used to arrange components in a particular manner. LayoutManager
is an interface that is implemented by all the classes of layout managers. There are few
following classes that representsthe layout managers:

1. [Link]
2. [Link]
3. [Link]
4. [Link]

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 frame or
window. The BorderLayout provides five constants for each region:

1. public static final int NORTH


2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER

Constructors of BorderLayout class:

o BorderLayout(): creates a border layout but with no gaps betweenthe components.

Example:

import [Link].*;
import [Link].*;
class BorderL extends Frame
{

BorderL()
{
setLayout(new BorderLayout());
Button b1=new Button("NORTH");
Button b2=new Button("SOUTH");
Button b3=new Button("EAST");
Button b4=new Button("WEST");
Button b5=new Button("CENTER");

add(b1,[Link]);
add(b2,[Link]);
add(b3,[Link]);
add(b4,[Link]);
add(b5,[Link]);
}
public static void main(String s[])
{
BorderL ob = new BorderL();
[Link](true);
[Link](200,200);
}
}

Java GridLayout
The GridLayout is used to arrange the components in rectangular grid. Onecomponent is displayed
in each rectangle.

Constructors of GridLayout class

1. GridLayout(): creates a grid layout with one column per componentin a row.
2. GridLayout(int rows, int columns): creates a grid layout with thegiven 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 alongwith given horizontaland vertical gaps.

import [Link].*;
import [Link].*;
class GridL extends Frame
{

GridL()
{
setLayout(new GridLayout(3,3));
Button b1=new Button("button1");
Button b2=new Button("button2");
Button b3=new Button("button3");
Button b4=new Button("button4");
Button b5=new Button("button5");
Button b6=new Button("button6");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
}
public static void main(String s[])
{
GridL ob = new GridL();
[Link](true);
[Link](200,200);
}
}
Java FlowLayout
The FlowLayout is used to arrange the components in a line, one afteranother (in a flow). It is the
default layout of applet or panel.

Constructors of FlowLayout class

1. FlowLayout(): creates a flow layout with centered alignment and adefault 5 unit horizontal
and vertical gap.

import [Link].*;
import [Link].*;
class FlowL extends Frame
{

FlowL()
{
setLayout(new FlowLayout());
Button b1=new Button("button1");
Button b2=new Button("button2");
Button b3=new Button("button3");
Button b4=new Button("button4");
Button b5=new Button("button5");
Button b6=new Button("button6");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
}
public static void main(String s[])
{
FlowL ob = new FlowL();
[Link](true);
[Link](200,200);
}
}

Java CardLayout
The 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 thatis why it is known as CardLayout.
Constructors of CardLayout class
1. CardLayout(): creates a card layout with zero horizontal and verticalgap.
Commonly used methods of CardLayout class
1. public void next(Container parent): is used to flip to the next card ofthe given
container.
2. public void previous(Container parent): is used to flip to the previouscard of the
given container.
3. public void first(Container parent): is used to flip to the first card ofthe given
container.
4. public void last(Container parent): is used to flip to the last card of thegiven
container.
5. public void show(Container parent, String name): is used to flip to thespecified
card with the given name.

import [Link].*;
import [Link].*;
class CardL extends Frame implements ActionListener
{
CardLayout C;
CardL()
{
C = new CardLayout();
setLayout(C);
Button b1=new Button("Card 1");
Button b2=new Button("Card 2");
Button b3=new Button("Card 3");
Button b4=new Button("Card 4");
Button b5=new Button("Card 5");
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);

}
public void actionPerformed(ActionEvent ae)
{
[Link](this);
}
public static void main(String s[])
{
CardL ob = new CardL();
[Link](true);
[Link](200,200); } }

You might also like