0% found this document useful (0 votes)
28 views36 pages

AJP Practical 01 09

Uploaded by

vinitsahare963
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views36 pages

AJP Practical 01 09

Uploaded by

vinitsahare963
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Practical 1

Aim: Write a program to demonstrate the use of AWT components like


Label, TextArea, Button, CheckBox, RadioButton, etc.

Theory: AWT is java programming language class library component are


visible objects that can interact with the user. containers (Frame, Panel,
Applet) are use to hold component using in a specific Layout.

Using applet window, design following AWT components using add()


method of component class. Following are some AWT component.

 1.Label: Create a label that displays a string.


 2.TextField: Create and accept a single-line text from user.
 3.TextArea: Create and accept a multiple line text from user.
 4.Button: Create a push button.
 5.Checkbox: Create a Checkbox which is used to select multiple option.
 6.Checkboxgroup: Create a group of checkbox act as Radiobutton.

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:

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical : 2

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.

Difference between Choice and List-

Choice: A Choice is displayed in a compact form that requires you to pull


it down to see the list of available choices. Only one item may be
selected from a Choice.Choice is the act of picking or deciding between
two or more possibilities. Only one item may be selected from a choice.

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);

Label sem = new Label("Select


Semester:"); sem.setBounds(50, 200,
120, 30);
add(sem);

Choice ch = new Choice();


ch.setBounds(180, 200, 150, 30);
ch.add("Semister 1");
ch.add("Semister 2");
ch.add("Semister 3");
ch.add("Semister 4");
ch.add("Smeister 5");
add(ch);
add(sub);
}

public static void main(String args[])


{
Practical_2VS p2 = new Practical_2VS();
p2.setSize(600, 600);
p2.setTitle("Practical
2");
p2.setVisible(tru
e);
}
}

Output:

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 3

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:

Grid Layout is used to make a bunch of components equal in size and


displays them in the requested number of rows and columns. One
component is displayed in each rectangle. The list of Constructor for
GridLayout are:

 GridLayout(): creates a grid layout with one column per component in a


row.
 GridLayout(int rows, int columns): creates a grid layout with the
given rows and columns but no gaps between the components.
 GridLayout(int rows, int columns, int hgap, int vgap): creates a grid
layout with the given rows and columns along with given horizontal
and vertical gaps if we give setLayout(null) the default layout is
disabled.then we have to use setBounds method to layout the
components.
Program Code:

import java.awt.*;

public class SimpleCalculator extends

Frame { public SimpleCalculator() {


setTitle("Practical 3");
setLayout(new
BorderLayout());

Panel dp = new Panel();


dp.setLayout(new BorderLayout());
TextField display = new
TextField(); dp.add(display,
BorderLayout.NORTH); add(dp,
BorderLayout.NORTH);

Panel bp = new Panel();


bp.setLayout(new GridLayout(4, 4,
5, 5)); bp.add(new Button("7"));
bp.add(new Button("8"));
bp.add(new Button("9"));
bp.add(new Button("/"));
bp.add(new Button("4"));
bp.add(new Button("5"));
bp.add(new Button("6"));
bp.add(new Button("*"));
bp.add(new Button("1"));
bp.add(new Button("2"));
bp.add(new Button("3"));
bp.add(new Button("-"));
bp.add(new Button("0"));
bp.add(new Button("."));
bp.add(new Button("="));
bp.add(new Button("+"));

add(bp,
BorderLayout.CENTER);
setSize(400, 500);
setVisible(true);
}

public static void main(String[]


args) { new
SimpleCalculator();
}
}

Output:

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 4

Aim:

Write a program to create two-level card desk that allow the


user to select component of panel using CardLayout

Theory:

The CardLayout class manages the components in such a manner that


only one component is visible at a time. It treats each component as a
card hence known as CardLayout.

Constructors of CardLayout class :

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.

Commonly used methods of CardLayout class:


1. public void next (Container parent): is used to flip to the next card of the
given container
2. public void previous (Container parent): is used to flip to the previous
card of the given container.

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.

5. public void show (Container parent, String name): is used to flip to


the specified card with the given name
Program Code:

import java.awt.*;
import
java.awt.event.*;
import
javax.swing.JFrame;
import javax.swing.*;

public class CardLayoutDemo extends JFrame implements

ActionListener { CardLayout card;


JButton b1, b2,
b3; Container c;

CardLayoutDemo()
{
c = getContentPane();
card = new CardLayout(40,
30); c.setLayout(card);

b1 = new JButton("First Level");


b2 = new JButton("Second Level");

b1.addActionListener(this);
b2.addActionListener(this);

c.add("a", b1);
c.add("b", b2);
}

public void actionPerformed(ActionEvent e)


{
card.next(c);
}

public static void main(String[] args)


{
CardLayoutDemo cl = new CardLayoutDemo();

cl.setSize(400,
400);
cl.setVisible(true);

}
}
Output:

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 5

Aim:

Write a program using AWT to create a menubar where menubar contains


menu items such as File, Edit, View and create a submenu under the File
Menu: New and Open.

Theory:

 Menu Bar

In AWT, a MenuBar is used to hold one or more Menu objects, and it is


typically added to a Frame or Dialog.

Creating a 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

A Menu is a drop-down list that can contain multiple MenuItem objects. It


is added to a MenuBar and represents a category of commands or
options.

Creating a Menu:

 Instantiate a Menu with a name.


 Add MenuItem objects to the Menu.

 Menu Item

A MenuItem represents a single option within a Menu. It can be a simple


menu item or a check box item.

Creating a 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();

Menu File = new Menu("File");


Menu Edit = new Menu("Edit");
Menu View = new Menu("View");
Menu New = new Menu("New");
Menu Zoom = new Menu("Zoom");
MenuItem i2 = new MenuItem("Open Ctrl+O");
MenuItem i3 = new MenuItem("Save Ctrl+S");
MenuItem i4 = new MenuItem("Exit ");
MenuItem i5 = new MenuItem("Undo Ctrl+Z");
MenuItem i6 = new MenuItem("Cut Ctrl+X");
MenuItem i7 = new MenuItem("Copy Ctrl+C");
MenuItem i8 = new MenuItem("Paste Ctrl+V");
MenuItem i9 = new MenuItem("New Tab Ctrl+N");
MenuItem i10 = new MenuItem("New Window Ctrl+Shift+N");
MenuItem i12 = new MenuItem("Status Bar");
MenuItem i13 = new MenuItem("Word wrap");
MenuItem i14 = new MenuItem("Zoom In ");
MenuItem i15 = new MenuItem("Zoom Out ");

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:

Write a program using swing to display a scrollpane and JCombobox in


an Japplet with the items: English, Marathi, Hindi, Sanskrit.

Theory:

 JComboBox

 JComboBox is a component that provides a drop-down list of


items for the user to choose from. It’s useful for selecting from a
limited set of options.

Key Points:

 Model: JComboBox uses a model to manage its list of items. The


default model is DefaultComboBoxModel, but you can use custom
models if needed.

 Editable vs. Non-Editable: By default, a JComboBox is non-


editable, meaning users can only select from the list. You can
make it editable by calling setEditable(true), allowing users to
type their own entries.

 JScrollPane:

JScrollPane is a key component in Java Swing used to provide scrolling


capabilities for other components when their content exceeds the visible
area.

Primary Function:

JScrollPane allows for the inclusion of a scrollable view for another


component, making it useful when the content is too large to fit within a
fixed-size area.
Program Code:

import java.awt.*;
import
javax.swing.*;
public class Practical8 extends JFrame
{
public Practical8()

Container ct=

getContentPane();

ct.setLayout(null);

JLabel jl = new JLabel("Select Language:");

JComboBox<String> jc = new JComboBox<>();

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);
}

public static void main(String ar[])

Practical8 fr= new

Practical8();

fr.setTitle("Practical 6

VS");
fr.setSize(300,40
0);
fr.setVisible(true
);

}
Output:

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 7

Aim:

Write a Program to create a JTree.

Theory:

 JTree:

JTree is a Swing component in Java used to display a hierarchical tree of


data. It is a powerful way to represent and interact with hierarchical
information, such as file systems, organizational structures, or nested
data. Here's a comprehensive overview of JTree, including its components,
configuration, and best practices.

 Purpose

 Hierarchical Data Representation: JTree is designed to display and


manage hierarchical data structures where each item (node) can
have zero or more child nodes, creating a tree-like structure.

 Interactivity: It provides a way for users to navigate and interact


with hierarchical data through expanding and collapsing nodes,
selecting items, and customizing the tree’s appearance.

 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");
}

public static void main(String[] args)


{
new TreeExample();

}
Output :

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 8

Aim:

Write a program to Demonstrate status of key on Applet window such as


KeyPressed, Key Released, KeyUp, KeyDown.

Theory :

 KeyListener Interface :

The KeyListener interface is a part of the java.awt.event package and is


used to handle keyboard events in AWT-based components. It has three
primary methods:

 keyPressed(KeyEvent e)
 keyReleased(KeyEvent e)
 keyTyped(KeyEvent e)

Each of these methods is invoked in response to a specific type of keyboard


event.

Method Details

1. keyPressed(KeyEvent e)

Purpose:

o Called when a key is pressed down.

Parameters:

o KeyEvent e: Contains information about the key event,


including the key code, key character, and other event
details.

Usage:

o This method is typically used to handle actions that should


occur immediately when a key is pressed. For example,
detecting when a user presses a shortcut key or handling
input that should be processed as soon as the key is pressed.
2. keyReleased(KeyEvent e)

Purpose:

 Called when a key is released after being pressed.

Parameters:

 KeyEvent e: Contains information about the key release event,


including the key code and other event details.

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:

 Called when a key is typed, which means the key is pressed


and then released.

Parameters:

 KeyEvent e: Contains information about the key typed event,


including the character representation of the key.

Usage:

 This method is used for handling character input, such as


processing typed characters or implementing text field validation.
Unlike keyPressed and keyReleased, which handle low-level key
codes, keyTyped deals with the actual character input.
Program Code :

import java.awt.*;
import java.applet.*;
import
java.awt.event.*;
public class VSPractical8 extends Applet implements KeyListener
{
String msg = "";

public void init()


{
addKeyListener(this);
}

public void keyReleased(KeyEvent k)


{
showStatus("Key Up / Key
Released"); repaint();
}

public void keyTyped(KeyEvent k)


{

showStatus("Key Down / Key Typed");


repaint();
}

public void keyPressed(KeyEvent k)


{
showStatus("Key Pressed");

repaint();
}

public void paint(Graphics g)


{
g.drawString(msg, 10, 10);
}
}
/* <applet code="VSPractical8.class" height="400" width="400">
</applet>*/
Output :

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)
Practical 9

Aim:

Write a program to Demonstrate various mouse event using MouseListener


and MouseMotionlistener.

Theory :

 MouseListener

MouseListener is an interface used for handling mouse events. It is part of


the java.awt.event package and provides methods to respond to various
mouse actions, such as clicks, presses, releases, and entering or exiting a
component.

 MouseListener Interface

The MouseListener interface defines the following five methods:

 mouseClicked(MouseEvent e): Invoked when the user clicks the mouse button
on a component.

 mousePressed(MouseEvent e): Invoked when the user presses a mouse button


on a component.

 mouseReleased(MouseEvent e): Invoked when the user releases a mouse button


on a component.

 mouseEntered(MouseEvent e): Invoked when the mouse enters a component.

 mouseExited(MouseEvent e): Invoked when the mouse exits a component.

 MouseMotionListener

The MouseMotionListener interface in Java is used to handle mouse


motion events, such as when the mouse is moved or dragged over a
component.

It's part of the java.awt.event package and is often used in conjunction


with MouseListener to provide a more comprehensive mouse interaction
experience.
 MouseMotionListener Interface :

The MouseMotionListener interface defines two methods:

 mouseDragged(MouseEvent e): Called when the user drags the mouse


while holding down a button.
 mouseMoved(MouseEvent e): Called when the mouse is moved over a
component.

Program Code for MouseListener Methods() :

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class VSPractical9 extends Applet implements MouseListener
{
String s="See your event here";

public void init()


{
this.addMouseListener(this);
}
public void paint(Graphics g)
{
g.drawString(s,100,100);
}
public void mouseEntered(MouseEvent me)
{
s="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
s="Mouse Exited"; repaint();
}
public void mouseClicked(MouseEvent me)
{
s="Mouse Clicked";
repaint();
}
public void mousePressed(MouseEvent me)
{
s="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent me)
{
s = "Mouse Released";
repaint();
}}

/*
<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 code="VSPractical9" width="300" height="300">

</applet>*/

public class VSPractical9 extends Applet

public void init()

{ this.addMouseMotionListener(new Innerl());

class Innerl extends MouseMotionAdapter

public void mouseMoved(MouseEvent me)

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 :

Marks Obtained Dated Signature


of Teacher
Process Product Total (50)
Related(15) Related(35)

You might also like