0% found this document useful (0 votes)
882 views

Practical No.1 Q1. Design An Applet/application To Demonstrate The Use of Radio Button and Checkbox

The document contains code snippets demonstrating the use of various Java Swing and AWT components. It includes 10 practical exercises with 2 questions each, focusing on different layout managers, components like buttons, text fields, menus etc. and incorporating events. The code snippets provide examples of how to create forms, layouts, trees and tables using these UI elements in Java.

Uploaded by

xapalek559
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
882 views

Practical No.1 Q1. Design An Applet/application To Demonstrate The Use of Radio Button and Checkbox

The document contains code snippets demonstrating the use of various Java Swing and AWT components. It includes 10 practical exercises with 2 questions each, focusing on different layout managers, components like buttons, text fields, menus etc. and incorporating events. The code snippets provide examples of how to create forms, layouts, trees and tables using these UI elements in Java.

Uploaded by

xapalek559
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Practical no.

1
Q1. Design an applet/application to demonstrate the use of Radio Button and Checkbox.

import java.awt.*;
public class Practice extends Frame{
Practice()
{
Checkbox c1=new Checkbox("IT");
Checkbox c2=new Checkbox("CS");
Checkbox c3=new Checkbox("ETC");
c1.setBounds(100,50,100,50);
c2.setBounds(100,100,100,50);
c3.setBounds(100,150,100,50);
add(c1);
add(c2);
add(c3);
CheckboxGroup c=new CheckboxGroup();
Checkbox c4=new Checkbox("Male",c,false);
Checkbox c5=new Checkbox("Female",c,true);
c4.setBounds(100,200,100,50);
c5.setBounds(150,100,100,50);
add(c4);
add(c5);
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new Practice();
}
}
Q2.Design an applet/application to create form using Text Field, Text Area, Button and
Label.

import java.awt.*;
public class Practice extends Frame{
Practice()
{
Label l=new Label("hello");
l.setBounds(100,100,100,50);
TextField t=new TextField(50);
t.setBounds(100,100,100,50);
TextArea ta=new TextArea(6,50);
ta.setBounds(100,200,100,50);
Button b=new Button("Submit");

add(l);
add(t);
add(ta);
add(b);
setVisible(true);
setSize(500,500);

}
public static void main(String[] args) {
new Practice();
}
}
Practical no.2
Write a program to design a form using the components List and Choice.
import java.awt.*;
public class Practice extends Frame{
Practice()
{
List l=new List(5);
l.add("a");
l.add("b");
l.add("c");
l.add("d");
l.setBounds(100,100,100,100);
add(l);
Choice c=new Choice();
c.add("cs");
c.add("IT");
c.add("AI");
add(c);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new Practice();
}

}
Practical no.3

Q1.Write a program to generate GridLayout.

import java.awt.*;
public class Practice extends Frame{
Practice()
{
setLayout(new GridLayout(3,2,20,20));
add(new Button("One"));
add(new Button("two"));
add(new Button("three"));
add(new Button("four"));
add(new Button("five"));

setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new Practice();
}

}
Q2.Write a program to generate following output using Border Layout.

import java.awt.*;
public class Practice extends Frame{
Practice()
{
setLayout(new BorderLayout(20,20));
add(new Button("EAST"),BorderLayout.EAST);
add(new Button("WEST"),BorderLayout.WEST);
add(new Button("NORTH"),BorderLayout.NORTH);
add(new Button("SOUTH"),BorderLayout.SOUTH);
add(new Button("CENTER"),BorderLayout.CENTER);
setVisible(true);
setSize(500,500);

}
public static void main(String[] args) {
new Practice();
}

}
Practical no.4

Q1.Write java program to display GridBag Layout.


import java.awt.*;
public class Practice extends Frame{
Practice() {
GridBagConstraints g=new GridBagConstraints();
setLayout(new GridBagLayout());
g.fill=GridBagConstraints.HORIZONTAL;
g.gridx=0;
g.gridy=0;
this.add(new Button("one"),g);
g.gridx=1;
g.gridy=0;
this.add(new Button("two"),g);
g.gridx=0;
g.gridy=1;
this.add(new Button("three"),g);
g.gridx=1;
g.gridy=1;
this.add(new Button("four"),g);
g.gridx=0;
g.gridy=2;
g.gridwidth=2;
this.add(new Button("five"),g);
setVisible(true);
setSize(500,500);}
public static void main(String[] args) {
new Practice(); }
}
Q2.Write java program to display GridBag Layout.

import java.awt.*;
public class Practice extends Frame{
Practice()
{
Label l=new Label("name");
TextField tf=new TextField(10);
Label l1=new Label("Comments");
TextArea t1=new TextArea(6,16);
Button b1=new Button("Submit");
GridBagLayout g1=new GridBagLayout();
GridBagConstraints g=new GridBagConstraints();
setLayout(g1);
l.setBounds(50,50,50,20);
tf.setBounds(70,50,70,20);
l1.setBounds(50,100,100,50);
t1.setBounds(100,100,70,50);
b1.setBounds(150,200,100,50);
add(l,g);
add(l1,g);
add(tf,g);
add(t1,g);
add(b1,g);
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new Practice();
}}
Practical no.5

Q1.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.
import java.awt.*;
public class Practice extends Frame{
Practice()
{
MenuBar mb=new MenuBar();
Menu m=new Menu("file");
Menu m1=new Menu("Edit");
Menu m2=new Menu("View");
Menu m3=new Menu("Create");
MenuItem mi1=new MenuItem("new");
MenuItem mi2=new MenuItem("Open");
m.add(mi1);
m.add(mi2);
mb.add(m);
mb.add(m1);
mb.add(m2);
mb.add(m3);
setMenuBar(mb);
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new Practice();
}

}
Practical no.6

Q1.Write a program using swing to display a ScrollPane and JCombobox in an JApplet


with the items-English, Marathi, Hindi, Sanskrit.
import java.awt.*;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class Practice extends Frame{
Practice() {
String a[]={"English","Marathi","Hindi","Sanskrit"};
JComboBox jc=new JComboBox<>(a);
jc.setBounds(100,100,100,100);
add(jc);
setSize(500,500);
setVisible(true);
JPanel jp=new JPanel(new GridBagLayout());
int b=0;
for(int i=0;i<10;i++) {
for(int j=0;j<10;j++){
jp.add(new Button("button"+b));
++b; } }
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
JScrollPane js=new JScrollPane(jp, v, h);
add(js);
setVisible(true);
setSize(500,500); }
public static void main(String[] args) {
new Practice(); } }
Practical no.7
Q1.Write a program code to generate the following output.
import java.awt.*;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
public class Practice extends Frame{
Practice() {
DefaultMutableTreeNode d1=new DefaultMutableTreeNode("Inida");
DefaultMutableTreeNode d12=new DefaultMutableTreeNode("Gujrat");
DefaultMutableTreeNode d11=new DefaultMutableTreeNode("Maharashtra");
DefaultMutableTreeNode d111=new DefaultMutableTreeNode("Mumbai");
DefaultMutableTreeNode d112=new DefaultMutableTreeNode("Pune");
DefaultMutableTreeNode d113=new DefaultMutableTreeNode("Nashik");
DefaultMutableTreeNode d114=new DefaultMutableTreeNode("Nagpur");
d1.add(d11);
d1.add(d12);
d11.add(d111);
d11.add(d112);
d11.add(d113);
d11.add(d114);
JTree jt=new JTree(d1);
add(jt);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new Practice();
}

}
Practical no.8
Q1.Write a program code to generate the following code. (JTable)
import java.awt.*;

import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Practice extends Frame{


Practice()
{
String d[][]={
{"101","Amit","670000"},
{"102","Jai","780000"},
{"101","Sachin","700000"}

};
String n[]={"ID","Name","Salary"};
JTable jt=new JTable(d,n);
jt.setBounds(100,100,100,100);
JScrollPane jp=new JScrollPane(jt);
add(jp);
setVisible(true);
setSize(500,500);

}
public static void main(String[] args) {
new Practice();
}
}
Practical no.9
Q1. Write a program code to generate the following output. (JProgressBar)
import java.awt.*;
import javax.swing.*;
public class Practice extends Frame{
JProgressBar p;
int i=0;
Practice() {
p=new JProgressBar(0,100);
p.setBounds(50,50,50,50);
p.setValue(0);
p.setStringPainted(true);
add(p);
setSize(500,500);
setVisible(true);
progress(); }
public void progress() {
while(i<=100) {
p.setValue(i);
i=i+20;
try {
Thread.sleep(300);
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
new Practice();
}
}
Q2.Write a program using JProgressBar to show the progress of Progressbar when user
clicks on JButton.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Practice extends Frame implements ActionListener{
JProgressBar p;
int i=0;
Button b;
Practice() {
p=new JProgressBar(0,100);
p.setBounds(50,50,50,50);
p.setValue(0);
p.setStringPainted(true);
add(p);
b=new Button("click");
b.setBounds(50,100,100,50);
add(b);
b.addActionListener(this);
setSize(500,500);
setVisible(true); }
public void actionPerformed(ActionEvent e) {
progress(); }
public void progress() {
while(i<=100) {
p.setValue(i);
i=i+20;
try {
Thread.sleep(300);
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
new Practice();
}
}
Practical no.10
Q1.Write a program to generate KeyEvent when a key is pressed and display “Key
Pressed” message.
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Practice extends KeyAdapter{
Label l;
TextField t;
Frame f;
Practice()
{ f=new Frame("keylistener");
l=new Label();
t=new TextField(20);
l.setBounds(70,70,100,50);
t.setBounds(70,170,100,50);
f.add(l);
f.add(t);
t.addKeyListener(this);
f.setSize(500,500);
f.setVisible(true); }
public void keyPressed(KeyEvent e) {
l.setText("Key Pressed"); }
public static void main(String[] args) { new Practice(); } }
Q2.Develop a program which will implement special keys such as function keys and
arrow keys.
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Practice extends KeyAdapter{
Label l;
TextField t;
Frame f;
Practice()
{ f=new Frame("keylistener");
l=new Label();
t=new TextField(20);
l.setBounds(70,70,100,50);
t.setBounds(70,170,100,50);
f.add(l);
f.add(t);
t.addKeyListener(this);
f.setSize(500,500);
f.setVisible(true);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
{
l.setText("key is up");
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
l.setText("key is down");
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
l.setText("key is left");
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
l.setText("key is right");
}
else if(e.getKeyCode()==KeyEvent.VK_F1)
{
l.setText("key clicked f1");
}
else if(e.getKeyCode()==KeyEvent.VK_F2)
{
l.setText("key clicked f2");
}
else if(e.getKeyCode()==KeyEvent.VK_F3)
{
l.setText("key clicked f3");
}
else if(e.getKeyCode()==KeyEvent.VK_F4)
{
l.setText("key clicked f4");
}
else if(e.getKeyCode()==KeyEvent.VK_F5)
{
l.setText("key clicked f5");
}
else if(e.getKeyCode()==KeyEvent.VK_F6)
{
l.setText("key clicked f6");
}
else if(e.getKeyCode()==KeyEvent.VK_F7)
{
l.setText("key clicked f7");
}
else if(e.getKeyCode()==KeyEvent.VK_F8)
{
l.setText("key clicked f8");
}
else if(e.getKeyCode()==KeyEvent.VK_F9)
{
l.setText("key clicked f9");
}
else if(e.getKeyCode()==KeyEvent.VK_F10)
{
l.setText("key clicked f10");
}
else if(e.getKeyCode()==KeyEvent.VK_F11)
{
l.setText("key clicked f11");
}
else if(e.getKeyCode()==KeyEvent.VK_F12)
{
l.setText("key clicked f12");
}
}
public static void main(String[] args) {
new Practice(); } }
Practical no.11
Q1.Write a program to demonstrate various mouse events using MouseListener and
MouseMotion listener interface.
1) import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
class Practice extends Frame implements MouseListener {
Label l=new Label();
Practice(){
add(l);
l.addMouseListener(this);
setSize(500,500);
setVisible(true); }
@Override
public void mouseClicked(MouseEvent e) {
l.setBackground(Color.red); }
@Override
public void mousePressed(MouseEvent e) {
l.setBackground(Color.green); }
@Override
public void mouseReleased(MouseEvent e) {
l.setBackground(Color.blue); }
@Override
public void mouseEntered(MouseEvent e) {
l.setBackground(Color.yellow); }
@Override
public void mouseExited(MouseEvent e) {
l.setBackground(Color.gray); }
public static void main(String[] args) {
new Practice(); } }
2) import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
class Practice extends Frame implements MouseMotionListener
{

Practice()
{
addMouseMotionListener(this);
setSize(500,500);
setVisible(true);

}
@Override
public void mouseDragged(MouseEvent e) {
Graphics g=getGraphics();
g.setColor(Color.green);
g.fillOval(e.getX(),e.getY(),10,10);
}
@Override
public void mouseMoved(MouseEvent e) {
Graphics g=getGraphics();
g.setColor(Color.red);
g.fillOval(e.getX(),e.getY(),30,10);
}
public static void main(String[] args) {
new Practice();
}
}
Practical no.12
Q1.Write a program to demonstrate the use of JTextField and JPasswordField using
Listener Interface.
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
class Practice extends Frame implements MouseMotionListener
{

Practice()
{
addMouseMotionListener(this);
setSize(500,500);
setVisible(true);

}
@Override
public void mouseDragged(MouseEvent e) {
Graphics g=getGraphics();
g.setColor(Color.green);
g.fillOval(e.getX(),e.getY(),10,10);
}
@Override
public void mouseMoved(MouseEvent e) {
Graphics g=getGraphics();
g.setColor(Color.red);
g.fillOval(e.getX(),e.getY(),30,10);
}
public static void main(String[] args) {
new Practice(); } }
Practical no.14
Q1.Develop a program using InetAddress class to retrive IP address of computer when
hostname is entered by the user.
import java.net.*;
import java.util.*;
class Practice
{
public static void main(String[] args) {
try {
Scanner sc=new Scanner(System.in);
System.out.println("enter the name :");
String s=sc.next();
InetAddress ip=InetAddress.getByName(s);
System.out.println("IP address :"+ip.getHostAddress());

} catch (Exception e) {
System.out.println(e);
}
}
}
Practical no.15
Q1.Write program using URL class to retrieve the host, protocol, port and file of URL
http://www.msbte.org.in
import java.net.*;
class Practice
{
public static void main(String[] args) {
try {
URL u=new URL("http://www.msbte.org.in");
System.out.println("Host :"+u.getHost());
System.out.println("protocol :"+u.getProtocol());
System.out.println("port "+u.getPort());
System.out.println("file :"+u.getFile());

} catch (Exception e) {
System.out.println(e);
}
}
}
Q2.Write a program using URL and URLConnection class to retrive the date, content
type, content length information of any entered URL.
import java.net.*;
class Practice {
public static void main(String[] args) { try {
URL u=new URL("http://www.msbte.org.in");
URLConnection uc=u.openConnection();
System.out.println("Date:"+uc.getDate());
System.out.println("Type:"+uc.getContentType());
System.out.println("port "+uc.getContentLength());
} catch (Exception e) {
System.out.println(e); } } }

You might also like