AVJP
AVJP
import java.awt.*;
class L {
L()
{
Frame f=new Frame();
Label l1=new Label("Welcome to Java");
l1.setBounds(100,50,120,80);
f.add(l1);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String a[])
{
new L();
}}
3. Write a program to create three Buttons with captions OK, RESET and CANCEL.
import java.awt.*;
class But
{
But()
{
Frame f=new Frame();
Button b1=new Button("Ok");
b1.setBounds(100,50,50,50);
f.add(b1);
Button b2=new Button("Reset");
b2.setBounds(100,101,50,50);
f.add(b2);
Button b3=new Button("Cancel");
b3.setBounds(100,150,80,50);
f.add(b3);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String a[])
{
new But();
1. Develop an applet/application using List components to add names of 10
different cities.
/*<applet code="Cities" width=300 height=300></applet>*/ import
java.applet.*;
import java.awt.*; import
java.awt.event.*; public class
Cities extends Applet
{
public void init()
{
List l1=new List(10);
l1.setBounds(60,70,100,120);
l1.add("Kolhapur");
l1.add("Mumbai");
l1.add("Pune");
l1.add("Nagpur");
l1.add("Nashik");
l1.add("Satara");
l1.add("Solapur");
l1.add("Jalgaon");
l1.add("Sangli");
l1.add("Ahmednagar");
add(l1);
setLayout(null);
}
public void paint(Graphics g)
{
repaint();
}
}
2. Develop applet/application to select multiple names of news papers.
/*
<applet code="News" width=300 height=300></applet>
*/ import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class News extends Applet
{
public void init()
{
List l1=new List(10,true);
l1.setBounds(50,60,100,120);
l1.add("TImes of India");
l1.add("Loksatta");
l1.add("Pudhari");
l1.add("Sakal");
add(l1);
setLayout(null);
}
public void paint(Graphics g)
{ repaint();
}
}
1. Write a program to generate following output.
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Grid_Buttons {
Grid_Buttons(){
JFrame frame = new JFrame("GridLayoutDemo");
JButton button,button1, button2, button3,button4;
button = new JButton("button 1");
button1 = new JButton("button 2");
button2 = new JButton("button 3");
button3 = new JButton("button 4");
button4 = new JButton("button 5");
frame.add(button);
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.setLayout(new GridLayout(3,2,20,20));
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[]args) {
new Grid_Buttons();
}
}
2. Write a program to generate following output using Border Layout.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Border_Layout {
Border_Layout(){
JFrame frame = new JFrame("Border Layout");
JButton button,button1, button2, button3,button4;
button = new JButton("West");
button1 = new JButton("East");
button2 = new JButton("North");
button3 = new JButton("South");
button4 = new JButton("center");
frame.add(button,BorderLayout.WEST);
frame.add(button1, BorderLayout.EAST);
frame.add(button2, BorderLayout.NORTH);
frame.add(button3, BorderLayout.SOUTH);
frame.add(button4, BorderLayout.CENTER);
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[] args){
new Border_Layout();
}
}
1. Write java program to display following output:
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);
}
2. Write java program to display following output.
import java.awt.*; class
GridBagLayoutExample extends Frame
{
GridBagLayoutExample()
{
Label lblName = new Label("Name");
TextField txtName =new TextField(10);
Label lblcomments = new Label("Comments");
TextArea TAreaComments=new TextArea(6,15);
Button btnSubmit = new Button("Submit");
setLayout(new GridBagLayout());
GridBagConstraints gc =new GridBagConstraints();
add(lblName,gc,0,0,1,1,0,0);
add(txtName,gc,1,0,1,1,0,20);
add(lblcomments,gc,0,1,1,1,0,0);
add(TAreaComments,gc,1,1,1,1,0,60);
add(btnSubmit,gc,0,2,2,1,0,20);
}
void add(Component comp,GridBagConstraints gc,int x,int y,int w,int h,int wx,int
wy)
{
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = w;
gc.gridheight= h;
gc.weightx = wx;
gc.weighty = wy;
add(comp,gc);
}
}
class GridBagLayoutJavaExample
{
public static void main(String args[])
{
GridBagLayoutExample frame = new GridBagLayoutExample();
frame.setTitle("GridBagLayout in Java Example");
frame.setSize(300,200);
frame.setVisible(true);
}
}
1. Find errors in following program and display output as shown below’
import java.awt.*;
import java.awt.event.*;
public class MenuDemo1 extends Frame
{
MenuBar mb;
MenuItem m1,m2,m3,m4;
Menu mn;
MenuShortcut ms;
MenuDemo1()
{
Frame f= new Frame("Menu and MenuItem Example");
f.setTitle("MenuBar Demo");
ms=new MenuShortcut(KeyEvent.VK_X);
mb=new MenuBar();
mn=new Menu("File");
m1=new MenuItem("New...");
m2=new MenuItem("Open...");
m3=new MenuItem("Save As...");
m4=new MenuItem("Exit",ms);
mn.add(m1);
mn.add(m2);
mn.add(m3);
mn.addSeparator();
mn.add(m4);
mb.add(mn);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
MenuDemo1 md=new MenuDemo1();
}}
Write a program to develop a frame to select the different states of India using
JComboBox.
import javax.swing.*;
public class CB2 {
CB2(){
JFrame f=new JFrame();
String s[]={"Maharashtra","Punjab","Gujrat","TamilNadu"};
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 CB2();
}
}
OUTPUT:
f.setSize(500, 500);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new FlowLayout());
JTextArea t = new JTextArea(20, 20);
JScrollPane s1 = new JScrollPane(t);
s1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
);
s1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
f.getContentPane().add(s1);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI();
}
});
} }
1.Develop a program to demonstrate the use of tree component in swing.
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeExample {
JFrame f;
TreeExample(){
f=new JFrame();
DefaultMutableTreeNode style=new DefaultMutableTreeNode("Style");
DefaultMutableTreeNode color=new DefaultMutableTreeNode("Color");
DefaultMutableTreeNode font=new DefaultMutableTreeNode("Font");
style.add(color);
style.add(font);
DefaultMutableTreeNode red=new DefaultMutableTreeNode("Red");
DefaultMutableTreeNode blue=new DefaultMutableTreeNode("Blue");
DefaultMutableTreeNode black=new DefaultMutableTreeNode("Black");
DefaultMutableTreeNode green=new DefaultMutableTreeNode("Green");
color.add(red); color.add(blue); color.add(black); color.add(green);
JTree jt=new JTree(style);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String[] args) {
new TreeExample();
}
}
Output:-
2.Write a program code to generate the following output.
Program Code:-
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeIndia {
JFrame f;
TreeIndia(){
f=new JFrame();
DefaultMutableTreeNode india=new DefaultMutableTreeNode("India");
DefaultMutableTreeNode maharashtra=new DefaultMutableTreeNode("Maharashtra");
DefaultMutableTreeNode gujrath=new DefaultMutableTreeNode("Gujrath");
india.add(maharashtra);
india.add(gujrath);
DefaultMutableTreeNode mumbai=new DefaultMutableTreeNode("Mumbai");
DefaultMutableTreeNode pune=new DefaultMutableTreeNode("Pune");
DefaultMutableTreeNode nashik=new DefaultMutableTreeNode("Nashik");
DefaultMutableTreeNode nagpur=new DefaultMutableTreeNode("Nagpur");
maharashtra.add(mumbai); maharashtra.add(pune);
maharashtra.add(nashik); maharashtra.add(nagpur);
JTree jt=new JTree(india);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String[] args) {
new TreeIndia();
}
}
3.Write a program to show root directory and its subfolders of your system.
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class SubFolder {
JFrame f;
SubFolder(){
f=new JFrame();
DefaultMutableTreeNode pc=new DefaultMutableTreeNode("This PC");
DefaultMutableTreeNode disk_c=new DefaultMutableTreeNode("Local Disk (C:)");
DefaultMutableTreeNode disk_d=new DefaultMutableTreeNode("Local Disk (D:)");
DefaultMutableTreeNode disk_g=new DefaultMutableTreeNode("Local Disk (G:)");
pc.add(disk_c);
pc.add(disk_d);
pc.add(disk_g);
//for disk_c
DefaultMutableTreeNode html=new DefaultMutableTreeNode("Html");
DefaultMutableTreeNode program_files=new DefaultMutableTreeNode("Program
Files");
DefaultMutableTreeNode tyco=new DefaultMutableTreeNode("Tyco");
DefaultMutableTreeNode xampp=new DefaultMutableTreeNode("Xampp");
disk_c.add(html); disk_c.add(program_files);
disk_c.add(tyco); disk_c.add(xampp);
//for disk_d
DefaultMutableTreeNode backup=new DefaultMutableTreeNode("Backup");
DefaultMutableTreeNode java_script=new DefaultMutableTreeNode("JavaScript");
disk_d.add(backup); disk_d.add(java_script);
JTree jt=new JTree(pc);
f.add(jt);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String[] args) {
new SubFolder();
}
}
1.Develop a program to demonstrate the use of JTable.
import javax.swing.*;
public class TableExample {
TableExample(){
JFrame f=new JFrame();
String data[][]={ {"1","Prashant","30,000"},
{"2","Anish","40,000"},
{"3","Rahul","50,000"},
{"4","Aniket","60,000"}
};
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();
}
}
1. Develop a program to demonstrate the use of JProgressBar.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ProgressEx extends JFrame {
static JFrame f;
static JProgressBar b;
public static void main(String[] args)
{
f = new JFrame("ProgressBar demo");
JPanel p = new JPanel();
b = new JProgressBar();
b.setValue(0);
b.setStringPainted(true);
p.add(b);
f.add(p);
f.setSize(500, 500);
f.setVisible(true);
fill();
}
public static void fill()
{
int i = 0;
try {
while (i <= 100) {
b.setValue(i + 10);
Thread.sleep(1000);
i += 20;
}
}
catch (Exception e) {
}
} }
OUTPUT:
2. Write a program using JProgressBar to show the Progress of Progressbar when user
clicks on JButton.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class MyFrame extends JFrame implements ActionListener {
JProgressBar pb;
JButton b1 = new JButton("LOGIN");
MyFrame() {
setLayout(null);
pb = new JProgressBar(1, 100);
pb.setValue(0);
pb.setStringPainted(true);
b1.setBounds(20, 20, 80, 25);
pb.setBounds(110, 20, 200, 25);
pb.setVisible(false);
add(b1);
add(pb);
b1.addActionListener(this);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
int i = 0;
if (e.getSource() == b1) {
pb.setVisible(true);
try {
while (i <= 100) {
Thread.sleep(50);
pb.paintImmediately(0, 0, 200, 25);
pb.setValue(i);
i++;
}
} catch (Exception e1) {
System.out.print("Caughted exception is =" + e1);
}
}
}}
public class Progressbar {
public static void main(String arg[]) {
MyFrame m = new MyFrame();
m.setSize(330, 100);
m.setVisible(true);
}}
1. Develop a program to accept two numbers and display product of two numbers when
user pressed “Multiply” Button.
import javax.swing.*;
import java.awt.event.*;
public class multiply implements ActionListener{
JTextField tf1,tf2,tf3;
JButton b1;
multiply(){
JFrame f= new JFrame();
tf1=new JTextField();
tf1.setBounds(50,50,150,20);
tf2=new JTextField();
tf2.setBounds(50,100,150,20);
tf3=new JTextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new JButton("*");
b1.setBounds(50,200,50,50);
b1.addActionListener(this);
f.add(tf1);
f.add(tf2);
f.add(tf3);
f.add(b1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) { String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a*b;
}
String result=String.valueOf(c); tf3.setText(result);
}
public static void main(String[] args) { new multiply();
} }
Write a program to change the background color of Applet when user performs
events using Mouse.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class bgchangeapplet extends Applet implements MouseListener {
Color color = Color.green;
public void init()
{
addMouseListener(this);
}
public void paint(Graphics g) {
g.setColor(this.color);
g.drawRect(10, 30, 150, 150);
}
public void mouseClicked(MouseEvent e) {
this.color = color.red;
this.repaint();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(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 Frame
window.
import java.awt.*;
import java.awt.event.*;
public class MultipleClicks extends Frame
{
Button b1;
public MultipleClicks()
{
setLayout(new FlowLayout());
b1 = new Button("CLICK MANY TIMES");
b1.addMouseListener(new HelperListener());
add(b1);
setSize(250, 250);
setVisible(true);
}
public static void main(String args[])
{
new MultipleClicks();
}
class HelperListener extends MouseAdapter
{
public void mouseClicked (MouseEvent e)
{
int count = e.getClickCount();
System.out.println(count);
}
}}OUTPUT:
3. Write a program using JPasswordField to accept password from user and if the
length
is less than 6 characters then error message should be displayed “Password length
must be >6 characters”.
import java.awt.*;
import java.awt.event.*;
public class myPassFrame extends Frame implements ActionListener
{
TextField nameField, passField, resultField;
Label lab1, lab2, lab3;
public myPassFrame()
{ // set layout
setLayout(new GridLayout(3, 2, 0, 10));
setBackground(Color.pink); // fill the gap with color
// create components
passField = new TextField(15);
resultField = new TextField(15);
lab2 = new Label("Enter Password");
lab3 = new Label("Display Result");
// register the listener
passField.addActionListener(this);
// beautification
passField.setEchoChar('*');
resultField.setEditable(false);
add(lab2); add(passField);
add(lab3); add(resultField);
setTitle(" Password Validation");
setSize(300, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ // get the values entered by the user
String str2 = passField.getText();
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Graphics g=f.getGraphics();
g.setColor(Color.ORANGE);
g.fillOval(e.getX(),e.getY(),20,20);
}
public static void main(String[] args) {
new MouseMotionAdapterExample();
}
}
1. Develop a program using InetAddress class to retrieve IP address of computer
when hostname
is entered by the user.
import java.net.*;
classInetAddressE
x{
publicstaticvoidmain(Stringargs[])throwsUnknownHostException
{
InetAddress
i=InetAddress.getLocalHost();System.out.printl
n("Local
Host="+i);i=InetAddress.getByName("www.m
sbte.org.in");System.out.println(i);
InetAddresssw[]=InetAddress.getAllByName("www.google.com"
);for(intj=0;j<=sw.length;j++)
{
System.out.println(sw[j]);
}}}
1. Write a program using URL class to retrieve the host, protocol, port and file of
URL
http://www.msbte.org.in
import java.net.*;
class URLDemo1 {
public static void main(String args[]) throws MalformedURLException
{
URL hp = new URL("http://www.msbte.org.in");
System.out.println("Protocol: " + hp.getProtocol());
System.out.println("Port: " + hp.getPort());
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
}}
OUTPUT:
2. Write a program using URL and URLConnection class to retrieve the date, content
type, content length information of any entered URL
import java.net.*;
import java.io.*;
import java.util.Date;
import java.lang.*;
public class UMLConDemo
{
public static void main(String args[]) throws Exception
{
int c;
URL url = new URL("http://www.msbte.org.in");
URLConnection urlc = url.openConnection();
long d = urlc.getDate();
if(d == 0)
System.out.println("No date Information.");
else
System.out.println("Date: "+new Date(d));
System.out.println("Content Type: "+urlc.getContentType());
int len = urlc.getContentLength();
if(len == -1)
System.out.println("Content length not available");
else
System.out.println("Lenght of the Content: "+len);
d = urlc.getExpiration();
}
}
1. Write a program using Socket and ServerSocket to create Chat Application
Client Program:-
import java.io.*;
import java.net.*;
public class ClientEx
{
public static void main(String args[]) throws Exception
{
Socket s=new Socket("127.0.0.1",3000);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
OutputStream os=s.getOutputStream();
PrintWriter pwrite=new PrintWriter(os,true);
InputStream is=s.getInputStream();
BufferedReader rread=new BufferedReader(new InputStreamReader(is));
System.out.println("start chat type and press enter");
String rm , sm;
while(true)
{
sm=br.readLine();
pwrite.println(sm);
pwrite.flush();
if((rm=rread.readLine())!=null)
{
System.out.println(rm);
}}}}
Server Program:-
import java.io.*;
import java.net.*;
public class ServerEx
{
public static void main(String args[]) throws Exception
{
ServerSocket sr=new ServerSocket(3000);
System.out.println("ready for chatting");
Socket sok=sr.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
OutputStream os=sok.getOutputStream();
PrintWriter pwrite=new PrintWriter(os,true);
InputStream is=sok.getInputStream();
BufferedReader rread=new BufferedReader(new InputStreamReader(is));
String rm , sm;
while(true)
{
if((rm=rread.readLine())!=null)
{
System.out.println(rm);
}
sm=br.readLine();
pwrite.println(sm);
pwrite.flush();
}}}
1.Write a program using DatagramPacket and DatagramSocket to create chat
application
import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ServerSideData
{
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(2019);
byte[] receiveData = new byte[512];
byte[] sendData = new byte[512];
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
System.out.println(" UDP Server Socket is created, waiting for client ");
do
{
DatagramPacket receiveDP = new DatagramPacket(receiveData,receiveData.length);
ds.receive(receiveDP);
String clientMessage = new String(receiveDP.getData(),0,receiveDP.getLength());
System.out.println("Client Message:"+clientMessage);
InetAddress ip = receiveDP.getAddress();
System.out.print("\n\nEnter Server Message:");
String serverMessage = br.readLine();
sendData = serverMessage.getBytes();
DatagramPacket sendDP = new DatagramPacket(sendData, sendData.length, ip,
receiveDP.getPort());
ds.send(sendDP);
ds.send(sendDP);
DatagramPacket receiveDP = new DatagramPacket(receiveData,receiveData.length);
ds.receive(receiveDP);
String serverMessage = new String(receiveDP.getData(),0,receiveDP.getLength());
System.out.println("\n\nServer Message:"+serverMessage);
}while(true);
} }
2. Write a program using DatagramPacket and DatagramSocket to transfer the file
from
one location to another.
ServerFile
import java.net.*;
import java.io.*;
public class ServerFile
{
public static void main(String args[])throws IOException
{
byte b[]=new byte[3072];
DatagramSocket dsoc=new DatagramSocket(2019);
FileOutputStream f=new
FileOutputStream("C:\\Users\\shivraj\\Documents\\adv java\\SFile.txt");
} }
ClientFile
import java.net.*;
import java.io.*;
public class ClientFile
{
public static void main(String args[])throws Exception
{
byte b[] = new byte[1024];
FileInputStream f = new FileInputStream("C:\\Users\\shivraj\\Documents\\adv
java\\CFile.txt");
DatagramSocket dsoc = new DatagramSocket();
int i=0;
while(f.available() != 0)
{
b[i]=(byte)f.read();
i++;
}
f.close();
dsoc.send(new DatagramPacket(b,i,InetAddress.getLocalHost(),2019));
} }