Sei sulla pagina 1di 46

Chapter 2- INTRODUCTION OF ABSTRACT

WINDOW TOOLKIT : (AWT)


Winter 2018

1.Give full form of HTTP and AWT. [2]


Ans:- HTTP: Hyper Text Transfer protocol.
AWT: Abstract Window Toolkit.

2. Compare Controls Button and JButton. [2]


Ans:- Button:
-> Buttons provide an important facility that is used to perform an event.
-> Button is a control component that generates events when it is pressed.
-> Button class extends the Component class and implements the Accessible interface.
-> Creation of Button:
Example:
Buton btn;
btn=new Button(“Button”);

JButton:

-> JButton class provided by Swing is totally different from Button which is provided by AWT.
-> It is used to add platform independent button in swing application.
-> Swing buttons are extended from the AbstractButton which implements
the Accessible interface.
-> Creation of Button:
Example:
JButon jbtn;
jbtn=new JButton(“JButton”);
3. Differentiate Between AWT and Swing. [No. of times asked in gtu -4] [2]
Ans:-
AWT SWING
AWT stands for Abstract windows toolkit. Swing is also called as JFC’s (Java Foundation
classes).
AWT components require java.awt package. Swing components require javax.swing package.
AWT components are heavyweight. Swing components are lightweight
AWT components are platform-dependent. Java swing components are platform-independent.
AWT supports limited number of GUI controls. Swing provides advanced GUI controls like Jtable,
JTabbedPane, etc.
More code is needed to implement AWT controls Less code is needed to implement SWING controls
functionality. functionality.
AWT doesn't support pluggable look and feel Swing supports pluggable look and feel.

4. Differentiate Flow Layout and Grid Layout. [3]


Ans:- Flow Layout:
->Used to arrange the components in a line, one after another (in a flow).
-> Default layout of applet or panel.
-> Arranges swing component from left to right until there’s no more space available.
->Then it begins a new row below it and moves from left to right again.
->To align the components in each row : FlowLayout.LEFT, FlowLayout.RIGHT &
FlowLayout.CENTER.

Grid Layout:

-> Used to arrange the components in rectangular grid inside the container.
->One component is displayed in each rectangle.
->Components added to the container in order from left to right.
5. Explain any one Swing component with example. [No. of times asked in gtu -4] [3]

Ans:- JButton:
->JButton class provides functionality of a button. It has 3 constuctors,
1.JButton(Icon ic)
2.JButton(String str)
3.JButton(String str, Icon ic)
->It allows a button to be created using icon, a string or both.
->JButton supports ActionEvent. When a button is pressed an ActionEvent is generated.

JLabel:

->JLabel is used to display a short string or an image icon. It has 4 constuctors,


1. JLabel()
2. JLabel(String s)
3. JLabel(Icon i)
4. JLabel(String s, Icon i, int horizontalAlignment)
-> JLabel is only a display of text or image and it cannot get focus.

Example:
import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
public class demo implements ActionListener
{
JFrame jf;
JButton jb;
JLabel jl;
demo()
{
jf=new JFrame();
jb=new JButton("Click ME");
jl=new JLabel("hello");

jf.add(jb);
jf.add(jl);

jf.setLayout(null);
jb.setBounds(200, 50, 80, 40);
jl.setBounds(200, 100, 150, 40);
jf.setVisible(true);
jf.setSize(500,300);
jb.addActionListener(this);

}
public static void main(String[] args) {
new demo();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Click ME"))
{
jl.setText("JButton is pressed");
}
}
}

6. Explain Canvas class with example. [No. of times asked in gtu -3] [3]
Ans:- Canvas:
->The canvas class implements a GUI object (Graphics object) that supports drawing.
->Canvas is a section of a window to draw graphics or display images.
->In other words, canvas Represents a rectangular area where application can draw something
or can receive inputs created by user.

Constructors of Canvas in AWT are as follows:


1.Canvas()
2. Canvas(GraphicsConviguration config)

Methods of Canvas in AWT are as follows:

1. Paint(Graphics g)
2. Update(Graphics g)
3. getAccessibleContext()

Example:

import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Color;

public class demo extends Applet


{
Canvas c;
public void init()
{
c = new Canvas();
c.setBackground(Color.red);
c.setSize(300, 300);
add(c);
}
}
7. Write a program to create a frame with one label in Applet. [3]
Ans:- Program:

import java.applet.Applet;
import java.awt.*;
public class demo extends Applet
{
Frame f;
Label l;

public void init()


{
f=new Frame();
l=new Label("Label in the frame");
f.add(l);
f.setVisible(true);
f.setSize(500,300);
}
}

8. Explain Border layout in Applet with example. [4]


Ans:-
->BorderLayout is used to arrange the components in five regions: north, south, east, west and
center.
-> BorderLayout is the default layout of frame or window.
->Constants for Regions: BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST,
BorderLayout.WEST & BorderLayout.CENTER.
->To add Component:
add(Component compObj, Object region);

Example:

import java.applet.Applet;
import java.awt.*;
public class demo extends Applet
{
Frame f;
Label l;
public void init()
{
f=new Frame();
f.setLayout(new BorderLayout());
l=new Label("Label in East");
f.add(l,BorderLayout.EAST);
f.setVisible(true);
f.setSize(500,300);
}
}
9. Describe java AWT class hierarchy. [No. of times asked in gtu -6] [4]
Ans:-
1.Component:
->Component class is at the top of AWT hierarchy.
->Component is an abstract class that encapsulates all attributes of visual component.
->Component is an object having a graphical representation that can be displayed on
the screen and that can interact with the user.
->Examples: buttons, checkboxes, list, scrollbars, etc.
->Useful methods of component class
1.public void add(Component c)
2.public void setSize(int width, int height)
3.public void setLayout(Layout Manager m)
4.public void setVisible(Boolean status)
2.Container:
->Container is a component that can contain other components like button textfields,
labels, etc in a specific layout.
->It is a subclass of component class.
->It keeps track of components that are added to another component.
->The classes that extends container class are known as container such as frame, dialog
and panel.

3.Window:
->Window is the container that has no borders and menu bars.
->It is rectangular area which is displayed on the screen.
->You must see frame, dialog or an another window for creating a window.
4.Panel:
->Panel is the container that doesn’t contain title bar and menu bars.
->It can have other components like button, textfield, etc.
->It is concrete subclass of container.
->The default LayoutManager of panel is flow layout.
5.Frame:
->Frame is the container that has title bar, menu bar, borders and resizing the corners.
->It is a top-level window & subclass if the window class.
->It can have other components like button, textfield, etc.
6.Dialog:
->Dialog is a window that takes input from the user.
->It is used to display message, list of options, etc.
->It has border and title.
10. Explain Event Handling in Java Applet. [No. of times asked in gtu -2] [4]
Ans:-
->Changing the state of an object is known as event. Event describes the change of any object.
->When the user interacts with a GUI application, an event is generated.
->In event-driven programming, a piece of event-handling codes is executed when an event has
been fired in response to a user input (such as clicking a mouse button or hitting the key).
->3 main components:
1.Events: An event is a change of state of an object.
2.Events Source: Event source is an object that generates an event.
3.Listeners: A listener is an object that listens the event. A listener gets notified when an
event occurs.

->There are different ways to handle event based on their type. Some of event that can be
generated in applet are listed below:
1.ActionEvent
2.KeyEvent
3.MouseEvent
4.MousemotionEvent
5.TextEvent
6.WindowEvent
7.ItemEvent
8.FocusEvent

Example:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class demo extends Applet implements ActionListener
{
Button btn;
Label l;

public void init()


{
btn=new Button("Click me");
l=new Label("Example of event handeling");
add(btn);
add(l);
btn.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Click me"))
{
l.setText("button is pressed");
}
}
}

11. Write an Applet that receives three numeric values as input from user and Print
largest value. [No. of times asked in gtu -2] [4]
Ans:- Program:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class demo extends Applet implements ActionListener
{
TextField tf1,tf2,tf3;
Button btn;
Label l;
public void init()
{
tf1=new TextField();
tf2=new TextField();
tf3=new TextField();
btn=new Button("Find Max");
l=new Label("Max no. is: ");

add(tf1);
add(tf2);
add(tf3);
add(btn);
add(l);

btn.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Find Max"))
{
int a,b,c;

a=Integer.parseInt(tf1.getText());
b=Integer.parseInt(tf2.getText());
c=Integer.parseInt(tf3.getText());
if(a>b && a>c)
{
l.setText("Max no. is:"+a);
}
else if(b>a && b>c)
{
l.setText("Max no. is:"+b);
}
else
{
l.setText("Max no. is:"+c);
}
}
}
}

12. Write a java Applet program to demonstrate Action Listener. [4]


Ans:- Program:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class demo extends Applet implements ActionListener
{
Button btn;
Label l;

public void init()


{
btn=new Button("Click me");
l=new Label("Example of event handeling");
add(btn);
add(l);
btn.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Click me"))
{
l.setText("button is pressed");
}
}
}
Summer 2018

1. List Constructors and Method of CANVAS in AWT. [2]


Ans:- Constructors of Canvas in AWT are as follows:
1.Canvas()
2. Canvas(GraphicsConviguration config)

Methods of Canvas in AWT are as follows:


1.Paint(Graphics g)
2.Update(Graphics g)
3.getAccessibleContext()

2. List Graphics class method available in Applet. [2]


Ans:-
1.public abstract void drawString(String str, int x, int y)
2. public void drawRect(int x, int y, int width, int height)
3. public abstract void fillRect(int x, int y, int width, int height)
4. public abstract void drawOval(int x, int y, int width, int height)
5. public abstract void fillOval(int x, int y, int width, int height)
6. public abstract void drawLine(int x1, int y1, int x2, int y2)
7. public abstract void setColor(Color c)
8. public abstract void setFont(Font font)

3. Write a GUI based application using Jlabel, JTextField and JButton that accept your
name and display it. [No. of times asked in gtu -3] [3]
Ans:- Program:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class demo extends Applet implements ActionListener
{
JTextField jtf1;
JButton jbtn;
JLabel jl;
public void init()
{
jtf1=new JTextField();
jbtn=new JButton("Submit");
jl=new JLabel("");

setLayout(null);
add(jtf1);
add(jbtn);
add(jl);

jtf1.setBounds(50, 50, 100, 20);


jbtn.setBounds(200, 50, 100, 20);
jl.setBounds(150, 100, 100, 20);
jbtn.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Submit"))
{
jl.setText(jtf1.getText());
}
}
}

4. Explain MouseEvent class in detail. [3]


Ans:-
->A MouseEvent is fired to all its registered listeners, when you press, release, or click a mouse-
button at the source object; or portion the mouse-pointer at and away from the source object.

Methods of MouseEvent Class:


1.getClickCount()
2.getPoint()
3.getX()
4.getY()

Example:

import java.awt.*;
import java.awt.event.*;
public class demo1 extends Frame implements MouseMotionListener
{
Label l1,l2;

demo1()
{
setLayout(null);
l1=new Label("x=");
l2=new Label("y=");
add(l1);
add(l2);
l1.setBounds(50, 50, 50, 50);
l2.setBounds(100, 50, 50, 50);
setVisible(true);
setSize(500,300);
addMouseMotionListener(this);

addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}

@Override
public void mouseDragged(MouseEvent e) {

@Override
public void mouseMoved(MouseEvent e)
{
l1.setText("x="+e.getX());
l2.setText("y="+e.getY());
}
public static void main(String[] args)
{
new demo1();
}
}

5. Differentiate CheckBox and RadioButton. [No. of times asked in gtu -4] [4]
Ans:-
6. List out Various Layout Managers? Explain any one with Example.
[No. of times asked in gtu -8] [4]

Ans:-
->Layout manager are used to arrange components within the container.
->By default, each container has a layout manager.
->setLayout() method is used to set layout.
->Types of Layout managers are as follows:
1.BorderLayout
2.CardLayout
3.FlowLayout
4.GridLayout
5.GridBagLayout
1.BorderLayout
->The BorderLayout manager divides the window container in five regions i.e. east,
west, north, south and center.
->It is used add method to add the component at specified region.
->Components of the BorderLayout Manager :

• BorderLayout.NORTH
• BorderLayout.SOUTH
• BorderLayout..EAST
• BorderLayout.WEST
• BorderLayout.CENTER

Example:
import java.awt.*;
import javax.swing.*;
public class BorderLayoutDemo
{
JFrame frame = new JFrame("BorderLayout");
JButton b1, b2, b3, b4, b5;
BorderLayoutDemo()
{
b1 = new JButton("NORTH");
b2 = new JButton("SOUTH");
b3 = new JButton("EAST");
b4 = new JButton("WEST");
b5 = new JButton("MIDDLE");
frame.add(b1, BorderLayout.NORTH);
frame.add(b2, BorderLayout.SOUTH);
frame.add(b3, BorderLayout.EAST);
frame.add(b4, BorderLayout.WEST);
frame.add(b5, BorderLayout.CENTER);
frame.setSize(250, 250);
frame.setVisible(true);
}
public static void main(String args[])
{
new BorderLayoutDemo();
}
}

2.CardLayout
->The CardLayout class contains several layouts in it.
->It Treat each component as a card.
->Only one card is visible a time.
->The Cardlayout manages the components in form of stack and provides visibility to
only one component at a time.
->Methods of card layout:
1.void first (Container a)
2.void last (Container a)
3.void next (Container a)
4.void previous (Container a)
5.void show (Container a, String card name)

Example:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Frame_CardLayout extends Applet implements ActionListener {

CardLayout cl;
Button bt1,bt2;
Panel p1,p2,main;
Label l1,l2;
public void init()
{
bt1=new Button("button 1");
bt2=new Button("button 2");
l1=new Label("1'st card");
l2=new Label("2'nd card");
setLayout(new BorderLayout());
add(bt1,BorderLayout.WEST);
add(bt2,BorderLayout.EAST);
p1=new Panel(new FlowLayout());
p1.add(l1);
p2=new Panel(new FlowLayout());
p2.add(l2);

cl=new CardLayout();
main=new Panel(cl);

main.add(p1,"firstCard");
main.add(p2,"secondCard");
add(main,BorderLayout.CENTER);

bt1.addActionListener(this);
bt2.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("button 1"))
{
cl.show(main, "firstCard");
}
if(e.getActionCommand().equals("button 2"))
{
cl.show(main, "secondCard");
}

}
}
3. Flow Layout:
->Used to arrange the components in a line, one after another (in a flow).
-> Default layout of applet or panel.
-> Arranges swing component from left to right until there’s no more space available.
->Then it begins a new row below it and moves from left to right again.
->To align the components in each row: FlowLayout.LEFT, FlowLayout.RIGHT &
FlowLayout.CENTER.

Example:
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Frame_FlowLayout extends Frame {

Frame f;

Frame_FlowLayout()
{
f=new Frame("Flow Layout");
Button bt=new Button("button");
f.setLayout(new FlowLayout());
f.add(bt);
bt.setSize(40,50);
f.setVisible(true);
f.setSize(500,300);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
}
public static void main(String[] args) {
new Frame_FlowLayout();
}

4. Grid Layout:

-> Used to arrange the components in rectangular grid inside the container.
->One component is displayed in each rectangle.
->Components added to the container in order from left to right.

Example:

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Frame_GridLayout extends Frame {

Frame f;

Frame_GridLayout()
{
f=new Frame("Flow Layout");
Button bt=new Button("button");
Button bt1=new Button("button1");
Button bt2=new Button("button2");
Button bt3=new Button("button3");
f.setLayout(new GridLayout(2,1));
f.add(bt);
f.add(bt1);
f.add(bt2);
f.add(bt3);
bt.setSize(40,50);
f.setVisible(true);
f.setSize(500,300);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new Frame_GridLayout();
}

}
7. Create an application that display a frame with a Menubar when a user selects any
Menu or Menu item, display that selection on a text area in the center of the Frame. [4]
Ans:-Program:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class p9 extends Applet implements ActionListener
{
Frame f;
MenuBar mb;
Menu m,xt;
MenuItem n,op,ex,w,t,d;

TextArea tx;
public static void main(String[] args)
{
p9 md= new p9();
}
public p9()
{
f=new Frame("Welcome to menu demo");
f.setLayout(null);

tx=new TextArea(5, 30);


f.add(tx);
tx.setBounds(800,400,300,200);
mb=new MenuBar();

m=new Menu("File");
n=new MenuItem("New");
op=new MenuItem("Open");
ex=new MenuItem("Exit");
xt=new Menu("Extra");
w=new MenuItem("What");
t=new MenuItem("To");
d=new MenuItem("Do");

m.add(n);
m.add(op);
m.addSeparator();
m.add(xt);
m.addSeparator();
m.add(ex);

xt.add(w);
xt.add(t);
xt.add(d);

mb.add(m);
f.setMenuBar(mb);

n.addActionListener(this);
op.addActionListener(this);
ex.addActionListener(this);
w.addActionListener(this);
t.addActionListener(this);
d.addActionListener(this);

f.setSize(500,300);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public c void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("New"))
{
tx.setText("New is Pressed");

}
if(e.getActionCommand().equals("Open"))
{
tx.setText("Open is Pressed");

}
if(e.getActionCommand().equals("Extra"))
{
tx.setText("Extra is Pressed");

}
if(e.getActionCommand().equals("What"))
{
tx.setText("What is Pressed");

}
if(e.getActionCommand().equals("To"))
{
tx.setText("To is Pressed");

}
if(e.getActionCommand().equals("Do"))
{
tx.setText("Do is Pressed");

}
if(e.getActionCommand().equals("Exit"))
{
tx.setText("Exit is Pressed");

}
}
}
8. Write an applet that contains two RadioButton and 40×40 pixel CANVAS. The Two
RadioButton should be labeled with Red and Green. When a user selects any
RadioButton, change that selection of color in the Canvas. [4]
Ans:-
Program:

import java.awt.*;
import java.awt.event.*;
public class demo1 implements ItemListener
{
Frame f;
CheckboxGroup cbg;
Checkbox cb1,cb2;
Canvas c;

demo1()
{
f=new Frame();
cbg=new CheckboxGroup();
cb1=new Checkbox("Red", cbg, false);
cb2=new Checkbox("Green", cbg, false);
c=new Canvas();

f.add(cb1);
f.add(cb2);
f.add(c);

cb1.setBounds(100,50,50,50);
cb2.setBounds(150, 50, 50, 50);
c.setBounds(30, 100,500, 300);

f.setLayout(null);
f.setSize(500,300);
f.setVisible(true);

cb1.addItemListener(this);
cb2.addItemListener(this);

f.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

}
public static void main(String[] args) {
new demo1();
}

@Override
public void itemStateChanged(ItemEvent e) {
if(e.getSource().equals(cb1))
{
c.setBackground(Color.red);
}
if(e.getSource().equals(cb2))
{
c.setBackground(Color.GREEN);
}
}
}
Winter 2017

1. List out AWT controls in JAVA. [2]


Ans:- 1.Label
2.Button
3.Check box
4.Check box Group
5.List
6.Text Field
7.Text area
8.Choice
9.Canvas
10.Image
11.Scroll bar
12.Dialog
13.File Dialog

2. Compare Label and JLabel controls. [2]


Ans:-
3. List out types of Event classes. [No. of times asked in gtu -2] [2]
Ans:-
1.ActionEvent
2.MouseEvent
3.KeyEvent
4.ItemEvent
5.TextEvent
6.MouseWheelEvent
7.WindowEvent
8.ComponentEvent
9.ContainerEvent
10.AdjustmentEvent
11.FocusEvent

4. Explain event listeners in JAVA. [3]


Ans:-
->The Event listeners represent the interfaces which are responsible to handle a particular
event.
->Java have provided us various Event listeners classes and Every method of an event listener
has a single and only argument as an object which is the subclass of EventObject class.
Requirements:
1.It should be registered to one more source object to receive event notification.
2.It must implement methods to receive and process these notifications.
->Differrent types of event listeners are listed below:
Summer 2017

1. List any four methods provided by Component in Java. [2]


Ans:-
1. public void add(Component c)
2. public void setSize(int width,int height)
3. public void setLayout(LayoutManager m)
4. public void setVisible(boolean status)

2. State why an adapter class is useful. [2]


Ans:-
->Java adapter classes provide the default implementation of listener interfaces. If you inherit
the adapter class, you will not be forced to provide the implementation of all the methods of
listener interfaces. So it saves code.

3. Define Frame and state two ways to create a frame. [2]


Ans:-
Frame:
-> A frame has title, border and menu bars.
->It can contain several components like buttons, text fields, scrollbars etc.
->This is most widely used container while developing an application in AWT.

We can create a GUI using Frame in two ways:


1) By extending Frame class
2) By creating the instance of Frame class

4. Write the name of class used to create input Dialog box in Java. [2]
Ans:- JOptionPane

5. Explain panel in java with example. [3]


Ans:- Panel:
->Panel is the container that doesn’t contain title bar and menu bars.
->It can have other components like button, textfield, etc.
->It is concrete subclass of container.
->The default LayoutManager of panel is flow layout.

Example:

import java.applet.Applet;
import java.awt.*;
public class Panel_Example extends Applet
{
Panel p;
Label l;
public void init()
{
p=new Panel();
l=new Label("In Panel");
p.add(l);
add(p);
p.setBackground(Color.red);
}
}

6. Write an applet that receives three numeric values as input from user and display the
average of three on screen. [No. of times asked in gtu -2] [3]
Ans:- Program:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class demo1 extends Applet implements ActionListener
{
TextField tf1,tf2,tf3;
Button btn;
Label l;
public void init()
{
tf1=new TextField();
tf2=new TextField();
tf3=new TextField();
btn=new Button("Find Average");
l=new Label("Average: ");

add(tf1);
add(tf2);
add(tf3);
add(btn);
add(l);

btn.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Find Average"))
{
int a,b,c,d;
a=Integer.parseInt(tf1.getText());
b=Integer.parseInt(tf2.getText());
c=Integer.parseInt(tf3.getText());
d=(a+b+c)/3;

l.setText("Average:"+d);
}
}
}

7. Write an applet that receives one string and display in reverse fashion(e.g. “gujarat”
should be display “tarajug”). [3]
Ans:- Program:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class demo1 extends Applet implements ActionListener
{
TextField tf1;
Button btn;
Label l;
public void init()
{
tf1=new TextField();
btn=new Button("Reverse String");
l=new Label("In Reverse: ");
add(tf1);
add(btn);
add(l);
btn.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Reverse String"))
{
String s=tf1.getText();
String rev="";
for(int i=s.length()-1;i>=0;i--)
{
rev=rev+s.charAt(i);
}
l.setText("In Reverse: "+rev);
}
}
}
8. Explain event delegation model.
or
Explain Event Handling in Java Applet. [4]
Ans:-
->Changing the state of an object is known as event. Event describes the change of any object.
->When the user interacts with a GUI application, an event is generated.
->In event-driven programming, a piece of event-handling codes is executed when an event has
been fired in response to a user input (such as clicking a mouse button or hitting the key).
->3 main components:
1.Events: An event is a change of state of an object.
2.Events Source: Event source is an object that generates an event.
3.Listeners: A listener is an object that listens the event. A listener gets notified when an
event occurs.

Example:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class demo extends Applet implements ActionListener
{
Button btn;
Label l;

public void init()


{
btn=new Button("Click me");
l=new Label("Example of event handeling");
add(btn);
add(l);
btn.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Click me"))
{
l.setText("button is pressed");
}
}
}
9. Describe mouse listener and mouse events using proper example.
[No. of times asked in gtu -2] [4]
Ans:-
->Mouse events are of two types. MouseListener handles the events when the mouse is not in
motion. While MouseMotionListener handles the events when mouse is in motion.
-> MouseListener and MouseMotionListener is an interface in java.awt.event package.

-> MouseListener methods are listed below :


1.void mouseReleased(MouseEvent e) : Invoked when Mouse key is released
2.void mouseClicked(MouseEvent e) : Invoked when Mouse key is pressed/released
3.void mouseExited(MouseEvent e) : Invoked when Mouse exited the component
4.void mouseEntered(MouseEvent e) : Invoked when Mouse entered the component
5.void mousepressed(MouseEvent e) : Invoked when Mouse key is pressed

-> MouseMotionListener methods are listed below :


1.void mouseDragged(MouseEvent e) : Invoked when a mouse button is pressed in the
component and dragged. Events are passed until the user releases the mouse button.
2.void mouseMoved(MouseEvent e) : invoked when the mouse cursor is moved from
one point to another within the component, without pressing any mouse buttons.

Example:

import java.awt.*;
import java.awt.event.*;
public class demo1 extends Frame implements MouseListener
{
Label l1;

demo1()
{
setLayout(null);
l1=new Label();
add(l1);
l1.setBounds(50, 50, 150, 50);
setVisible(true);
setSize(500,300);
addMouseListener(this);

addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
@Override
public void mouseClicked(MouseEvent e) {
l1.setText("mouse clicked");
}

@Override
public void mousePressed(MouseEvent e) {
l1.setText("mouse pressed");
}

@Override
public void mouseReleased(MouseEvent e) {
l1.setText("mouse released");
}

@Override
public void mouseEntered(MouseEvent e) {
l1.setText("mouse entered");
}

@Override
public void mouseExited(MouseEvent e) {
l1.setText("mouse exited");
}

public static void main(String[] args)


{
new demo1();
}


Check it its possibily wrong question
10. Write an applet that tracks the position of mouse when it is dragged or moved. Draw
a 10 x 10 pixel rectangle filled with green at the current mouse position.
[No. of times asked in gtu -2] [4]
Ans:- Program:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class p4 extends Applet implements MouseMotionListener {

Label l;
int x,y;
public void init()
{
l=new Label();
add(l);
setLayout(null);
l.setBounds(x, y, 100, 10);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
g.setColor(Color.GREEN);
g.fillRect(x, y, 10, 10);
}
@Override
public void mouseDragged(MouseEvent e) {
mouseMoved(e);
}
@Override
public void mouseMoved(MouseEvent e) {
x=e.getX();
y=e.getY();
l.setText(x+" "+y);
repaint();
}
}
11. Write an applet that contains a check box group with three check boxes labeled “Red”,
“Green” and “Blue”. There is also a 30 x 30 pixel canvas. The selections of the check boxes
determine the color of the canvas. [No. of times asked in gtu -2] [4]
Ans:- Program:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class p8 extends Applet implements ItemListener {


Canvas c;
Checkbox c1;
Checkbox c2;
Checkbox c3;
public void init()
{
c=new Canvas();
c1=new Checkbox("Red");
c2=new Checkbox("Blue");
c3=new Checkbox("Green");
add(c);
add(c1);
add(c2);
add(c3);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c.setBackground(Color.black);
c.setSize(30,30);
}
@Override
public void itemStateChanged(ItemEvent e)
{
int r=0,g=0,b=0;
if(c1.getState())
{
r=255;
}
if(c2.getState())
{
b=255;
}
if(c3.getState())
{
g=255;
}
c.setBackground(new Color(r,g,b));
}
}
12. Explain the process of creating a frame and adding a button to it. [4]
Ans:-
Winter 2016
1. List out Layout Manager. [No. of times asked in gtu -2] [2]
Ans:-
->Types of Layout managers are as follows:
1.BorderLayout
2.CardLayout
3.FlowLayout
4.GridLayout
5.GridBagLayout

2. Explain mouseReleased method of mouse listener. [2]


Ans:-
mouseReleased:

void mouseReleased(MouseEvent e)
->Invoked when a mouse button has been released on a component.



3. Explain JButton AWT control in java. [3]


Ans:- JButton:
->JButton class provides functionality of a button. It has 3 constuctors,
1.JButton(Icon ic)
2.JButton(String str)
3.JButton(String str, Icon ic)
->It allows a button to be created using icon, a string or both.
->JButton supports ActionEvent. When a button is pressed an ActionEvent is generated.

Syntax:
JButton jbtn=new JButton(“String”);

4. Explain JComboBox AWT control in java. [3]


Ans:-
JComboBox

->JComboBox is a part of Java Swing package.


-> JComboBox shows a popup menu that shows a list and the user can select a
option from that specified list .
->JComboBox can be editable or read- only depending on the choice of the
programmer .
Commonly used methods are:
1.addItem(E item) : adds the item to the JComboBox
2. addItemListener( ItemListener l) : adds a ItemListener to JComboBox
3. getSelectedItem() : returns the item which is selected
4. setEditable(boolean b) : the boolean b determines whether the combo box is
editable or not .If true is passed then the combo box is editable or vice versa.
5. removeAllItems(): removes all items from the item list.
6. getItemCount() : returns the number of items in the list.

Example:

import java.applet.Applet;
import javax.swing.*;
public class demo1 extends Applet
{
JComboBox jcb;
public void init()
{
jcb=new JComboBox();
jcb.addItem("MCOM");
jcb.addItem("AJAVA");
jcb.addItem("NMA");
add(jcb);
}
}

5. Difference between component and container class in java. [3]


Ans:-
Component:

->The Component class is found under java.awt package.


->All non-menu-related elements that comprise a graphical user interface are derived
from the abstract class Component.
-> The Component class defines a number of of methods for handling events, changing
window bounds, controlling fonts and colors, and drawing components and their
content.

Container:

->The container class is the subclass of Component class.


->A container is a component that can accommodate other components.
->Containers provide support for building complex graphical user interface.
->Container provides the method add () to include components in the container.
6. Write a brief note on AWT WindowEvent Class. [4]
Ans:-
->WindowEvent Class extends ComponentEvent.
-> The object of this class represents the change in state of a window.
-> This low-level event is generated by a Window object when it is opened, closed, activated,
deactivated, iconified, or deiconified, or when focus is transfered into or out of the Window.
-> The event is passed to every WindowListener or WindowAdapter object which registered to
receive such events using the window's addWindowListener method.
-> Each such listener object gets this WindowEvent when the event occurs.

Example:

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class demo1


{
Frame f;
demo1()
{
f=new Frame();
f.setLayout(new FlowLayout());
f.add(new Label("hello"));
f.setVisible(true);
f.setSize(500,300);
f.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}

});
}
public static void main(String[] args) {
new demo1();
}
}
Summer 2016
1. List out Swing component classes. [2]
Ans:- 1.JTextfield, JCheckBox, JRadioButton
2.JComboBox
3.JLabel
4.JMenuBar
5.JOptionPane
6.JPanel
7.JScrollBar
8.JProgressBar
9.JToggleButton
10.JList

2. List out Interfaces of Event Listener [2]


Ans:-
3. Explain AWT label control. [3]
Ans:-
->The object of Label class is a component for placing text in a container.
->It is used to display a single line of read only text.
->The text can be changed by an application but a user cannot edit it directly.

Example:
import java.awt.*;
class LabelExample
{
public static void main(String args[])
{
Frame f= new Frame("Label Example");
Label l1,l2;
l1=new Label("First Label.");
l1.setBounds(50,100, 100,30);
l2=new Label("Second Label.");
l2.setBounds(50,150, 100,30);
f.add(l1);
f.add(l2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

4. Explain AWT push buttons control. [3]


Ans:-
-> Buttons provide an important facility that is used to perform an event.
-> Button is a control component that generates events when it is pressed.
-> Button class extends the Component class and implements the Accessible interface.

Syntax for adding a button component:


Button btn=new Button(“Click Me”);
add(btn);

Example:

import java.awt.*;
class demo1
{
public static void main(String args[])
{
Frame f= new Frame("Label Example");
Button btn;
btn=new Button("Button");
btn.setBounds(50,100, 100,30);
f.add(btn);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

5. Explain Mouse Listener. [3]


Ans:-
->MouseListener handles the events when the mouse is not in motion.
-> MouseListener methods are listed below :
1.void mouseReleased(MouseEvent e) : Invoked when Mouse key is released
2.void mouseClicked(MouseEvent e) : Invoked when Mouse key is pressed/released
3.void mouseExited(MouseEvent e) : Invoked when Mouse exited the component
4.void mouseEntered(MouseEvent e) : Invoked when Mouse entered the component
5.void mousepressed(MouseEvent e) : Invoked when Mouse key is pressed

Example:

import java.awt.*;
import java.awt.event.*;
public class demo1 extends Frame implements MouseListener
{
Label l1;

demo1()
{
setLayout(null);
l1=new Label();
add(l1);
l1.setBounds(50, 50, 150, 50);
setVisible(true);
setSize(500,300);
addMouseListener(this);

addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
@Override
public void mouseClicked(MouseEvent e) {
l1.setText("mouse clicked");
}

@Override
public void mousePressed(MouseEvent e) {
l1.setText("mouse pressed");
}

@Override
public void mouseReleased(MouseEvent e) {
l1.setText("mouse released");
}

@Override
public void mouseEntered(MouseEvent e) {
l1.setText("mouse entered");
}

@Override
public void mouseExited(MouseEvent e) {
l1.setText("mouse exited");
}

public static void main(String[] args)


{
new demo1();
}

6. Write a program that has one button in the frame, clicking on the button cycles
through the colors of background: red->green>blue and so on.
[No. of times asked in gtu -2] [7]
Ans:- Program:

import java.awt.*;
import java.awt.event.*;
public class p7 implements ActionListener{

Button b;
Frame f;
p7()
{
f=new Frame("Welcome to frame");
b= new Button("Click me");
f.add(b);
f.setSize(500,300);
f.setVisible(true);
f.setBackground(Color.red);
f.setLayout(new FlowLayout());
b.addActionListener(this);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
}

@Override
public void actionPerformed(ActionEvent e) {
if(f.getBackground().equals(Color.red))
{
f.setBackground(Color.green);
}
else if(f.getBackground().equals(Color.green))
{
f.setBackground(Color.blue);
}
else if(f.getBackground().equals(Color.blue))
{
f.setBackground(Color.YELLOW);
}
else if(f.getBackground().equals(Color.YELLOW))
{
f.setBackground(Color.MAGENTA);
}
else if(f.getBackground().equals(Color.MAGENTA))
{
f.setBackground(Color.ORANGE);
}
else if(f.getBackground().equals(Color.ORANGE))
{
f.setBackground(Color.PINK);
}
else if(f.getBackground().equals(Color.PINK))
{
f.setBackground(Color.red);
}
}
public static void main(String[] args) {
new p7();
}
}
Winter 2015
1. Which are the two sets of JAVA API Graphics Programming? [2]
Ans:-

2. What is Swing, Explain. [2]


Ans:-
->Swing is new feature provided by the jfc(java foundation classes).
->Swing extends awt by supplying many more types of GUI components, providing 100% pure
Java implementation of these components, and gives the capability to change the appearance
and behavior of these components on different platforms.
->Swing API is set of extensively GUI components to ease developer’s life to create java based
front end GUI Applications.

3. Differentiate Textarea & Textfield. [3]


Ans:-

4. Describe MOUSEEVENT and MOUSELISTENER interface with example. [4]


Ans:-
->A MouseEvent is fired to all its registered listeners, when you press, release, or click a mouse
button at the source object or position the mouse pointer at and away from the source object.
->MouseListener handles the events when the mouse is not in motion.
-> MouseListener methods are listed below :
1.void mouseReleased(MouseEvent e) : Invoked when Mouse key is released
2.void mouseClicked(MouseEvent e) : Invoked when Mouse key is pressed/released
3.void mouseExited(MouseEvent e) : Invoked when Mouse exited the component
4.void mouseEntered(MouseEvent e) : Invoked when Mouse entered the component
5.void mousepressed(MouseEvent e) : Invoked when Mouse key is pressed
Example:

import java.awt.*;
import java.awt.event.*;
public class demo1 extends Frame implements MouseListener
{
Label l1;

demo1()
{
setLayout(null);
l1=new Label();
add(l1);
l1.setBounds(50, 50, 150, 50);
setVisible(true);
setSize(500,300);
addMouseListener(this);
addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}

@Override
public void mouseClicked(MouseEvent e) {
l1.setText("mouse clicked");
}

@Override
public void mousePressed(MouseEvent e) {
l1.setText("mouse pressed");
}

@Override
public void mouseReleased(MouseEvent e) {
l1.setText("mouse released");
}

@Override
public void mouseEntered(MouseEvent e) {
l1.setText("mouse entered");
}

@Override
public void mouseExited(MouseEvent e) {
l1.setText("mouse exited");
}

public static void main(String[] args)


{
new demo1();
}

}
5. Describe KEYEVENT and KEYLISTENER interface with example. [4]
Ans:-
->A KeyEvent is fired when you pressed, released, and typed a key on the source object.
->The Java KeyListener is notified whenever you change the state of key.
->KeyListener methods are listed below :
1.void keyPressed(KeyEvent e);
2.void keyReleased(KeyEvent e);
3.void keyTyped(KeyEvent e);

Example:

import java.awt.*;
import java.awt.event.*;

class demo extends Frame implements KeyListener


{
TextArea tx;
Label l;
demo()
{
setLayout(new FlowLayout());
tx=new TextArea();
l=new Label("hello");
add(tx);
add(l);
setVisible(true);
setSize(500,300);
tx.addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e)
{
l.setText(e.getKeyChar()+" is pressed");
}

@Override
public void keyPressed(KeyEvent e)
{
}

@Override
public void keyReleased(KeyEvent e)
{
}
public static void main(String[] args) {
new demo();
}
}
Summer 2015
1. What is Swing? Explain the need of Swing. [2]
Ans:-
Swing:
->Swing is a set of program components for Java programmers that provide the ability
to create graphical user interface ( GUI ) components, such as buttons and scroll bars,
that are independent of the windowing system for specific operating system .

->Need of Swing
1. The uniform look and feel across platforms is an advantage
2. Compatible for both standalone/GUI applications,Applets and Servlets
3. Employs model/view design pattern.
4. Has ability to replace the objects on-the-fly
5. Light weight components
6. Has the pluggable look and feel

2. Which method associates a checkbox with a particular group? [2]


Ans:- CheckboxGroup

3. ________ Layout manager lays out the components from left to right. [2]
Ans:- FlowLayout

4. List any five Event classes and five Listener interfaces in JAVA. [2]
Ans:-
Event class:
1.ActionEvent
2.MouseEvent
3.KeyEvent
4.ItemEvent
5.TextEvent
6.WindowEvent

Listener Interface:
1.ActionListener
2.MouseListener
3.KeyListener
4.ItemListener
5.TextListener
6.WindowListener

5. How to create a Frame window within an applet? Give an example.


[No. of times asked in gtu -4] [4]
Ans:-

Potrebbero piacerti anche