Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Develop a program using Label to display message “Welcome to Java”.
[Link].*;
Class Exp1
{
Public static void main(String args[])
{
Frame f= new Frame();
[Link](true);
[Link](400,400);
[Link](new FlowLayout());
[Link](“Experiment no.1”);
Label l1=new Label(“Welcome to Java”);
[Link](1);
}
}
1
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Develop a program to select multiple languages known to user.
[Link].*;
class Expt1
{
Public static void main(String args[])
{
Frame f = new Frame();
[Link](true);
[Link](400,400);
[Link](new FlowLayout());
[Link](“Experiment no.1”);
Checkbox c1=new Checkbox(“Marathi”);
Checkbox c2=new Checkbox(“English”);
Checkbox c3=new Checkbox(“Hindi”);
Checkbox c4=new Checkbox(“Sanskrit”);
[Link](c1);
[Link](c2);
[Link](c3);
[Link](c4);
}
}
2
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Write a program to create three Buttons with Caption Ok, Reset and Cancel.
[Link].*;
class Expt1
{
Public static void main(String args[])
{
Frame f= new Frame;
[Link](true);
[Link](400,400);
[Link](new FlowLayout());
f,setTitle(“Experiment no.1”);
Button b1=new Button(“OK”);
Button b2=new Button(“RESET”);
Button b3=new Button(“CANCEL”);
[Link](b1);
[Link](b2);
[Link](b3);
}
}
3
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Develop an applet/application using List components to add names of 10 different cities.
import [Link].*;
import [Link].*;
/* <applet code=”T1” width=300 height=300> </applet>*/
Public class T1 extends Applet
{
Public void init()
{
List L= new List(10,true);
[Link](“Mumbai”);
[Link](“Pune”);
[Link](“Delhi”);
[Link](“Hydrabad”);
[Link](“Bengaluru”);
[Link](“Chennai”);
[Link](“Kolkata”);
[Link](“Ahemdabad”);
[Link](“Amritsar”);
[Link](“Agra”);
Add(L);
}
4
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Develop applet/application to select multiple names of news papers
import [Link].*;
import [Link].*;
/* <applet code=”T1” width=300 height=300> </applet>*/
Public class T1 extends Applet
Public void init()
{
List L= new List(10,true);
[Link](“Lokmat”);
[Link](“Sakal”);
[Link](“Indian Express”);
add(L);
Import [Link].*;
5
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
class P
{
public static void main(String args[])
{
Frame f=new Frame();
[Link](true);
[Link](500,500);
[Link](new GridLayout(3,2));
Button b1=new Button(“Button1”);
Button b2=new Button(“Button2”);
Button b3=new Button(“Button3”);
Button b4=new Button(“Button4”);
Button b5=new Button(“Button5”);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
}
}
Import [Link].*;
Class R
{
6
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
public static void main(String args[])
{
Frame f=new Frame();
[Link](true);
[Link](500,500);
[Link](new BorderLayout() );
Button b1=new Button(“North”);
Button b2=new Button(“South”);
Button b3=new Button(“East”);
Button b4=new Button(“West”);
Button b5=new Button(“Center”);
[Link](b1,[Link]);
[Link](b2,[Link]);
[Link](b3,[Link]);
[Link](b4,[Link]);
[Link](b5,[Link]);
}
}
7
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Import [Link];
Import [Link];
Import [Link];
Import [Link].*;
Public class FridBagLayoutExample extends JFrame{
Public static void main(String[] args){
GridBagLayoutExample a = new GridBagLayoutExample();
}
Public GridBagLayoutExample(){
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = newGridBagConstraints();
setLayout(grid);
setTitle(“Griding Layout Example”);
GridBagLayout layout = new GridBagLayouy();
[Link] layout = new GridBagLayout();
[Link] = [Link];
[Link]=0;
[Link]=0;
[Link](new Button(“Button One”), gbc);
[Link] =1;
[Link] =0; [Link](new Button(“Button two”),gbc);
[Link]= [Link];
[Link] = 20;
[Link] = 0;
[Link] =1;
[Link](new Button(“Button Three”), gbc);
[Link] =1;
[Link] =1;
[Link](new Button(“Button Four”), gbc);
[Link] =0;
8
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
[Link] =2;
[Link]=[Link];
[Link] = 2;
[Link](new Button(“Button Five”),gbc);
setSize(300,300);
setPreferredSize(getSize());
setVisible(true);
}
}
9
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Import [Link].*;
Class GridBagLayoutExample extends Frame
{
GridBagLayoutExample()
{
setLayout(new FlowLayout() );
Lablel b = new
Label(“Name”);
TextField t =new TextField(10);
Label c =new Label (“Comments”);
TextArea ta =new TextArea(6,15);
Button bs = new Button(“Submit”);
add(b);
add(t);
add(c);
add(ta);
add(bs);
}
}
class GridBagLayoutJavaExamplel
{
Public static void main (String args[])
{
GridBagLayoutExample frame = new GridBagLayoutExample();
[Link](“GridBagLayout in Java Example”); [Link](300,200);
[Link](true);
}
}
10
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
package harshad;
import [Link].*;
import [Link];
public class menubar extends Frame {
MenuBar mb;
MenuItem m2,m1,m3,m4;
Menu mn;
MenuShortcut ms;
menubar()
{
setTitle("MenuBar Demo");
setSize(300,300);
setVisible(true);
setLayout(null);
ms = new MenuShortcut(KeyEvent.VK_X);
mn = new Menu("File");
mb = new MenuBar();
m1= new MenuItem("New...");
m2 = new MenuItem("Open...");
m3 = new MenuItem("Save as...");
m4 = new MenuItem("Exit",ms);
[Link](m1);
[Link](m2);
[Link](m3);
[Link](m4);
[Link]();
[Link](mn);
setMenuBar(mb);
}
public static void main(String args[])
{
menubar md = new menubar();
[Link](true);
}
}
1
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Import [Link].*;
Public class Aaa
Aaa(){
JFrame f= new JFrame();
String a[] = {“Maharashtra”, “Punjab”, “Gujarat”, “TamilNadu”);
JcomboBox cb=new JComboBox(a); [Link](50,50,90,20);
[Link](cb); f,setLayou(null);
[Link](400,400);
[Link](true);
Public static void main(String args[])
{new Aaa();}
1
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Import [Link].*;
Import class Aaa {
Private statuc void Scroll(){
Final JFrame f = new JFrame();
[Link](500,500);
[Link](true);
[Link](J/frame.EXIT_ON_CLOSE);
[Link]().
setLayout(new FlowLayout());
JTextArea t = new JTextArea(20,20);
JScrollPane s1 = new JScrollPane(t);
[Link].HORIZONTAL_SCROLLBAR_ALWAYS);
[Link](JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
[Link]().
Add(s1);
Public static void main (String args[]){
[Link](new Runnable() {
Pubic void run() {
Scroll();
2
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Import [Link].*:
Public class Main{
Public static void main (String[] args) {
File[] roots =[Link]();
[Link](roots[i].toString());
}}} */
Import [Link]*;
Import [Link];
Class Main{
Public static void main(String[] args)
{ try{
File[] files = [Link]();
for (File file : files) {
if([Link]()) {
[Link]{ “directory:”+ [Link]()};
displayDirectory(file);
}
[Link]{ “file:”+ [Link]()};
} } }
Catch(IOException e) {
[Link]();
} }}
1
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Import [Link].*;
Import [Link].*;
Public class Exp10 extends Frame implements ActionListener
{
Label l1, l2, l3;
Textfield tf1;
Int num1, num2;
Exp10(int n1, int n2) {
setSize(400,400);
setLayout(
new FlowLayout());
setVisible(true); num1=n1;
num2=n2; l1=new
Label(“num1:”+n1); l2= new
Label(“num2:” +n2);
tf1 = new TextField(30);
Button b1= new Button(“Multiply”);
Add(l1);
Add(l1);
Add(l1)
Add(l2);
Add(b1);
Add(tf1);
[Link](this);
}
Public void actionPerformed(ActionEvrent e) // overriding the keyPressed() method of Keylistener interface
{
[Link](“Multication is:” +num1*num2);
}
Public static vpoid main(String[][ args)
{
New Exp10(12,13);
} }
1
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Practical Related Questions
1. Name any four Event Listener interfaces.
Ans. Four Event Listener Interface are: i.
ActionListener ii. ItemListener iii.
KeyListener iv. MouseListener
2. State the situation when all three events of KeyListener interface are generated?
Ans.
Import [Link].*;
Import [Link].*;
Public class Exp10 extends Frame implements KeyListener
{
Label l;
TextArea area; Exp10(){
setSize(400,400);
setLayout(null);
setVisible(true); l
= new Label();
[Link] (20, 50, 100, 20);
area = new TextArea();
[Link](this);
add(l); add(area);
}
Public void keyPressed (KeyEvent e) //overriding the keypressed() method of
KeyListener interface { [Link] (“Key Pressed”);
}
Public void keyReleased (KeyEvent e) //overriding the keyReleased() method of
KeyListener interface
{ [Link](“Key Released”);
}
Public void keyTyped (keyEvent e) // overriding the keyTyped() method of Keylistener
interface
2
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
}
Public static void main(String[] args)
{
New Expt10();
}
}
3. Elaborate the terms Event, Source and Listener.
Ans.
i. Event:- Event is an object that describes state chage in a source. Event
may also occur that are not directly caused by interactions with the UI.
Applet are event driven programs. Tjere are several types of events. ii. Event
Sources:- A source is an object that generates an event. Sources may generate more than
one type of event. A source must registr listeners I order for the listeners to receive
notification about specific type of event. Each type of event has its own registration method.
iii. Event Listener:- A listener is an object that is notified when an event occurs. It has two
major requirements. It must registered with one or more Sources to receive notification
about specific type of events.
4. List various methods of ActionListener interface.
Ans. Method in ActionListener interface
i. Void actionPeerformed(ActionEvent e) Invoked when an action occurs.
Marks Obtained Dated
signatur
e of
Teacher
Process Product Tota l
Related Related (50)
(35) (15)
3
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
4
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
5
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Name of Swati Padhi
Student
Roll No. 20203C0038
Class CO5I-C
Experiment 11
No
Title of Write a program to demonstrate various mouse events using
Experiment MouseListener and MouseMotion listener interface.
Experiment-11
1. Write a program to change the background color of Applet when user performs events using Mouse.
Ans.
Import [Link].*;
Import [Link].*;
Import [Link].*;
/*<Applet code =”e1” width=300 height=200></applet>*?
Public class e1 extends Applet implements MouseListener
Public void init()
{
addMouseListener(this);
Public void mouseClicked(MouseEvent e)
{
setBackgrounf([Link]);
}
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Public void mouseEntered(MouseEvent e)
}
Public void mouseExitedd(MouseEvent e)
{
}
Public void mousePressed(MouseEvent e)
Public void mouseReleased(MouseEvent e)
{
}
Output:
2. Write a program to count the number of clicks performed by the user in a Frane window.
Ans.
import [Link]; import [Link].*; import [Link].*;
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
/* <APPLET CODE ="MouseDemo" WIDTH=300 HEIGHT=200> </APPLET> */ public
class MouseDemo extends Applet implements MouseListener
Label l;
public void init()
setLayout(null); l=new Label("Hello Mouse");
[Link](50,150,200,100); add(l);
public void mousePressed(MouseEvent e)
[Link]("Mouse Pressed no. of clicks:" + [Link]() + "at position" + [Link]() + ","+ [Link]());
public void mouseReleased(MouseEvent e)
[Link]("Mouse Released; # of clicks:"+[Link]());
public void mouseEntered(MouseEvent e)
[Link]("Mouse Entered");
public void mouseExited(MouseEvent e)
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
{
[Link]("Mouse exited");
public void mouseClicked(MouseEvent e)
[Link]("mouse clicked(# of clicks:"+[Link]());
}
3. Write a program to demonstrate the use o f mouse Dragged and mouseMoved method of
MouseMotionListener.
Ans.
Import [Link].*;
Import [Link].*;
Import [Link].*;
/*<applet code=”[Link]” height=300 width=300></applet>*/
Public class MouseMotionEventTest extends implements MouseMotionListener
{
String msg=””;
Int mouseX=0,mouseY=0;
Public void init()
{
addMouseMOtionListener(this);
}
Public void mouseDragged(MouseEvent me)
{
mouseX=[Link]();
mouseY=[Link](); msg=””;
showStatus(“Dragging mouse at”+mouseX+”,”+mouseY”);
repaint();
}
Public void mouseMoved(MouseEvent me)
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
{
showStatus(“Moving mouse at”+[Link]()+”,”+[Link]());
}
Public void paint (Graphics g)
{
[Link](msg, mouseX, mouseY);
}
}
Output:
Practical Related Questions
1. List various method of MouseListener and MouseMotionListener
Ans. There are five types of event that MouseListener can generate:
I. Void mouseReleased(MouseEvent e): Mouse key is released
II. Void mouseClicked(MouseEvent e): Mouse key is released
III. Void mouseExited(MouseEvent e): Mouse exited the component
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
IV. Void mouseEnterd(MouseEvent e): Mouse eneterd the component
V. Void mousepressed(MouseEvent e): Mouse key is pressed
There are two types of events that MOuseMOtionListener can generate.
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.
2. Do all componets generate the MouseEvent
Ans “YES” all components generate mouse event in java.
3. Write the steps to obtain the coordinate of MouseClick.
Ans. The coordinates of the mouse when ever a click takes place can be found by detecting the click event with
an event listener and finding the event’s x and y position.
4. Write the steps to register for MouseEvents
Ans. A MouseEvent object is also passed to every MOuseMotionListener or
MouseMotionAdpter object which is registered to receive mouse motion events using the component’s
addMouseMotionListener method.
Marks
Dated signature of
arks Obtained Teacher
Process Related(35) Product
Related(15) Total(50)
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
`
Program: Computer Engineering (NBA Accredited)
Course - Advanced Java Programming (22515) Practical
Answer Sheet
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
7
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
Name of Student Swati Padhi
Roll No. 20203C0038
Class CO5I-C
Experiment No 12
Title of Experiment Write a program to demonstrate the use of
JTextField and JPasswordField using Listener
Interface.
Experiment-12
1. Write a program using JPasswordField and JTextField to demonstrate the use of user authentication. Ans.
Import [Link].*;
Import [Link].*;
Import [Link].*;
Public class A extends JFrame implements ActionListener
{
JButton nutton;
JTextField jtf;
JPasswodField jpf;
JLabel j1, j2;
Public A()
{
setVisible(true);
setTitle(“MyFrame”);
setSize(300,300); setLayout(new FlowLayout());
j1=new
JLabel(“User Name”); j2=new
JLabel(“Password”); jtf= new
JTextField(15); button = new
JButton(“Submit”); add(j1); add(jtf);
add(j2); add(jpf);
add(button);
[Link](this);
}
Public void actionPerformed(ActionEvent e)
{
String s1,s2;
S1= [Link]();
S2=[Link]();
Name : Tanmay Mukesh Ghanekar
Roll No : 12
Div : TYCO - B
If([Link](“Prakash Koyander””)&&([Link](“123”)))
{
[Link](this, “Authenticated User”);
}
Else
{
[Link] MessageDialog(this, “Unauthorized User”);
}
Pubic static void main(String[] args)
{
A frame2 = new A();
}
}
Output:
2. Write a program using JTextField to perform the addition of two numbers.
Ans.
Import [Link].*;
Import [Link].*;
Import [Link].*;
Public class A extends JFrame implements ActionLIstener
{
JButton button;
JTextField jtf1, jtf2, jtf3;
JLabel j1, j2,j3;
Public A()
{
setVisible(true);
setTitle(“MyFrame”);
setSize(300,300); setLayout.(new FlowLayout());
j1=new
JLabel(“First NUmber”); j2=new
JLabel(“Second Number”); jtf1= new
JTextField(15); jtf2=new JTextField(15):
jtf3=new JTextField(15); button = new
JButton(“Addition”); add(j1); add(jtf1);
add(j2); add(jtf2); add(button);
add(j3); add(jtf3);
[Link](this);