AJP Practical 01 09
AJP Practical 01 09
Program Code:
import java.awt.*;
public class Practical_VS1{
public static void main(String[]
args) { Frame f = new
Frame("Practical 1");
f.setSize(500, 400);
f.setLayout(null);
Label nameLabel = new Label("Student
Name:"); nameLabel.setBounds(50, 50,
100, 30); TextField name = new
TextField(); name.setBounds(150, 50,
200, 30);
Label rollNoLabel = new Label("Roll
No:"); rollNoLabel.setBounds(50,
90, 100, 30); TextField rollNo =
new TextField();
rollNo.setBounds(150, 90, 200,
30);
Label enrollmentLabel = new Label("Enrollment
No.:"); enrollmentLabel.setBounds(50, 130,
100, 30); TextField enrollment = new
TextField(); enrollment.setBounds(150, 130,
200, 30);
Label casteLabel = new
Label("Caste:");
casteLabel.setBounds(50, 170, 100,
30); Choice caste = new Choice();
caste.add("OBC");
caste.add("SC/ST");
caste.add("OPEN");
caste.add("NT");
caste.setBounds(150, 170, 200,
30);
Label addressLabel = new
Label("Address:");
addressLabel.setBounds(50, 210, 100,
30); TextArea address = new
TextArea(10, 3);
address.setBounds(150, 210, 200, 50);
Label genderLabel = new
Label("Gender:");
genderLabel.setBounds(50, 270, 100,
30);
CheckboxGroup checkboxGroup = new CheckboxGroup();
Checkbox male = new Checkbox("Male", false,
checkboxGroup); male.setBounds(150, 270, 100, 30);
Checkbox female = new Checkbox("Female", false,
checkboxGroup); female.setBounds(250, 270, 100, 30);
Button submit = new
Button("Submit");
submit.setBounds(200, 320, 100,
30); f.add(nameLabel);
f.add(name);
f.add(rollNoLabel);
f.add(rollNo);
f.add(enrollmentLab
el);
f.add(enrollment);
f.add(casteLabel);
f.add(caste);
f.add(addressLabel);
f.add(address);
f.add(genderLabel);
f.add(male);
f.add(female);
f.add(submit);
f.setVisible(true);
}
}
Output:
Aim:
Write a program to design a form using the components List and Choice.
Theory:
What is Choice and List?
The java.awt.Choice component implements a list of items where only
the selected item is displayed.
The list appears as a drop down (pop-down menu) menu can be seen
through the touch of a button built into the component, so it is also
known as a check box or ComboBox. In the same manner as in
component java.awt.List; a vertical scrollbar is automatically displayed
when the list can not simultaneously show all the items they contain. The
selection only operate in simple mode i.e. only one item can be selected
at a time, and the choice of an item not selected selects and vice versa.
List: A List may be displayed in such a way that several List items are
visible. A List supports the selection of one or more List items.A list is any
enumeration of a set of items. A List supports the selection of one or
more list items.
• Constructor:
1) Choice()
• Method:
void add(String item); //Items added int the Choice List.
String getSelectedItem();
int getSelectedIndex();
int getItemCount();
void select(int index);
void select(String item);
String getItem(int index);
• Constructor:
List();
List(int visible_rows);
List(int visible_rows,boolean flag);
• Method :
void add(String item);
void add(String item,int index);
String getSelectedItem();
int getSelectedIndex();
String[] getSelectedItems();
Program Code:
import java.awt.*;
class Practical_2VS extends
Frame { public Practical_2VS()
{ setLayout(null);
Label sub = new Label("Choose Subjects:");
sub.setBounds(50, 50, 120, 30);
List ls = new List(5, true);
ls.setBounds(180, 50, 150, 120);
ls.add("Java");
ls.add("C++");
ls.add("Python");
ls.add("Operating
System");
ls.add("Applied
Mathematics"); add(ls);
Output:
Aim: Write a program to design simple calculator with the use of Grid Layout.
Theory:
A layout manager automatically arranges your controls within a window.
While it is possible to layout Java controls by hand, too, you generally
won’t. It is very tedious to manually lay out a large number of
components.
Layout Manager is a facility that determines how components should be
arranged when they are added to the container. Layout Manager is an
interface that is implemented by all the classes of layout managers.
There are following classes that represent the layout
managers. Layout Types :
FlowLayout
GridLayout
BorderLayout
CardLayout
GridbagLayout
Grid Layout:
import java.awt.*;
add(bp,
BorderLayout.CENTER);
setSize(400, 500);
setVisible(true);
}
Output:
Aim:
Theory:
1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
2. CardLayout(int hgap, int vgap): creates a card layout with the given
horizontal and vertical gap.
3. public void first (Container parent): is used to flip to the first card of the
given container.
4. public void last (Container parent): is used to flip to the last card of the
given container.
import java.awt.*;
import
java.awt.event.*;
import
javax.swing.JFrame;
import javax.swing.*;
CardLayoutDemo()
{
c = getContentPane();
card = new CardLayout(40,
30); c.setLayout(card);
b1.addActionListener(this);
b2.addActionListener(this);
c.add("a", b1);
c.add("b", b2);
}
cl.setSize(400,
400);
cl.setVisible(true);
}
}
Output:
Aim:
Theory:
Menu Bar
Instantiate a MenuBar.
Create Menu objects (like "File", "Edit", etc.).
Add Menu objects to the MenuBar.
Set the MenuBar to your Frame or Dialog.
Menu
Creating a Menu:
Menu Item
Instantiate a MenuItem.
Optionally, set properties like labels or shortcuts.
Add an ActionListener to handle actions.
Program Code:
import java.awt.*;
public class MenuDialog
{
public static void main(String[] args)
{
Frame f=new Frame("Practical 5 by VS");
MenuBar mBar = new MenuBar();
File.add(New);
New.add(i9);
New.add(i10);
File.add(i2);
File.add(i3);
File.add(i4);
mBar.add(File);
Edit.add(i5);
Edit.add(i6);
Edit.add(i7);
Edit.add(i8);
View.add(Zoom);
Zoom.add(i14);
Zoom.add(i15);
View.add(i12);
View.add(i13);
mBar.add(Edit);
mBar.add(View);
f.setMenuBar(mBar);
f.setVisible(true);
f.setSize(400,400);
f.setLayout(null);
}
Output:
Marks Obtained Dated Signature
of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 6
Aim:
Theory:
JComboBox
Key Points:
JScrollPane:
Primary Function:
import java.awt.*;
import
javax.swing.*;
public class Practical8 extends JFrame
{
public Practical8()
Container ct=
getContentPane();
ct.setLayout(null);
jc.addItem("English");
jc.addItem("Hindi");
jc.addItem("Marath
i");
jc.addItem("Sanskri
t"); ct.add(jl);
ct.add(jc);
jl.setBounds(30,50,100,
30);
jc.setBounds( 150,50,100,30);
}
Practical8();
fr.setTitle("Practical 6
VS");
fr.setSize(300,40
0);
fr.setVisible(true
);
}
Output:
Aim:
Theory:
JTree:
Purpose
Key Components
1. Tree Model:
o TreeModel: The data model used by JTree to manage and
provide data for the tree. The TreeModel interface defines
methods for interacting with the tree’s data structure.
o DefaultTreeModel: A commonly used implementation of
TreeModel that works with DefaultMutableTreeNode to
create and manage the tree structure.
2. Tree Nodes:
o TreeNode: The interface representing each node in the tree. It
includes methods for managing child nodes and querying the
node's information.
o DefaultMutableTreeNode: A concrete implementation of
TreeNode that allows nodes to be mutable and supports
adding, removing, and accessing children.
Program Code :
import javax.swing.*;
import
javax.swing.tree.DefaultMutableTreeNode;
public class TreeExample
{
JFrame f;
TreeExample
()
{
f=new JFrame();
DefaultMutableTreeNode Universe=new
DefaultMutableTreeNode("Universe"); DefaultMutableTreeNode
Planets=new DefaultMutableTreeNode("Planets");
DefaultMutableTreeNode Stars=new
DefaultMutableTreeNode("Stars"); Universe.add(Planets);
Universe.add(Stars);
DefaultMutableTreeNode Earth=new DefaultMutableTreeNode("Earth");
DefaultMutableTreeNode Jupiter=new
DefaultMutableTreeNode("Jupiter"); DefaultMutableTreeNode
Venus=new DefaultMutableTreeNode("Venus");
DefaultMutableTreeNode Mars=new DefaultMutableTreeNode("Mars");
Planets.add(Earth); Planets.add(Jupiter); Planets.add(Venus);
Planets.add(Mars); JTree jt=new JTree(Universe);
f.add(jt);
f.setSize(200,20
0);
f.setVisible(true)
;
f.setTitle("Practical 7 VS");
}
}
Output :
Aim:
Theory :
KeyListener Interface :
keyPressed(KeyEvent e)
keyReleased(KeyEvent e)
keyTyped(KeyEvent e)
Method Details
1. keyPressed(KeyEvent e)
Purpose:
Parameters:
Usage:
Purpose:
Parameters:
Usage:
This method is used to handle actions that should occur after a key
is released. It’s useful for actions that should be finalized or
updated only when the user completes a key press.
3. keyTyped(KeyEvent
e) Purpose:
Parameters:
Usage:
import java.awt.*;
import java.applet.*;
import
java.awt.event.*;
public class VSPractical8 extends Applet implements KeyListener
{
String msg = "";
repaint();
}
Aim:
Theory :
MouseListener
MouseListener Interface
mouseClicked(MouseEvent e): Invoked when the user clicks the mouse button
on a component.
MouseMotionListener
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class VSPractical9 extends Applet implements MouseListener
{
String s="See your event here";
/*
<applet code="VSPractical9.class" width="500" heigth="300">
</applet>*/
Output :
Program Code for MouseMotionListener Methods() :
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
</applet>*/
{ this.addMouseMotionListener(new Innerl());
Graphics g=getGraphics();
g.setColor(Color.RED);
g.fillRect(me.getX(),me.getY(),20,20);
}
public void mouseDragged(MouseEvent me)
{
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillRect(me.getX(),me.getY(),20,20);
}
}
}
Output :