SlideShare a Scribd company logo
QUESTION BANK
UNIT –I
Q.1 Write a Short note on “Event Listeners”?Explain the workingwith code specification.
Ans: 1. The Event listener represent the interfacesresponsibleto handleevents.Java providesusvariousEvent
listener classes
2. It is also known aseventhandler.Listeneris responsibleforgenerating responseto an event.
3. Fromjava implementation pointof view the listener is also an object.Listener waits until it receives an
event.Once theevent is received , the listener processthe event and then returns.
Code specification:
importjava.awt.*;
importjava.awt.event.*;//Using AWTeventsand listener interfaces
public class HELLO extendsFrameimplementsWindowListener{
HELLO() {
setLayout(new FlowLayout());
Button btnCount=newButton("ok");
add(btnCount);
addWindowListener(this);
setTitle("WindowEventDemo");
setSize(250, 100);
setVisible(true);
}
public static void main(String[] args){
HELLO h= newHELLO ();
}
public void windowClosing(WindowEvente) {
System.exit(0); //Terminatethe program
}
public void windowOpened(WindowEvente) { }
public void windowClosed(WindowEvente) { }
public void windowIconified(WindowEvente) { }
public void windowDeiconified(WindowEvente) { }
public void windowActivated(WindowEvente) { }
public void windowDeactivated(WindowEvente) { }
}
Q.2 How does AWTcreate radio buttons ? Explain with syntax & code specification.
Ans:  Radiobuttons givea more userfriendly environmentwhen theuserhasto select onlyone option
amongmany.
 There is no radio button classin java.awt package.
 AWT usesthe Checkbox classforboth check boxesand radio buttons.
 Radio buttonsarea group of checkboxesgrouped asoneunit.In the unit,if anotherradio button is
selected, theearlier gets automatically deselected.
 Becauseradio button isa check box,all the methodsillustrated in "Java AWTCheckbox" can beused
here.
 To create a radio button,you need to create check boxes,and add themto a checkbox group.
Code specification:
importjava.awt.*;
importjava.awt.event.*;
public class Demo3extendsFrameimplementsItemListener{
Label stLabel;
Demo3(){
setLayout(newFlowLayout());
CheckboxGroup fruitGroup=newCheckboxGroup();
Checkbox chkApple=new Checkbox("Apple",fruitGroup,true);
Checkbox chkMango =newCheckbox("Mango",fruitGroup,false);
Checkbox chkPeer= newCheckbox("Peer",fruitGroup,false);
add(chkApple);
add(chkMango);
add(chkPeer);
stLabel=newLabel();
add(stLabel);
chkApple.addItemListener(this);
chkMango.addItemListener(this);
chkPeer.addItemListener(this);
setTitle("WindowEventDemo");
setSize(250, 100);
setVisible(true);
}
public static void main(String[] args){
Demo3d=new Demo3();
}
public void itemStateChanged(ItemEvente) {
stLabel.setText(e.getItem()
+" Checkbox:"
+ (e.getStateChange()==1?"checked":"unchecked"));
}
}
Q.3 What is the defaultlayout of the frame?Explain the same.
Ans: 1. A containerhasa so-called layoutmanagerto arrangeitscomponents.Thelayoutmanagersprovidea
level of abstraction to map youruserinterface on all windowing systems,so thatthelayoutcan be
platform-independent.
2. AWT providesthefollowing layoutmanagers(in
packagejava.awt): FlowLayout, GridLayout, BorderLayout, GridBagLayout, BoxLayout, CardLayout,and
others.
3. DefaultLayoutof the frameis Border Layout
4. In java.awt.BorderLayout, thecontaineris divided into 5 zones:EAST, WEST, SOUTH, NORTH,
and CENTER.
5. Constructor:
 public BorderLayout();
 public BorderLayout(inthgap,intvgap);
6. Code Specification:
importjava.awt.*;
public class Demo extendsFrame{
Demo() {
setLayout(newBorderLayout(3,3));
ButtonbtnNorth= newButton("NORTH");
add(btnNorth, BorderLayout.NORTH);
ButtonbtnSouth= newButton("SOUTH");
add(btnSouth,BorderLayout.SOUTH);
ButtonbtnCenter= newButton("CENTER");
add(btnCenter,BorderLayout.CENTER);
ButtonbtnEast = newButton("EAST");
add(btnEast,BorderLayout.EAST);
ButtonbtnWest= newButton("WEST");
add(btnWest,BorderLayout.WEST);
setTitle("BorderLayoutDemo");//"this"Frame setstitle
setSize(280,150); // "this"Frame setsinitial size
setVisible(true); // "this"Frame shows
}
public static void main(String[] args) {
Demo d=new Demo();
}
}
Q.4 Write a java AWT program that creates the followingGUI( considerthe window closingevent)
Login Screen
Userid:
Passwd:
Ans:
importjava.awt.*;
public class LoginfrmextendsFrame implementsWindowListener{
Loginfrm(){
setLayout(new FlowLayout());
addWindowListener(this)
Label a = new Label("Userid :");
add(a);
TextField tf = newTextField("EnterUR name",10);
tf.setEditable(true);
add(tf);
Label b = newLabel("Passwd :");
add(b);
TextField tf1 = new TextField("EnterURname",5);
tf.setEditable(true);
add(tf1);
Button btn= new Button("SUBMIT");
add(btn);
Button btn1= newButton("CANCEL");
add(btn1);
setTitle("DemoForm");
setSize(300, 100);
setVisible(true);
}
public static void main(String[] args) {
Loginfrm d=new Loginfrm();
}
public void windowClosing(WindowEvente) {
System.exit(0); //Terminatethe program
}
public void windowOpened(WindowEvente) { }
public void windowClosed(WindowEvente) { }
public void windowIconified(WindowEvente) { }
CANCELSUBMIT
public void windowDeiconified(WindowEvente) { }
public void windowActivated(WindowEvente) { }
public void windowDeactivated(WindowEvente) { } }
Q.5 List differenttypesof layout manager ? ExplainGridLayout.
Ans: Sr.
No.
LayoutManager & Description
1
BorderLayout
The borderlayout arranges the components to fit in the five regions: east, west, north, south and
center.
2
CardLayout
The CardLayout object treats each component in the container as a card. Onlyone card is
visible at a time.
3
FlowLayout
The FlowLayout is the default layout.It layouts the components in a directionalflow.
4
GridLayout
The GridLayout manages the components in form of a rectangular grid.
5
GridBagLayout
This is the most flexible layout manager class.The object of GridBagLayout alignsthe
component vertically,horizontallyor along their baseline without requiringthe components of
same size.
GridLayout:
A GridLayoutobjectplacescomponentsin a grid of cells. Each componenttakesallthe availablespace within its
cell, and each cell is exactly the samesize. If the GridLayoutDemo window isresized, the GridLayoutobject
changesthecell size so thatthe cells are as large as possible,given the spaceavailableto the container.
GridLayoutDesign :
Q.6 Compare and contrast AWT& Swings
Ans:  AWT componentsarecalled HeavyWeight componentand Swingsarecalled ligth weightcomponent
becauseswing componentssitson thetop of AWT componentsand do thework.
 Swingscomponentsaremadein purely java and they are platformindependentwhereasAWT
compoentsareplatformdependent.
 We can havedifferentlookand feel in Swing whereasthis featureis notsupported in AWT.
 Swing hasmany advanced featureslikeJTabel,Jtabbed pane which isnot availablein awt
Q.7. Explainthe java EventDelegationmodel
Ans:
EventHandlingisthe mechanismthatcontrolsthe eventanddecideswhatshouldhappenif anevent
occurs. Thismechanismhas the code whichisknownas eventhandlerthatisexecuted whenanevent
occurs java Usesthe DelegationEventModel to handle the events. Thismodel definesthe standard
mechanismtogenerate andhandle the events.
The DelegationEventModel has the followingkeyparticipants namely:
 Source - The source is an objectonwhicheventoccurs.Source isresponsibleforprovidinginformation
of the occurredeventtoit's handler.Javaprovide aswithclassesforsource object.
 Listener- It isalsoknownas eventhandler.Listenerisresponsible forgeneratingresponse toanevent.
From javaimplementationpointof view the listenerisalsoanobject.Listenerwaitsuntilitreceivesan
event.Once the eventisreceived,the listenerprocessthe event andthenreturns.
The benefitof thisapproach isthat
 The user interface logiciscompletelyseparatedfromthe logicthatgeneratesthe event.
 The user interface elementisable todelegate the processingof aneventtothe separate piece of
code.
 In thismodel ,Listenerneedsto be registeredwiththe source objectsothat the listenercanreceive
the eventnotification.
 Thisis an efficientwayof handlingthe eventbecause the eventnotificationsare sentonlytothose
listenerthatwanttoreceive them.
Q.8. What are adapter classes? Whatare innerclasses? Explain withexamples.
Ans: Adapter Classes:
An adapterclass providesthedefaultimplementation of all methodsin an eventlistener interface.Adapter
classesare very usefulwhen you wantto processonly few of the eventsthatare handled by a particularevent
listenerinterface.You can definea new classby extending oneof theadapterclasses and implement only those
eventsrelevantto you.
Exampleof Adapter class:Here's a mouseadapterthatbeeps when themouseis clicked
importjava.awt.*;
importjava.awt.event.*;
public class MouseBeeperextendsMouseAdapter
{
public void mouseClicked(MouseEventevt)
{
Toolkit.getDefaultToolkit().beep();
}
}
By subclassing MouseAdapterratherthan implementing MouseListenerdirectly,you avoid having to writethe
methodsyou don'tactually need.You only overridethosethatyou plan to actually implement.
Inner Class:
Innerclasses are classwithin Class.Innerclass instancehasspecial relationship with Outer class.This special
relationship givesinner class accessto memberof outerclass asif they are thepart of outerclass.
Exampleof Inner class:
importjava.awt.Frame;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
public class FrameClosing3extendsFrame
{
publicFrameClosing3()
{
addWindowListener(newWindowAdapter()
{
public void windowClosing(WindowEvente)
{ System.exit(0);}
}
);
setTitle("Frame closing Style 3");
setSize(300, 300);
setVisible(true);
}
public static void main(String args[])
{
new FrameClosing3();
}
}
Q.9 What are differentoperationsthat can be carried out on frame windows?
Ans: A Frameis a top-levelwindowwitha title and a border.The size of the frameincludesany area designated for
the border.The dimensionsof theborderarea may be obtained using thegetInsetsmethod,however,since
these dimensionsareplatform-dependent,a valid insetsvaluecannotbe obtained untilthe frameis made
displayableby either calling packor show.The defaultlayoutfora frameis BorderLayout.
Operationsassociated withframe:
1. Add(Componentobj)
2. setLayout(LayoutManagerObject)
3. setSize(int,int)
4. setSize(Dimension ob)
5. setVisible(Boolean)
6. pack()/show()
Example:
importjava.awt.*;
public class NewMain extendsFrame{
NewMain(){
setLayout(new BorderLayout(3,3));
Button btnNorth=newButton("NORTH");
add(btnNorth,BorderLayout.NORTH);
Button btnSouth=newButton("SOUTH");
add(btnSouth,BorderLayout.SOUTH);
Button btnCenter= newButton("CENTER");
add(btnCenter,BorderLayout.CENTER);
Button btnEast= newButton("EAST");
add(btnEast,BorderLayout.EAST);
Button btnWest= new Button("WEST");
add(btnWest,BorderLayout.WEST);
setTitle("BorderLayoutDemo");//"this"Framesetstitle
setSize(280, 150); //"this" Framesets initial size
setVisible(true); //"this" Frameshows
}
public static void main(String[] args) {
NewMain nw=newNewMain();
}}

More Related Content

What's hot (20)

PDF
Spring 3: What's New
Ted Pennings
 
PPT
比XML更好用的Java Annotation
javatwo2011
 
PDF
Java Web Programming [5/9] : EL, JSTL and Custom Tags
IMC Institute
 
PDF
JSP Technology II
People Strategists
 
PPTX
Apache Struts 2 Advance
Emprovise
 
PDF
Hibernate III
People Strategists
 
PPT
JEE5 New Features
Haitham Raik
 
PPT
Spring 3.x - Spring MVC
Guy Nir
 
ODP
Java Spring MVC Framework with AngularJS by Google and HTML5
Tuna Tore
 
PPT
Struts course material
Vibrant Technologies & Computers
 
PPT
Java Server Faces (JSF) - advanced
BG Java EE Course
 
PDF
JSP Technology I
People Strategists
 
PDF
What's new in Android O
Kirill Rozov
 
PDF
Spring MVC Annotations
Jordan Silva
 
PPT
Spring MVC Basics
Bozhidar Bozhanov
 
PDF
Lecture 3: Servlets - Session Management
Fahad Golra
 
PPTX
Jpa 2.1 Application Development
ThirupathiReddy Vajjala
 
ODT
Types of Dependency Injection in Spring
Sunil kumar Mohanty
 
PDF
Servlets lecture1
Tata Consultancy Services
 
PPT
Chap4 4 1
Hemo Chella
 
Spring 3: What's New
Ted Pennings
 
比XML更好用的Java Annotation
javatwo2011
 
Java Web Programming [5/9] : EL, JSTL and Custom Tags
IMC Institute
 
JSP Technology II
People Strategists
 
Apache Struts 2 Advance
Emprovise
 
Hibernate III
People Strategists
 
JEE5 New Features
Haitham Raik
 
Spring 3.x - Spring MVC
Guy Nir
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Tuna Tore
 
Struts course material
Vibrant Technologies & Computers
 
Java Server Faces (JSF) - advanced
BG Java EE Course
 
JSP Technology I
People Strategists
 
What's new in Android O
Kirill Rozov
 
Spring MVC Annotations
Jordan Silva
 
Spring MVC Basics
Bozhidar Bozhanov
 
Lecture 3: Servlets - Session Management
Fahad Golra
 
Jpa 2.1 Application Development
ThirupathiReddy Vajjala
 
Types of Dependency Injection in Spring
Sunil kumar Mohanty
 
Servlets lecture1
Tata Consultancy Services
 
Chap4 4 1
Hemo Chella
 

Viewers also liked (20)

PPT
Pra taruna
Andre Wirasasmita
 
PPT
Internet of Things
Lokesh Singrol
 
DOCX
TY.BSc.IT Java QB U2
Lokesh Singrol
 
DOCX
Pat
Fred_macha
 
PPT
Παρουσίαση για πρόγραμμα του Γυμνασίου Σπηλίου
vaggreth
 
DOC
Amr Farag CV
Amr Farag
 
PDF
OSF BROCHURE FINAL ORIGINAL
Patricia McKenzie-Thomas
 
PDF
Toyota Industrial Equipment to Partner With I.D. Systems on New Wireless Vehi...
tartleader4357
 
PDF
Internship Report on Financial Statements Analysis of FSIBL by Moez Ansary
Moez Ansary
 
PPTX
майстер клас. 07
Marina Chesnokova
 
PDF
Internship Report on Financial Statements Analysis of FSIBL by Moez Ansary
Moez Ansary
 
PPTX
Community Development Through Cooperative in Ethiopia
sintayehu chokolu
 
DOCX
TY.BSc.IT Java QB U6
Lokesh Singrol
 
DOCX
TY.BSc.IT Java QB U5
Lokesh Singrol
 
PDF
HLW Report Shahnawaz Lodhi
Shahnawaz Lodhi B.Eng | EIT | CSSGB
 
PPT
Sosok pilihan
Andre Wirasasmita
 
PPTX
Computer institute website(TYIT project)
Lokesh Singrol
 
PPTX
HP Software Testing project (Advanced)
Lokesh Singrol
 
PDF
Project black book TYIT
Lokesh Singrol
 
Pra taruna
Andre Wirasasmita
 
Internet of Things
Lokesh Singrol
 
TY.BSc.IT Java QB U2
Lokesh Singrol
 
Παρουσίαση για πρόγραμμα του Γυμνασίου Σπηλίου
vaggreth
 
Amr Farag CV
Amr Farag
 
OSF BROCHURE FINAL ORIGINAL
Patricia McKenzie-Thomas
 
Toyota Industrial Equipment to Partner With I.D. Systems on New Wireless Vehi...
tartleader4357
 
Internship Report on Financial Statements Analysis of FSIBL by Moez Ansary
Moez Ansary
 
майстер клас. 07
Marina Chesnokova
 
Internship Report on Financial Statements Analysis of FSIBL by Moez Ansary
Moez Ansary
 
Community Development Through Cooperative in Ethiopia
sintayehu chokolu
 
TY.BSc.IT Java QB U6
Lokesh Singrol
 
TY.BSc.IT Java QB U5
Lokesh Singrol
 
HLW Report Shahnawaz Lodhi
Shahnawaz Lodhi B.Eng | EIT | CSSGB
 
Sosok pilihan
Andre Wirasasmita
 
Computer institute website(TYIT project)
Lokesh Singrol
 
HP Software Testing project (Advanced)
Lokesh Singrol
 
Project black book TYIT
Lokesh Singrol
 
Ad

Similar to TY.BSc.IT Java QB U1 (20)

PPTX
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
PDF
Advance java for bscit
YogeshDhamke2
 
PPT
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
PPT
engineeringdsgtnotesofunitfivesnists.ppt
sharanyak0721
 
PDF
The AWT and Swing
adil raja
 
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
kashyapneha2809
 
PPTX
GUI components in Java
kirupasuchi1996
 
PPT
Swing and Graphical User Interface in Java
babak danyal
 
PPT
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
PDF
28-GUI application.pptx.pdf
NguynThiThanhTho
 
PPTX
Jp notes
Sreedhar Chowdam
 
PDF
PraveenKumar A T AWS
Praveen Kumar
 
PDF
Gui
Sardar Alam
 
PDF
Session 9_AWT in java with all demonstrations.pdf
tabbu23
 
PPTX
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Salini P
 
PPTX
object oriented programming examples
Abdii Rashid
 
PPTX
Logic and Coding of Java Interfaces & Swing Applications
kjkleindorfer
 
PDF
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
Advance java for bscit
YogeshDhamke2
 
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
engineeringdsgtnotesofunitfivesnists.ppt
sharanyak0721
 
The AWT and Swing
adil raja
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
kashyapneha2809
 
GUI components in Java
kirupasuchi1996
 
Swing and Graphical User Interface in Java
babak danyal
 
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
28-GUI application.pptx.pdf
NguynThiThanhTho
 
PraveenKumar A T AWS
Praveen Kumar
 
Session 9_AWT in java with all demonstrations.pdf
tabbu23
 
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Salini P
 
object oriented programming examples
Abdii Rashid
 
Logic and Coding of Java Interfaces & Swing Applications
kjkleindorfer
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
Ad

More from Lokesh Singrol (10)

PDF
MCLS 45 Lab Manual
Lokesh Singrol
 
PDF
MCSL 036 (Jan 2018)
Lokesh Singrol
 
PPTX
Top Technology product failure
Lokesh Singrol
 
PPTX
Testing project (basic)
Lokesh Singrol
 
PPTX
Computer institute Website(TYIT project)
Lokesh Singrol
 
PPTX
Trees and graphs
Lokesh Singrol
 
PPTX
behavioral model (DFD & state diagram)
Lokesh Singrol
 
PPTX
Desktop system,clustered system,Handheld system
Lokesh Singrol
 
PPTX
Raster Scan display
Lokesh Singrol
 
PPT
Flash memory
Lokesh Singrol
 
MCLS 45 Lab Manual
Lokesh Singrol
 
MCSL 036 (Jan 2018)
Lokesh Singrol
 
Top Technology product failure
Lokesh Singrol
 
Testing project (basic)
Lokesh Singrol
 
Computer institute Website(TYIT project)
Lokesh Singrol
 
Trees and graphs
Lokesh Singrol
 
behavioral model (DFD & state diagram)
Lokesh Singrol
 
Desktop system,clustered system,Handheld system
Lokesh Singrol
 
Raster Scan display
Lokesh Singrol
 
Flash memory
Lokesh Singrol
 

Recently uploaded (20)

PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
community health nursing question paper 2.pdf
Prince kumar
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 

TY.BSc.IT Java QB U1

  • 1. QUESTION BANK UNIT –I Q.1 Write a Short note on “Event Listeners”?Explain the workingwith code specification. Ans: 1. The Event listener represent the interfacesresponsibleto handleevents.Java providesusvariousEvent listener classes 2. It is also known aseventhandler.Listeneris responsibleforgenerating responseto an event. 3. Fromjava implementation pointof view the listener is also an object.Listener waits until it receives an event.Once theevent is received , the listener processthe event and then returns. Code specification: importjava.awt.*; importjava.awt.event.*;//Using AWTeventsand listener interfaces public class HELLO extendsFrameimplementsWindowListener{ HELLO() { setLayout(new FlowLayout()); Button btnCount=newButton("ok"); add(btnCount); addWindowListener(this); setTitle("WindowEventDemo"); setSize(250, 100); setVisible(true); } public static void main(String[] args){ HELLO h= newHELLO (); } public void windowClosing(WindowEvente) { System.exit(0); //Terminatethe program } public void windowOpened(WindowEvente) { } public void windowClosed(WindowEvente) { } public void windowIconified(WindowEvente) { } public void windowDeiconified(WindowEvente) { } public void windowActivated(WindowEvente) { } public void windowDeactivated(WindowEvente) { }
  • 2. } Q.2 How does AWTcreate radio buttons ? Explain with syntax & code specification. Ans:  Radiobuttons givea more userfriendly environmentwhen theuserhasto select onlyone option amongmany.  There is no radio button classin java.awt package.  AWT usesthe Checkbox classforboth check boxesand radio buttons.  Radio buttonsarea group of checkboxesgrouped asoneunit.In the unit,if anotherradio button is selected, theearlier gets automatically deselected.  Becauseradio button isa check box,all the methodsillustrated in "Java AWTCheckbox" can beused here.  To create a radio button,you need to create check boxes,and add themto a checkbox group. Code specification: importjava.awt.*; importjava.awt.event.*; public class Demo3extendsFrameimplementsItemListener{ Label stLabel; Demo3(){ setLayout(newFlowLayout()); CheckboxGroup fruitGroup=newCheckboxGroup(); Checkbox chkApple=new Checkbox("Apple",fruitGroup,true); Checkbox chkMango =newCheckbox("Mango",fruitGroup,false); Checkbox chkPeer= newCheckbox("Peer",fruitGroup,false); add(chkApple); add(chkMango); add(chkPeer); stLabel=newLabel(); add(stLabel); chkApple.addItemListener(this); chkMango.addItemListener(this); chkPeer.addItemListener(this); setTitle("WindowEventDemo"); setSize(250, 100); setVisible(true); } public static void main(String[] args){ Demo3d=new Demo3(); } public void itemStateChanged(ItemEvente) { stLabel.setText(e.getItem() +" Checkbox:" + (e.getStateChange()==1?"checked":"unchecked")); } }
  • 3. Q.3 What is the defaultlayout of the frame?Explain the same. Ans: 1. A containerhasa so-called layoutmanagerto arrangeitscomponents.Thelayoutmanagersprovidea level of abstraction to map youruserinterface on all windowing systems,so thatthelayoutcan be platform-independent. 2. AWT providesthefollowing layoutmanagers(in packagejava.awt): FlowLayout, GridLayout, BorderLayout, GridBagLayout, BoxLayout, CardLayout,and others. 3. DefaultLayoutof the frameis Border Layout 4. In java.awt.BorderLayout, thecontaineris divided into 5 zones:EAST, WEST, SOUTH, NORTH, and CENTER. 5. Constructor:  public BorderLayout();  public BorderLayout(inthgap,intvgap); 6. Code Specification: importjava.awt.*; public class Demo extendsFrame{ Demo() { setLayout(newBorderLayout(3,3)); ButtonbtnNorth= newButton("NORTH"); add(btnNorth, BorderLayout.NORTH); ButtonbtnSouth= newButton("SOUTH"); add(btnSouth,BorderLayout.SOUTH); ButtonbtnCenter= newButton("CENTER"); add(btnCenter,BorderLayout.CENTER); ButtonbtnEast = newButton("EAST"); add(btnEast,BorderLayout.EAST); ButtonbtnWest= newButton("WEST"); add(btnWest,BorderLayout.WEST); setTitle("BorderLayoutDemo");//"this"Frame setstitle setSize(280,150); // "this"Frame setsinitial size setVisible(true); // "this"Frame shows } public static void main(String[] args) { Demo d=new Demo(); } }
  • 4. Q.4 Write a java AWT program that creates the followingGUI( considerthe window closingevent) Login Screen Userid: Passwd: Ans: importjava.awt.*; public class LoginfrmextendsFrame implementsWindowListener{ Loginfrm(){ setLayout(new FlowLayout()); addWindowListener(this) Label a = new Label("Userid :"); add(a); TextField tf = newTextField("EnterUR name",10); tf.setEditable(true); add(tf); Label b = newLabel("Passwd :"); add(b); TextField tf1 = new TextField("EnterURname",5); tf.setEditable(true); add(tf1); Button btn= new Button("SUBMIT"); add(btn); Button btn1= newButton("CANCEL"); add(btn1); setTitle("DemoForm"); setSize(300, 100); setVisible(true); } public static void main(String[] args) { Loginfrm d=new Loginfrm(); } public void windowClosing(WindowEvente) { System.exit(0); //Terminatethe program } public void windowOpened(WindowEvente) { } public void windowClosed(WindowEvente) { } public void windowIconified(WindowEvente) { } CANCELSUBMIT
  • 5. public void windowDeiconified(WindowEvente) { } public void windowActivated(WindowEvente) { } public void windowDeactivated(WindowEvente) { } } Q.5 List differenttypesof layout manager ? ExplainGridLayout. Ans: Sr. No. LayoutManager & Description 1 BorderLayout The borderlayout arranges the components to fit in the five regions: east, west, north, south and center. 2 CardLayout The CardLayout object treats each component in the container as a card. Onlyone card is visible at a time. 3 FlowLayout The FlowLayout is the default layout.It layouts the components in a directionalflow. 4 GridLayout The GridLayout manages the components in form of a rectangular grid. 5 GridBagLayout This is the most flexible layout manager class.The object of GridBagLayout alignsthe component vertically,horizontallyor along their baseline without requiringthe components of same size. GridLayout: A GridLayoutobjectplacescomponentsin a grid of cells. Each componenttakesallthe availablespace within its cell, and each cell is exactly the samesize. If the GridLayoutDemo window isresized, the GridLayoutobject changesthecell size so thatthe cells are as large as possible,given the spaceavailableto the container. GridLayoutDesign : Q.6 Compare and contrast AWT& Swings Ans:  AWT componentsarecalled HeavyWeight componentand Swingsarecalled ligth weightcomponent becauseswing componentssitson thetop of AWT componentsand do thework.  Swingscomponentsaremadein purely java and they are platformindependentwhereasAWT compoentsareplatformdependent.  We can havedifferentlookand feel in Swing whereasthis featureis notsupported in AWT.  Swing hasmany advanced featureslikeJTabel,Jtabbed pane which isnot availablein awt Q.7. Explainthe java EventDelegationmodel Ans: EventHandlingisthe mechanismthatcontrolsthe eventanddecideswhatshouldhappenif anevent
  • 6. occurs. Thismechanismhas the code whichisknownas eventhandlerthatisexecuted whenanevent occurs java Usesthe DelegationEventModel to handle the events. Thismodel definesthe standard mechanismtogenerate andhandle the events. The DelegationEventModel has the followingkeyparticipants namely:  Source - The source is an objectonwhicheventoccurs.Source isresponsibleforprovidinginformation of the occurredeventtoit's handler.Javaprovide aswithclassesforsource object.  Listener- It isalsoknownas eventhandler.Listenerisresponsible forgeneratingresponse toanevent. From javaimplementationpointof view the listenerisalsoanobject.Listenerwaitsuntilitreceivesan event.Once the eventisreceived,the listenerprocessthe event andthenreturns. The benefitof thisapproach isthat  The user interface logiciscompletelyseparatedfromthe logicthatgeneratesthe event.  The user interface elementisable todelegate the processingof aneventtothe separate piece of code.  In thismodel ,Listenerneedsto be registeredwiththe source objectsothat the listenercanreceive the eventnotification.  Thisis an efficientwayof handlingthe eventbecause the eventnotificationsare sentonlytothose listenerthatwanttoreceive them. Q.8. What are adapter classes? Whatare innerclasses? Explain withexamples. Ans: Adapter Classes: An adapterclass providesthedefaultimplementation of all methodsin an eventlistener interface.Adapter classesare very usefulwhen you wantto processonly few of the eventsthatare handled by a particularevent listenerinterface.You can definea new classby extending oneof theadapterclasses and implement only those eventsrelevantto you. Exampleof Adapter class:Here's a mouseadapterthatbeeps when themouseis clicked importjava.awt.*; importjava.awt.event.*; public class MouseBeeperextendsMouseAdapter { public void mouseClicked(MouseEventevt) { Toolkit.getDefaultToolkit().beep(); } } By subclassing MouseAdapterratherthan implementing MouseListenerdirectly,you avoid having to writethe methodsyou don'tactually need.You only overridethosethatyou plan to actually implement. Inner Class: Innerclasses are classwithin Class.Innerclass instancehasspecial relationship with Outer class.This special relationship givesinner class accessto memberof outerclass asif they are thepart of outerclass. Exampleof Inner class: importjava.awt.Frame; importjava.awt.event.WindowAdapter; importjava.awt.event.WindowEvent;
  • 7. public class FrameClosing3extendsFrame { publicFrameClosing3() { addWindowListener(newWindowAdapter() { public void windowClosing(WindowEvente) { System.exit(0);} } ); setTitle("Frame closing Style 3"); setSize(300, 300); setVisible(true); } public static void main(String args[]) { new FrameClosing3(); } } Q.9 What are differentoperationsthat can be carried out on frame windows? Ans: A Frameis a top-levelwindowwitha title and a border.The size of the frameincludesany area designated for the border.The dimensionsof theborderarea may be obtained using thegetInsetsmethod,however,since these dimensionsareplatform-dependent,a valid insetsvaluecannotbe obtained untilthe frameis made displayableby either calling packor show.The defaultlayoutfora frameis BorderLayout. Operationsassociated withframe: 1. Add(Componentobj) 2. setLayout(LayoutManagerObject) 3. setSize(int,int) 4. setSize(Dimension ob) 5. setVisible(Boolean) 6. pack()/show() Example: importjava.awt.*; public class NewMain extendsFrame{ NewMain(){ setLayout(new BorderLayout(3,3)); Button btnNorth=newButton("NORTH"); add(btnNorth,BorderLayout.NORTH); Button btnSouth=newButton("SOUTH"); add(btnSouth,BorderLayout.SOUTH); Button btnCenter= newButton("CENTER"); add(btnCenter,BorderLayout.CENTER); Button btnEast= newButton("EAST"); add(btnEast,BorderLayout.EAST); Button btnWest= new Button("WEST"); add(btnWest,BorderLayout.WEST);
  • 8. setTitle("BorderLayoutDemo");//"this"Framesetstitle setSize(280, 150); //"this" Framesets initial size setVisible(true); //"this" Frameshows } public static void main(String[] args) { NewMain nw=newNewMain(); }}