AJP_Practical Exam
AJP_Practical Exam
l1=new Label("Name:");
l2=new Label("DOB:");
l3=new Label("Gender:");
l4=new Label("Email:");
l5=new Label("Phone No:");
l6=new Label("Select Course:");
l7=new Label("Address:");
l1.setBounds(70,50,60,20);
l2.setBounds(70,80,60,20);
l3.setBounds(70,110,60,20);
l4.setBounds(70,140,60,20);
l5.setBounds(70,170,60,20);
l6.setBounds(50,200,90,20);
l7.setBounds(70,230,60,20);
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
t1=new TextField();
t2=new TextField();
CheckboxGroup cbg=new CheckboxGroup();
M=new Checkbox("Male",cbg,true);
F=new Checkbox("Female",cbg,false);
t3=new TextField();
t4=new TextField();
t5=new TextField();
c1=new Choice();
c1.add("IT");
c1.add("CO");
c1.add("AN");
ta1=new TextArea(5,4);
b1=new Button("Submit");
t1.setBounds(150,50,150,20);
t2.setBounds(150,80,150,20);
M.setBounds(150,110,50,20);
F.setBounds(200,110,70,20);
t3.setBounds(150,110,150,20);
t4.setBounds(150,140,150,20);
t5.setBounds(150,170,150,20);
c1.setBounds(150,200,150,20);
ta1.setBounds(150,235,150,70);
b1.setBounds(150,320,70,20);
add(t1);
add(t2);
add(t4);
add(t5);
add(M);
add(F);
add(c1);
add(ta1);
add(b1);
}
public static void main(String[] args) {
a_college ac=new a_college();
}
}
2) Resume
ANS:
import java.awt.*;
import java.awt.event.*;
public class BiodataExample extends Frame implements ActionListener{
Label l1, l2, l3, l4, l5, l6, l7, l8, l9;
TextField t1, t2, t3, t4, t5, t6;
TextArea ta1;
CheckboxGroup cbg;
Checkbox M, F;
Choice c1;
Button b1, b2;
BiodataExample(){
setVisible(true);
setSize(500, 500);
setTitle("BIODATA");
setLayout(null);
l1=new Label("Name:");
l2=new Label("Age:");
l3=new Label("Address:");
l4=new Label("Contact No:");
l5=new Label("Email-Id:");
l6=new Label("Gender:");
l7=new Label("Qualification:");
l8=new Label("Skills:");
l9=new Label("Hobbies:");
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
add(l8);
add(l9);
t1=new TextField(3);
t1.setBounds(130, 50, 200, 20);
add(t1);
t2=new TextField(3);
t2.setBounds(130, 80, 200, 20);
add(t2);
ta1=new TextArea();
ta1.setBounds(130, 110, 300, 100);
add(ta1);
t3=new TextField(3);
t3.setBounds(130, 220, 200, 20);
add(t3);
t4=new TextField(3);
t4.setBounds(130, 250, 200, 20);
add(t4);
cbg=new CheckboxGroup();
M=new Checkbox("Male", cbg, true);
add(M);
F=new Checkbox("Female", cbg, true);
add(F);
M.setBounds(130, 280, 50, 24);
F.setBounds(190, 280, 70, 24);
c1=new Choice();
c1.add("---------Select Qualification---------");
c1.add("B.Tech");
c1.add("M.Tech");
c1.add("B.Sc");
c1.add("B.com");
c1.setBounds(130, 310, 200, 20);
add(c1);
t5=new TextField(3);
t5.setBounds(130, 340, 200, 20);
add(t5);
t6=new TextField(3);
t6.setBounds(130, 370, 200, 20);
add(t6);
b1=new Button("SUBMIT");
b1.setBounds(100, 410, 70, 30);
add(b1);
b2=new Button("CLEAR");
b2.setBounds(200, 410, 70, 30);
add(b2);
}
public static void main(String[] args) {
BiodataExample bd=new BiodataExample();
}
}
2. WAP to implement all swing components.
1) College Admission Form
ANS:
import javax.swing.*;
public class CollegeForm extends JFrame {
JLabel l1, l2, l3, l4, l5, l6, l7;
JTextField t1, t2, t3, t4, t5;
JTextArea ta1;
JComboBox c1;
JRadioButton M, F;
JButton b1;
CollegeForm() {
// Set up the frame
setVisible(true);
setSize(600, 500);
setLayout(null);
setTitle("College Admission Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Initialize components
l1 = new JLabel("Name:");
l2 = new JLabel("DOB:");
l3 = new JLabel("Gender:");
l4 = new JLabel("Email:");
l5 = new JLabel("Phone No:");
l6 = new JLabel("Select Course:");
l7 = new JLabel("Address:");
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
t1 = new JTextField();
t2 = new JTextField();
t3 = new JTextField();
t4 = new JTextField();
t5 = new JTextField();
M = new JRadioButton("Male",true);
F = new JRadioButton("Female");
c1 = new JComboBox();
c1.addItem("IT");
c1.addItem("CO");
c1.addItem("AN");
add(t1);
add(t2);
add(t3);
add(t4);
add(M);
add(F);
add(c1);
add(ta1);
add(b1);
}
import javax.swing.*;
public class BiodataExample extends JFrame {
JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9;
JTextField t1, t2, t3, t4, t5, t6;
JTextArea ta1;
JRadioButton M, F;
JComboBox c1;
JButton b1, b2;
BiodataExample() {
// Frame setup
setVisible(true);
setSize(500, 500);
setTitle("BIODATA");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Initialize labels
l1 = new JLabel("Name:");
l2 = new JLabel("Age:");
l3 = new JLabel("Address:");
l4 = new JLabel("Contact No:");
l5 = new JLabel("Email-Id:");
l6 = new JLabel("Gender:");
l7 = new JLabel("Qualification:");
l8 = new JLabel("Skills:");
l9 = new JLabel("Hobbies:");
// Initialize buttons
b1 = new JButton("SUBMIT");
b2 = new JButton("CLEAR");
// Set button bounds
b1.setBounds(100, 410, 100, 30);
b2.setBounds(220, 410, 100, 30);
ANS:
Grid Layout:
import java.awt.*;
class GridLayoutDemo extends Frame
{
GridLayoutDemo()
{
setLayout(new GridLayout(3,2,3,3));
Button b1=new Button("Button1");
add(b1);
Button b2=new Button("Button2");
add(b2);
Button b3=new Button("Button3");
add(b3);
Button b4=new Button("Button4");
add(b4);
Button b5=new Button("Button5");
add(b5);
Button b6=new Button("Button6");
add(b6);
setVisible(true);
setSize(500,500);
setTitle("GridLayoutDemo");
}
public static void main(String args[])
{
new GridLayoutDemo();
}
}
Border Layout:
import java.awt.*;
class BorderLayoutDemo extends Frame
{
BorderLayoutDemo()
{
setLayout(new BorderLayout(3,3));
Button north=new Button("North");
add(north,BorderLayout.NORTH);
Button south=new Button("South");
add(south,BorderLayout.SOUTH);
Button east=new Button("EAST");
add(east,BorderLayout.EAST);
Button west=new Button("West");
add(west,BorderLayout.WEST);
setVisible(true);
setSize(500,500);
setTitle("BorderLayoutDemo");
}
public static void main(String args[])
{
new BorderLayoutDemo();
}
}
Flow Layout:
import java.awt.*;
class FlowLayoutDemo extends Frame
{
Button b1,b2;
FlowLayoutDemo()
{
setLayout(new FlowLayout());
b1=new Button("Button1");
add(b1);
b2=new Button("Button2");
add(b2);
setVisible(true);
setSize(500,500);
setTitle("Hello");
}
public static void main(String args[])
{
new FlowLayoutDemo();
}}
menuBar.add(m1);
menuBar.add(m2);
menuBar.add(m3);
m3.setEnabled(false);
setSize(400, 400);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new MenuExample();
}
}
5. JComboBox in Swing.
ANS:
import javax.swing.*;
public class JComboboxDemo extends JFrame {
public JComboboxDemo()
{
String country[] = {"India", "Aus", "U.S.A", "England", "Newzeland"};
JComboBox cb=new JComboBox(country);
cb.setBounds(50, 50, 90, 20);
add(cb);
setLayout(null);
setSize(400, 500);
setVisible(true);
}
public static void main(String args[]) {
new JComboboxDemo();
}}
6. JTree.
ANS:
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
public class JTreeDemo
{
JTree tree;
JFrame f;
JTreeDemo(){
f=new JFrame();
f.setLayout(new BorderLayout());
DefaultMutableTreeNode top=new DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a=new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode b=new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode a1=new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2=new DefaultMutableTreeNode("A2");
a.add(a2);
DefaultMutableTreeNode b1=new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2=new DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3=new DefaultMutableTreeNode("B3");
b.add(b3);
tree=new JTree(top);
JScrollPane jsp=new JScrollPane(tree);
f.add(jsp, BorderLayout.CENTER);
f.setSize(500,500);
f.setVisible(true);
}
public static void main(String args[]){
JTreeDemo jl=new JTreeDemo();
}}
7. JTable.
ANS:
import javax.swing.*;
public class JTableDemo
{
JFrame f;
JTableDemo()
{
f=new JFrame();
String rows[][]={ {"101", "Amit", "670000"},
{"102", "Jai", "780000"},
{"101", "Sachin", "700000"}
};
String columns[]={"ID", "NAME", "SALARY"};
JTable jt=new JTable (rows, columns);
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 JTableDemo();
}
}
KeyListenerEXP10(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(500,500);
getContentPane().setBackground(new Color(255, 236, 0, 255));
label.setFont(lf);
label2.setFont(lf);
label3.setFont(lf);
label4.setFont(lf);
label.setVisible(true);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(false);
label.setBounds(30,40,400,50);
label2.setBounds(30,40,400,50);
label3.setBounds(30,40,400,50);
label4.setBounds(30,40,400,50);
tf.setBounds(100,120,400,50);
tf.setVisible(true);
add(label);
add(label2);
add(label3);
add(label4);
add(tf);
addKeyListener(this);
tf.addKeyListener(this);
setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
tf.setBackground(Color.green);
label.setVisible(false);
label2.setVisible(true);
label3.setVisible(false);
label4.setVisible(false);
}
@Override
public void keyTyped(KeyEvent e) {
tf.setBackground(Color.red);
label.setVisible(false);
label2.setVisible(false);
label3.setVisible(true);
label4.setVisible(false);
}
@Override
public void keyReleased(KeyEvent e) {
tf.setBackground(Color.blue);
label.setVisible(false);
label2.setVisible(false);
label3.setVisible(false);
label4.setVisible(true);
}
public static void main(String[] args) {
new KeyListenerEXP10();
}
}
10. WAP to demonstrate Mouse Event.
ANS:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
/*
<applet code="Practical11" width="300" height="300">
</applet>
*/
public class Practical11 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="MouseEntered";
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();
}
}
11. WAP to demonstrate the Use of JTextField and JPassword field.
ANS:
import javax.swing.*;
public class Example {
public static void main(String[] args) {
JFrame f=new JFrame();
JTextField id = new JTextField();
id.setBounds(100, 50, 100, 30);
f.add(id);
f.add(value);
f.add(l1);
f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}}
12. WAP that creates Username and pwd use setEchochar().
ANS:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code="TextFieldEvent" width=300 height=300></applet>
*/
public class TextFieldEvent extends Applet
{
TextField name,pass;
Button b1,b2;
public void init()
{
Label n=new Label("Name:",Label.CENTER);
Label p=new Label("Password:",Label.CENTER);
name=new TextField(20);
pass=new TextField(20);
pass.setEchoChar('*');
b1=new Button("Submit");
b2=new Button("Cancel");
add(n);
add(name);
add(p);
add(pass);
add(b1);
add(b2);
n.setBounds(70,90,90,60);
p.setBounds(70,100,90,60);
name.setBounds(280,100,90,20);
pass.setBounds(200,120,90,20);
b1.setBounds(100,260,70,40);
b2.setBounds(180,260,70,40);
}
public void paint(Graphics g)
{
repaint();
}}
13. WAP to perform basic arithmetic operation on 2 no’s using TextField and
Button to handle ActionEvent.
ANS:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
add(label1);
add(tf1);
add(label2);
add(tf2);
add(b1);
add(b2);
add(b3);
add(b4);
tf1.addActionListener(this);
tf2.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
if (whichButtonClk.equals("Add"))
g.drawString("Your sum is " + sum, 10, 190);
if (whichButtonClk.equals("Subtract"))
g.drawString("Your subtract is " + subtract, 10,190);
if (whichButtonClk.equals("Multiply"))
g.drawString("Your multiply is " + multiply, 10,190);
if (whichButtonClk.equals("Divide"))
g.drawString("Your divide is " + divide, 10, 190);
}
}
}
14. WAP to Implement Adapter class.
ANS:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseMotionAdapterExample extends MouseMotionAdapter{
JFrame f;
MouseMotionAdapterExample() {
f = new JFrame ("Mouse Motion Adapter");
f.addMouseMotionListener (this);
f.setLayout (null);
f.setVisible (true);
f.setSize(500,500);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void mouseDragged (MouseEvent e) {
Graphics g = f.getGraphics();
g.setColor (Color.GRAY);
g.fillOval (e.getX(), e.getY(), 10, 20);
}
public static void main(String[] args) {
new MouseMotionAdapterExample();
}
}
conn.close();
} catch(Exception e)
{
System.out.println(e.getMessage());
}}}