Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

Practical No 1: Write a program to demonstrate the use of AWT components.

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Design an applet/application to create form using TextField,TextArea,Button and Label.

Code:

import java.awt.*;

class Practical1 extends Frame

public Practical1()

setLayout(new FlowLayout());

Label lb = new Label("Welcome To RDTC");

TextField tf = new TextField("This is AWT TextField");

TextArea ta = new TextArea("This is AWT TextArea");

Button b1 = new Button("Click me");

add(lb);

add(tf);

add(ta);

add(b1);

public static void main(String[] args)

Practical1 P1 = new Practical1();


P1.setSize(500,500);

P1.setTitle("AJP Practical no 1");

P1.setVisible(true);

}
Output:
Practical No 2: Write a program to design a form using the componets List and Choice.

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Code:

import java.awt.*;

import java.applet.*;

/*<applet code = "Practical2" width = "500" height = "500">

</applet>*/

public class Practical2 extends Applet

public void init()

List Ls1 = new List(3);

Ls1.add("Summer");

Ls1.add("Winter");

Ls1.add("Rainy");

add(Ls1);

}
Output:
Practical No 3: Write a program to design simple calculator with thw use of Grid Layout.

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Code:

import java.awt.*;

import java.applet.*;

/*

<applet code="Practical3" width="500" height="500">

</applet>

*/

public class Practical3 extends Applet

public void init()

setFont(new Font("SansSerif",Font.BOLD,24));

GridLayout g1=new GridLayout(4,3);

setLayout(g1);

for(int i=1;i<=9;i++)

add(new Button(""+i));

Button bt1=new Button("*");

Button bt2=new Button("0");

Button bt3=new Button("#");


add(bt1);

add(bt2);

add(bt3);

}
Output:
Practical No 4: Use of CardLayout to write a program to create a two-level card deck that
allows the user to select an operating system.

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Code:

import java.awt.Button;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import javax.swing.*;

public class GridBagLayoutExample extends JFrame

public static void main(String args[])

GridBagLayoutExample a = new GridBagLayoutExample();

public GridBagLayoutExample()

GridBagLayout grid = new GridBagLayout();

GridBagConstraints gbc = new GridBagConstraints();

setLayout(grid);

setTitle("GridBag Layout Example");

GridBagLayout layout = new GridBagLayout();

this.setLayout(layout);

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.gridx = 0;
gbc.gridy = 0;

this.add(new Button("Button One"), gbc);

gbc.gridx = 1;

gbc.gridy = 0;

this.add (new Button("Button Two"), gbc);

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.ipady = 20;

gbc.gridx = 0;

gbc.gridy = 1;

this.add(new Button("Button Three"), gbc);

gbc.gridx = 1;

gbc.gridy = 1;

this.add(new Button("Button Four"), gbc);

gbc.gridx = 0;

gbc.gridy = 2;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.gridwidth = 2;

this.add(new Button("Button Five"), gbc);

setSize(300,300);

setPreferredSize(getSize());

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}
Output:
Practical No 5: Write a program using AWT to create a menu bar where menu bar contains
menu items such as File,Edit,View and Create a submenu under the File menu:New and Open.

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Write a program which creates Menu of diiferent colors and disable menu item for Black
color.

Code:

import java.awt.*;

class MenuEx extends Frame

MenuEx()

MenuBar mr = new MenuBar();

setMenuBar(mr);

Menu m1 = new Menu("Colours");

MenuItem mn1 = new MenuItem("RED");

MenuItem mn2 = new MenuItem("YELLOW");

MenuItem mn3 = new MenuItem("BLACK");

mn3.setEnabled(false);

MenuItem mn4 = new MenuItem("BLUE");

MenuItem mn5 = new MenuItem("GREEN");

m1.add(mn1);

m1.add(mn2);

m1.add(mn3);
m1.add(mn4);

m1.add(mn5);

mr.add(m1);

class Practical5

public static void main(String[] args)

MenuEx m = new MenuEx();

m.setTitle("Menu Bar");

m.setSize(500,500);

m.setVisible(true);

}
Output:
Practical No 6: Write a program using swing to display a ScrollPane and JcomboBox in nan
JApplet with the items –English,Marathi,Hindi,Sanskrit.

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Write a program to develop a frame to select the different states of India using JcomboBox .

Code:

import javax.swing.*;

public class Practical6

Practical6()

JFrame f = new JFrame();

String s[] = {"Maharashtra","Gujrat","Tamilnadu","Punjab","Kerala"};

JComboBox cb = new JComboBox(s);

cb.setBounds(50,50,90,20);

f.add(cb);

f.setLayout(null);

f.setSize(400,400);

f.setVisible(true);

public static void main(String[] args)

new Practical6();

}
}

Output:
Practical No 7: Write a program to create a JTree.

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Develop a Program to demonstrate the use of tree component in Swing.

Code:

import javax.swing.*;

import javax.swing.tree.DefaultMutableTreeNode;

public class Tree

Tree()

JFrame f=new JFrame();

DefaultMutableTreeNode s=new DefaultMutableTreeNode("India");

DefaultMutableTreeNode s2=new DefaultMutableTreeNode("Maharashtra");

DefaultMutableTreeNode s3=new DefaultMutableTreeNode("Gujrath"); s.add(s2);

s.add(s3);

DefaultMutableTreeNode s4=new DefaultMutableTreeNode("Mumbai");

DefaultMutableTreeNode s5=new DefaultMutableTreeNode("Pune");

DefaultMutableTreeNode s6=new DefaultMutableTreeNode("Nashik");

DefaultMutableTreeNode s7=new DefaultMutableTreeNode("Nagpur");

s2.add(s4);

s2.add(s5);

s2.add(s6); s2.add(s7);
JTree j=new JTree(s);

f.add(j);

f.setSize(200,200);

f.setVisible(true);

public static void main(String[] args)

new Tree();

}
Output:
Practical No 8: Write a program to create a Jtable.

Name: Shirawale Vaishnavi Sudhir

Roll No:

Develop a program to demonstrate the use of JTable .

Code:

import javax.swing.*;

public class TableExample {

JFrame f;

TableExample(){

f=new JFrame();

String data[][]={ {"101","Vaishnavi","800000"},

{"102","Sanika","780000"},

{"101","Anushka","700000"}};

String column[]={"ID","NAME","SALARY"};

JTable jt=new JTable(data,column);

jt.setBounds(30,40,200,300);

JScrollPane sp=new JScrollPane(jt);

f.add(sp);

f.setSize(300,400);

f.setVisible(true);

public static void main(String[] args) {

new TableExample();

}
}

Output:
Practical No 10: Write a program to demonstrate status of key on Applet window such as
KeyPressed,KeyReleased,KeyUp,KeyDown.

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Code:

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code="Simple Key"width="200"height="200">

</applet>*/

public class SimpleKey extends Applet implements KeyListener

String msg = ""; int X = 10, Y = 20;

public void init()

addKeyListener(this);

public void keyPressed(KeyEvent ke)

showStatus("Key Down");

public void keyReleased(KeyEvent ke)

showStatus("Key Up");
}

public void keyTyped(KeyEvent ke)

msg += ke.getKeyChar();

repaint();

public void paint(Graphics g)

g.drawString(msg, X, Y);

}
Output:
Practical No 11: Write a program to demonstrate various mouse events using MouseListener
and MouseMotion listener interface.

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Code:

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

/*<applet code="Count" width="300" height="300">

</applet>*/

public class Count extends Applet implements MouseListener

Label l;

public void init()

l=new Label("Mouse Listener Demo");

add(l);

addMouseListener(this);

public void mousePressed(MouseEvent me)

int a=me.getClickCount();

l.setText("No. of Click Count= "+a);

showStatus("Mouse Clicked");
}

public void mouseClicked(MouseEvent me)

int a=me.getClickCount();

l.setText("No. of Click Count= "+a);

showStatus("Mouse Clicked");

public void mouseReleased(MouseEvent me)

int a=me.getClickCount();

l.setText("No. of Click Count= "+a);

showStatus("Mouse Clicked");

public void mouseEntered(MouseEvent me)

showStatus("Mouse Clicked");

public void mouseExited(MouseEvent me)

showStatus("Mouse Clicked");

}
Output:
Practical No 12: Write a program to demonstrate the use of JtextField and JpasswordField
using Listener Interface .

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Code:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class SwingAddition

public static void main(String args[])

Abc obj=new Abc();

class Abc extends JFrame implements ActionListener

JLabel l1;

JTextField t1;

JLabel l2;

JTextField t2;

JButton b;

JLabel l3;
public Abc()

setLayout(new FlowLayout());

l1=new JLabel("First Number:");

t1=new JTextField(20);

l2=new JLabel("Second Number:");

t2=new JTextField(20);

b=new JButton("Add");

l3=new JLabel("Result");

add(l1);

add(t1);

add(l2);

add(t2);

add(b);

add(l3);

b.addActionListener(this);

setVisible(true);

setSize(250,400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public void actionPerformed(ActionEvent ae)

int num1=Integer.parseInt(t1.getText());
int num2=Integer.parseInt(t2.getText());

int value=num1+num2;

l3.setText(""+value);

}
Output:
Practical No 14: Write a program to demonstrate the use of InetAddress class and its factory
methods.

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Code:

import java.net.*;

import java.util.*;

public class B

public static void main(String[] args) throws Exception

System.out.println(" Enter Host Name: ");

Scanner sc=new Scanner(System.in);

String hostname=sc.nextLine();

InetAddress myIP= InetAddress.getByName(hostname);

System.out.println(" My IP Address is: ");

System.out.println(myIP.getHostAddress());

}
Output:
Practical No 15: Write a program to demonstrate the use of URL and URLConnection class and
its methods .

Name: Shirawale Vaishnavi Sudhir

Roll No: 26

Code:

import java.net.*;

public class practical15

public static void main(String[] args)throws Exception

URL ul=new URL("http://www.msbte.org.in");

System.out.println("Protocol: "+ul.getProtocol());

System.out.println("Port: "+ul.getPort());

System.out.println("Host: "+ul.getHost());

System.out.println("File: "+ul.getFile());

}
Output:

You might also like