Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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:
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.
1. Paint(Graphics g)
2. Update(Graphics g)
3. getAccessibleContext()
Example:
import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Color;
import java.applet.Applet;
import java.awt.*;
public class demo extends Applet
{
Frame f;
Label l;
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;
@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);
}
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Click me"))
{
l.setText("button is pressed");
}
}
}
Summer 2018
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);
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Submit"))
{
jl.setText(jtf1.getText());
}
}
}
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;
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;
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);
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
4. Write the name of class used to create input Dialog box in Java. [2]
Ans:- JOptionPane
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;
@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.
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");
}
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.*;
void mouseReleased(MouseEvent e)
->Invoked when a mouse button has been released on a component.
Syntax:
JButton jbtn=new JButton(“String”);
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);
}
}
Container:
Example:
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
});
}
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
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);
}
}
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);
}
}
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");
}
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:-
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");
}
}
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.*;
@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
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