0% found this document useful (0 votes)
5 views44 pages

Fukte Runner

Uploaded by

gauribhalerao965
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)
5 views44 pages

Fukte Runner

Uploaded by

gauribhalerao965
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/ 44

AJP Practical Exam Programs

Q.1] WAP to demonstrate the use of AWT Components like Label,


TextField, TextArea, Button, CheckBox, RadioButton.
Ans.
import java.awt.*;
import java.awt.event.*;

class VJTechSoft extends Frame implements ActionListener {

// Declare AWT components


Label L1, L2, L3, L4, L6, L15, L16; // Removed L7, L8, L9, L10, L11, L12, L13, L14
Button b1;
TextField tf1, tf2; // Removed tf3 (City field)
TextArea taAddress; // TextArea for address
Checkbox c1, c2, lang1, lang2, lang3, lang4, lang5, lang6;
CheckboxGroup cbg;

VJTechSoft() {
// Set layout and colors
setLayout(null);
setBackground(Color.orange);
setForeground(Color.black);

// Fonts for styling


Font f1 = new Font("Arial Black", Font.BOLD | Font.ITALIC, 25);
Font f2 = new Font("Arial", Font.BOLD, 15); // Bold font for labels
// Initialize labels with bold font
L1 = new Label("VJTECH SOFTWARE PVT LTD", Label.CENTER);
L1.setFont(f1);
L1.setBackground(Color.yellow);

L2 = new Label("Enter Name:", Label.RIGHT);


L2.setFont(f2);

L3 = new Label("Select Gender:", Label.RIGHT);


L3.setFont(f2);

L4 = new Label("Enter Mobile Number:", Label.RIGHT);


L4.setFont(f2);

L6 = new Label();
L6.setForeground(Color.red);

// New label for "Select Language"


L15 = new Label("Select Language:", Label.RIGHT);
L15.setFont(f2);

// New label for "Enter Address"


L16 = new Label("Enter Address:", Label.RIGHT);
L16.setFont(f2);

// Set result label colors


L6.setForeground(Color.blue);

// Initialize button and text fields


b1 = new Button("Submit");
tf1 = new TextField(20); // Name field
tf2 = new TextField(20); // Mobile field

// Initialize checkbox group and gender checkboxes


cbg = new CheckboxGroup();
c1 = new Checkbox("Male", false, cbg);
c2 = new Checkbox("Female", false, cbg);

// Initialize checkboxes for languages


lang1 = new Checkbox("C lang");
lang2 = new Checkbox("C++ lang");
lang3 = new Checkbox("PHP lang");
lang4 = new Checkbox("CSS lang");
lang5 = new Checkbox("Python lang");
lang6 = new Checkbox("Java lang");

// Initialize TextArea for address


taAddress = new TextArea();
taAddress.setBackground(Color.lightGray);
taAddress.setRows(3); // Set number of rows for better display

// Set component bounds for layout


L1.setBounds(150, 100, 400, 40);
L2.setBounds(100, 200, 150, 40);
tf1.setBounds(300, 200, 150, 40);
L3.setBounds(100, 250, 150, 40);
c1.setBounds(300, 250, 60, 40);
c2.setBounds(400, 250, 100, 40);
L4.setBounds(100, 300, 170, 40);
tf2.setBounds(300, 300, 150, 40);

// Set bounds for "Select Language" label and language checkboxes


L15.setBounds(100, 350, 150, 40); // "Select Language" label
lang1.setBounds(300, 350, 150, 40);
lang2.setBounds(300, 400, 150, 40);
lang3.setBounds(300, 450, 150, 40);
lang4.setBounds(300, 500, 150, 40);
lang5.setBounds(300, 550, 150, 40);
lang6.setBounds(300, 600, 150, 40);

// Set bounds for "Enter Address" label and TextArea for address
L16.setBounds(100, 650, 150, 40); // "Enter Address" label
taAddress.setBounds(300, 650, 300, 80); // TextArea for address

// Set bounds for submit button and confirmation label


b1.setBounds(150, 750, 150, 40);
L6.setBounds(350, 750, 500, 40);

// Add action listener to the button


b1.addActionListener(this);

// Add components to the frame


add(L1);
add(L2);
add(L3);
add(L4);
add(L6);
add(L15); // Add "Select Language" label
add(L16); // Add "Enter Address" label
add(b1);
add(tf1);
add(tf2);
add(c1);
add(c2);
add(lang1);
add(lang2);
add(lang3);
add(lang4);
add(lang5);
add(lang6);
add(taAddress); // Add TextArea for address
}

// Action performed when submit button is clicked


public void actionPerformed(ActionEvent ae) {
// Collect data from the form
String name = tf1.getText();
String gender = cbg.getSelectedCheckbox() != null ?
cbg.getSelectedCheckbox().getLabel() : "Not selected";
String mobile = tf2.getText();
String address = taAddress.getText();

// Collect selected programming languages


StringBuilder selectedLanguages = new StringBuilder();
if (lang1.getState()) selectedLanguages.append("C, ");
if (lang2.getState()) selectedLanguages.append("C++, ");
if (lang3.getState()) selectedLanguages.append("PHP, ");
if (lang4.getState()) selectedLanguages.append("CSS, ");
if (lang5.getState()) selectedLanguages.append("Python, ");
if (lang6.getState()) selectedLanguages.append("Java");

// Remove trailing comma and space


if (selectedLanguages.length() > 0) {
selectedLanguages.setLength(selectedLanguages.length() - 2);
}

// Display confirmation in the label (not at the bottom)


L6.setText("Records Submitted Successfully!!!");

// Do not display the entered data in labels at the bottom.


}

// Main method to run the application


public static void main(String args[]) {
VJTechSoft v1 = new VJTechSoft();
v1.setVisible(true);
v1.setTitle("VJTech Software PVT.LTD.");
v1.setSize(900, 1000); // Increased size to fit all components
}
}

Q.2] WAP to design Student Registration Form using components like


list and choise.
Ans.
import java.awt.*;
import java.awt.event.*;
class StudentRegistrationForm extends Frame implements ActionListener {

Label L1, L2, L3, L4, L5, L6;


TextField tfName, tfMobile;
Choice chDepartment;
List lstSubjects;
CheckboxGroup genderGroup;
Checkbox male, female;
Button submitButton;
Label resultLabel;

StudentRegistrationForm() {
setLayout(null);
setBackground(Color.lightGray);
setForeground(Color.black);

Font fontTitle = new Font("Arial", Font.BOLD, 20);


Font fontLabel = new Font("Arial", Font.BOLD, 15);

L1 = new Label("Student Registration Form", Label.CENTER);


L1.setFont(fontTitle);
L1.setBackground(Color.yellow);

L2 = new Label("Enter Name:", Label.RIGHT);


L2.setFont(fontLabel);
L3 = new Label("Select Gender:", Label.RIGHT);
L3.setFont(fontLabel);

L4 = new Label("Enter Mobile Number:", Label.RIGHT);


L4.setFont(fontLabel);

L5 = new Label("Select Department:", Label.RIGHT);


L5.setFont(fontLabel);

L6 = new Label("Select Subjects:", Label.RIGHT);


L6.setFont(fontLabel);

resultLabel = new Label();


resultLabel.setForeground(Color.red);
tfName = new TextField(20);
tfMobile = new TextField(20);

chDepartment = new Choice();


chDepartment.add("Computer");
chDepartment.add("Electronics");
chDepartment.add("Information Technology");
chDepartment.add("Civil");

lstSubjects = new List();


lstSubjects.add("C Lang");
lstSubjects.add("C++ Lang");
lstSubjects.add("Java Lang");
lstSubjects.add("Python Lang");
lstSubjects.add("Css Lang");
lstSubjects.setMultipleMode(true);

genderGroup = new CheckboxGroup();


male = new Checkbox("Male", genderGroup, false);
female = new Checkbox("Female", genderGroup, false);

submitButton = new Button("Submit");


submitButton.addActionListener(this);

L1.setBounds(150, 50, 300, 40);


L2.setBounds(100, 150, 150, 30);
tfName.setBounds(300, 150, 150, 30);

L3.setBounds(100, 200, 150, 30);


male.setBounds(300, 200, 60, 30);
female.setBounds(400, 200, 80, 30);

L4.setBounds(100, 250, 180, 30);


tfMobile.setBounds(300, 250, 150, 30);

L5.setBounds(100, 300, 150, 30);


chDepartment.setBounds(300, 300, 150, 30);

L6.setBounds(100, 350, 150, 30);


lstSubjects.setBounds(300, 350, 150, 100);

submitButton.setBounds(200, 500, 100, 30);


resultLabel.setBounds(100, 550, 500, 30);

add(L1);
add(L2);
add(tfName);
add(L3);
add(male);
add(female);
add(L4);
add(tfMobile);
add(L5);
add(chDepartment);
add(L6);
add(lstSubjects);
add(submitButton);
add(resultLabel);
}

public void actionPerformed(ActionEvent ae) {

String resultText = "Registration Successful!\n";


resultLabel.setText(resultText);
}

public static void main(String[] args) {


StudentRegistrationForm form = new StudentRegistrationForm();
form.setVisible(true);
form.setTitle("Student Registration Form");
form.setSize(600, 650);
}
}

Q.3] WAP to show following output.

Ans.
import java.awt.*;
import javax.swing.*;
class seasonslist extends JFrame
{
seasonslist()
{
Container c=getContentPane();
setLayout(new FlowLayout());
String str[]={"summer","winter","rainy"};
JList j1=new JList(str);
c.add(j1);

}
public static void main(String args[])
{
seasonslist s=new seasonslist();
s.setVisible(true);
s.setSize(500,500);
s.setTitle("subjects");
s.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Ans.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class subject extends JFrame
{
JComboBox cb;
subject()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
String str[]={"Maths","Physics","Chemistry","Java","c+
+","javascript","php","python","ajp","c"};
cb=new JComboBox(str);
c.add(cb);
}
public static void main(String args[])
{
subject s=new subject();
s.setVisible(true);
s.setSize(500,500);
s.setTitle("subjects");
s.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Q.4] WAP to create 5 cards of fruits name using card layouts.


Ans.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutExample extends JFrame implements ActionListener
{
CardLayout card;
JButton b1,b2,b3,b4,b5;
Container c;
CardLayoutExample()
{
Font f1=new Font("Arial Black",Font.ITALIC,22);
setFont(f1);
c=getContentPane();
card=new CardLayout(40,30);
c.setLayout(card);
b1=new JButton("Apple");
b2=new JButton("Orange");
b3=new JButton("Grapes");
b4=new JButton("Strawberry");
b5=new JButton("Mango");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);

c.add("a",b1);
c.add("b",b2);
c.add("c",b3);
c.add("d",b4);
c.add("e",b5);
}
public void actionPerformed(ActionEvent e)
{
card.next(c);
}
public static void main(String args[])
{
CardLayoutExample c1=new CardLayoutExample();
c1.setVisible(true);
c1.setSize(400,400);
c1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Q.5] Write a program using AWT to create a MenuBar containing


menu items such as file,edit,view and create a sub menu under the file
menu new and open
Ans.
import java.awt.*;
import java.awt.event.*;

public class MenuBarExample extends Frame


{

public MenuBarExample()
{
MenuBar = new MenuBar();
Menu fileMenu = new Menu("File");
MenuItem newItem = new MenuItem("New");
MenuItem openItem = new MenuItem("Open");
fileMenu.add(newItem);
fileMenu.add(openItem);
menuBar.add(fileMenu );
Menu editMenu = new Menu("Edit");
menuBar.add(editMenu);
Menu viewMenu = new Menu("View");
menuBar.add(viewMenu);
setMenuBar(menuBar);

}
public static void main(String[] args)
{
MenuBarExample me=new MenuBarExample();
me.setTitle("AWT MenuBar Example");
me.setSize(400, 300);
me.setVisible(true);
}
}

Q.6] Write a program which creates menu of different colours and


disable menu item for black colour
Ans.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ColorMenuExample {


public static void main(String[] args) {
// Create the frame for the application
JFrame frame = new JFrame("Color Menu Example");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create a menu bar


JMenuBar menuBar = new JMenuBar();
// Create a menu
JMenu colorMenu = new JMenu("Colors");

// Create menu items


JMenuItem redItem = new JMenuItem("Red");
JMenuItem greenItem = new JMenuItem("Green");
JMenuItem blueItem = new JMenuItem("Blue");
JMenuItem blackItem = new JMenuItem("Black");

// Disable the "Black" menu item


blackItem.setEnabled(false);

// Add menu items to the menu


colorMenu.add(redItem);
colorMenu.add(greenItem);
colorMenu.add(blueItem);
colorMenu.add(blackItem);

// Add the color menu to the menu bar


menuBar.add(colorMenu);

// Set the menu bar for the frame


frame.setJMenuBar(menuBar);

// Create a panel for displaying background color


JPanel panel = new JPanel();
frame.add(panel);

// Add action listeners for menu items


redItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.RED);
}
});

greenItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.GREEN);
}
});

blueItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.BLUE);
}
});

// Display the frame


frame.setVisible(true);
}
}

Q.7] Write a program using swing to display a scrollPane and


JComboBox in an JApplet with the items- English,
Marathi, Hindi, Sanskrit
Ans.
import javax.swing.*;
import java.awt.*;
public class comboBoxApplet extends JApplet {
public void init() {

setLayout(null);

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


comboBox.addItem("English");
comboBox.addItem("Marathi");
comboBox.addItem("Hindi");
comboBox.addItem("Sanskrit");

JTextArea textArea = new JTextArea(5, 20);


JScrollPane scrollPane = new JScrollPane(textArea);

comboBox.setBounds(50, 50, 150, 30);


scrollPane.setBounds(50, 100, 300, 100);

JLabel label = new JLabel("Select a Language:");


label.setBounds(50, 20, 150, 30);

add(label);
add(comboBox);
add(scrollPane);
}
}
Q.8]
i) Write a program to develop frame to select the different states of
India using JComboBox
Ans.
import javax.swing.*;
import java.awt.*;
public class ComboboxDemoDemo extends JFrame
{
JComboBox jc;
ComboboxDemoDemo()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
String state[]={"Maharashtra", "Gujrat", "Delhi", "Rajsthan","Asam","Andhra
Pradesh","Arunachal Pradesh","Bihar","Chattisgadh","goa","Haryana","Himachal
Pradesh","Zarkhand","Karnataka","Kerala","Madhya
Pradesh","Manipur","Meghalaya","Mizoram","Nagaland","Odisha","Panjab","Sikkim","Tamil
nadu","Telangana","Tripura","Uttar Pradesh"};
jc = new JComboBox<>(state);
c.add(jc);
}
public static void main(String args[])
{
ComboboxDemoDemo cb =new ComboboxDemoDemo();
cb.setVisible(true);
cb.setSize(400,400);
cb.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
ii) Develop a program to demonstrate the use of ScrollPane in swings
Ans.
import javax.swing.*;
import java.awt.*;

public class ScrollPaneFrameExample


{
public static void main(String[] args) {

JFrame frame = new JFrame("ScrollPane Example");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);

JTextArea textArea = new JTextArea(10, 30);

textArea.setText("Scroll down to see more text.\n");


textArea.setEditable(true);

JScrollPane scrollPane = new JScrollPane(textArea);

frame.add(scrollPane);

frame.setVisible(true);
}
}

Q.9] Write a program to create a JTree


Ans.
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class demoJTreePract6 extends JFrame
{
demoJTreePract6()
{
Container c=getContentPane();
c.setLayout(new BorderLayout());

DefaultMutableTreeNode i=new DefaultMutableTreeNode("India");


DefaultMutableTreeNode m=new DefaultMutableTreeNode("maharashtra");
DefaultMutableTreeNode g=new DefaultMutableTreeNode("gujrath");
i.add(m);
i.add(g);
DefaultMutableTreeNode mu=new DefaultMutableTreeNode("mumbai");
DefaultMutableTreeNode pu=new DefaultMutableTreeNode("pune");
DefaultMutableTreeNode na=new DefaultMutableTreeNode("nashik");
DefaultMutableTreeNode nag=new DefaultMutableTreeNode("nagpur");
m.add(mu);
m.add(pu);
m.add(na);
m.add(nag);
JTree jt=new JTree(i);
c.add(jt,BorderLayout.WEST);

}
public static void main(String args[]) {
demoJTreePract6 f1 = new demoJTreePract6();
f1.setVisible(true);
f1.setSize(600, 600);
f1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Q.10] Write a program code to generate the following output

Ans.
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class demoJTreePract6 extends JFrame
{
demoJTreePract6()
{
Container c=getContentPane();
c.setLayout(new BorderLayout());

DefaultMutableTreeNode i=new DefaultMutableTreeNode("India");


DefaultMutableTreeNode m=new DefaultMutableTreeNode("maharashtra");
DefaultMutableTreeNode g=new DefaultMutableTreeNode("gujrath");
i.add(m);
i.add(g);
DefaultMutableTreeNode mu=new DefaultMutableTreeNode("mumbai");
DefaultMutableTreeNode pu=new DefaultMutableTreeNode("pune");
DefaultMutableTreeNode na=new DefaultMutableTreeNode("nashik");
DefaultMutableTreeNode nag=new DefaultMutableTreeNode("nagpur");
m.add(mu);
m.add(pu);
m.add(na);
m.add(nag);
JTree jt=new JTree(i);
c.add(jt,BorderLayout.WEST);

}
public static void main(String args[]) {
demoJTreePract6 f1 = new demoJTreePract6();
f1.setVisible(true);
f1.setSize(600, 600);
f1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Q.11] Write a program to create a JTable


Ans.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
public class JTableDemo extends JFrame implements ActionListener {
JLabel l1, l2, l3;
JButton b1;
JTextField tf1, tf2;
JTable t1;
DefaultTableModel model;

JTableDemo() {
Container c = getContentPane();
c.setLayout(null);

l1 = new JLabel("Name:");
l2 = new JLabel("Age:");
l3 = new JLabel(" ");

l1.setBounds(80, 120, 200, 40);


l2.setBounds(100, 180, 200, 40);

tf1 = new JTextField(50);


tf2 = new JTextField( 20);

tf1.setBounds(150, 130, 200, 40);


tf2.setBounds(150, 180, 200, 40);

b1 = new JButton("Add");
b1.setBounds(100, 250, 200, 40);
l3.setBounds(100, 280, 200, 40);
c.add(l1);
c.add(l2);
c.add(l3);
c.add(b1);
c.add(tf1);
c.add(tf2);

// Setting up table
String colName[] = {"Name", "Age"};
model = new DefaultTableModel(null, colName); // Initially no data
t1 = new JTable(model);
JScrollPane js = new JScrollPane(t1);
js.setBounds(100, 500, 400, 100);
c.add(js);

b1.addActionListener(this);
}

public void actionPerformed(ActionEvent ae) {


String name = tf1.getText();
String age = tf2.getText();

if (ae.getSource() == b1) {
// Add data to the table model
model.addRow(new Object[]{name, age});
l3.setText("Successfully added");
tf1.setText("");
tf2.setText("");
}
}

public static void main(String args[]) {


JTableDemo f1 = new JTableDemo();
f1.setVisible(true);
f1.setSize(600, 600);
f1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Q.12]
i) Write a program to launch a progressBar
Ans.
import java.awt.*;
import javax.swing.*;

class JProgressBarDemo extends JFrame {


JProgressBar jpb;

JProgressBarDemo() {
Container c = getContentPane();
c.setLayout(null);
c.setBackground(Color.orange);

// Initialize progress bar


jpb = new JProgressBar(0, 3000);
jpb.setBounds(200, 200, 200, 40);
jpb.setValue(0);
jpb.setStringPainted(true);

// Add progress bar to container


c.add(jpb);
}

public void ChangeProgressBarValue() {


// Run progress bar update in a separate thread
new Thread(() -> {
int i = 0;
while (i <= 3000) {
jpb.setValue(i);
i += 20;

try {
Thread.sleep(150); // Delay for smoother progress
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}

public static void main(String args[]) {


// Create and configure the JFrame
JProgressBarDemo j1 = new JProgressBarDemo();
j1.setVisible(true);
j1.setTitle("JProgressBar Demo");
j1.setSize(700, 700);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Start progress bar update


j1.ChangeProgressBarValue();
}
}

ii) Write a program using JProgressBar to show the progress of


ProgressBar when user clicks on JButton
Ans.
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class JProgressBarBtn extends JFrame {


JProgressBar jpb;
JButton showProgressButton;

JProgressBarBtn() {
Container c = getContentPane();
c.setLayout(null);
c.setBackground(Color.orange);

// Initialize progress bar


jpb = new JProgressBar(0, 3000);
jpb.setBounds(200, 200, 200, 40);
jpb.setValue(0);
jpb.setStringPainted(true);
jpb.setVisible(false); // Initially hidden

// Initialize button
showProgressButton = new JButton("Show Progress");
showProgressButton.setBounds(200, 300, 200, 40);

// Add components to container


c.add(jpb);
c.add(showProgressButton);

// Add action listener to the button


showProgressButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jpb.setVisible(true); // Show progress bar when button is clicked
ChangeProgressBarValue();
}
});
}

public void ChangeProgressBarValue() {


// Run progress bar update in a separate thread
new Thread(() -> {
int i = 0;
while (i <= 3000) {
jpb.setValue(i);
i += 20;

try {
Thread.sleep(150); // Delay for smoother progress
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}

public static void main(String args[]) {


// Create and configure the JFrame
JProgressBarBtn j1 = new JProgressBarBtn();
j1.setVisible(true);
j1.setTitle("JProgressBar Demo");
j1.setSize(700, 700);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Q.13] Write a program to demonstrate status of key on Applet


window such as KeyPressed, KeyReleased, KeyUp, KeyDown
Ans.
import java.awt.event.*;
import java.awt.*;
public class keyDemo extends Frame implements KeyListener
{
keyDemo()
{
setLayout(new FlowLayout());
TextField tf1=new TextField(20);
add(tf1);
tf1.addKeyListener(this);
}
public void keyTyped(KeyEvent ke)
{
System.out.println("Key Typed");
}
public void keyPressed(KeyEvent ke)
{
System.out.println("Key Pressed");
}
public void keyReleased(KeyEvent ke)
{
System.out.println("Key Released");
}
public static void main(String args[])
{
keyDemo c1=new keyDemo();
c1.setVisible(true);
c1.setSize(500,500);
}
}

Q.14] Develop a program to accept two numbers and display product


of two numbers when user pressed "Multiply" button
Ans.
import java.awt.*;
import java.awt.event.*;
class MultiplyDemo extends Frame implements ActionListener {
TextField t1, t2, t3; // Correctly named variables
Button b1;

MultiplyDemo() {
// Frame setup
setLayout(null);
setBackground(Color.cyan); // Correct syntax

// Create and position components


Label l1 = new Label("Enter First No:- ", Label.RIGHT);
Label l2 = new Label("Enter Second No:- ", Label.RIGHT);
Label l3 = new Label("Result= ", Label.RIGHT);

t1 = new TextField();
t2 = new TextField();
t3 = new TextField();
t3.setEditable(false); // Result field should not be editable

b1 = new Button("Multiply");

l1.setBounds(100, 100, 150, 40);


t1.setBounds(260, 100, 200, 40);

l2.setBounds(100, 160, 150, 40);


t2.setBounds(260, 160, 200, 40);

l3.setBounds(100, 220, 150, 40);


t3.setBounds(260, 220, 200, 40);
b1.setBounds(180, 280, 100, 40);

// Add components to frame


add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);

// Add ActionListener to the button


b1.addActionListener(this);

// Frame settings
setTitle("Multiplication Demo");
setSize(500, 400);
setVisible(true);

// Close operation
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

// Handle button click event


public void actionPerformed(ActionEvent ae) {
try {
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());
int c = a * b; // Multiplication
t3.setText(String.valueOf(c));
} catch (NumberFormatException e) {
t3.setText("Invalid Input");
}
}

// Main method
public static void main(String[] args) {
new MultiplyDemo();
}
}

Q.15] Write a program to demonstrate various mouse events using


mouse listener and mouse motion listener interface
&
Q.18] Write a program to demonstrate use of MouseDragged
and MouseMoved
Ans.
import java.awt.event.*;
import java.awt.*;
public class mouseDemo extends Frame implements MouseListener,MouseMotionListener
{
mouseDemo()
{
setLayout(new FlowLayout());
TextField tf1=new TextField(20);
add(tf1);
tf1.addMouseListener(this);
tf1.addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
System.out.println("Mouse Clicked");
}
public void mouseEntered(MouseEvent me)
{
System.out.println("Mouse Entered");
}
public void mouseReleased(MouseEvent me)
{
System.out.println("Mouse Released");
}
public void mouseExited(MouseEvent me)
{
System.out.println("Mouse Exited");
}
public void mousePressed(MouseEvent me)
{
System.out.println("Mouse Pressed");
}
public void mouseDragged(MouseEvent me)
{
System.out.println("Mouse Dragged");
}
public void mouseMoved(MouseEvent me)
{
System.out.println("Mouse Moved");
}
public static void main(String args[])
{
mouseDemo c1=new mouseDemo();
c1.setVisible(true);
c1.setSize(500,500);
}
}

Q.16] Write a program to change the background colour of applet


when user performs events using mouse
Ans.
import java.awt.*;
import java.awt.event.*;
class BackgroundColorDemo extends Frame implements
ActionListener
{
Button b1,b2,b3;
BackgroundColorDemo()
{
setLayout(null);
setBackground(Color.red);
setForeground(Color.black);
b1=new Button("yellow");
b2=new Button("pink");
b3=new Button("green");
b1.setBounds(200,150,150,40);
b2.setBounds(400,150,150,40);
b3.setBounds(600,150,150,40);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
add(b1);
add(b2);
add(b3);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand(); /*If we use
getActionCommand method for one color then again sub color we
can use getSource method then we can perform the getSource
method and getActionCommand has a same use*/
if(str.equals("yellow"))
{
setBackground(Color.yellow);
}
else if(ae.getSource()==b2)
{
setBackground(Color.pink);
}
else if(ae.getSource()==b3)
{
setBackground(Color.green);
}
}
public static void main(String args[])
{
BackgroundColorDemo bcd=new
BackgroundColorDemo();
bcd.setVisible(true);
bcd.setTitle("Change Background Color");
bcd.setSize(850,850);
}
}

Q.17] Write a program to count the number of clicks performed by the


user in a frame window
Ans.
import javax.swing.*;
import java.awt.event.*;
public class ClickCounter22 {
int clickCount = 0;
public ClickCounter22()
{
JFrame frame = new JFrame("Click Counter");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
JLabel label = new JLabel("Click Count: 0");
label.setBounds(150,100,200,30);
frame.add(label);
frame.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
clickCount++;
label.setText("Click Count: " + clickCount);
}
});
frame.setVisible(true);
System.out.println("Program started");
}
public static void main(String[] args)
{
new ClickCounter22();
}
}

Q.19] Write a program to demonstrate use of JTextField and


JPasswordField using listener interface
Ans.
import java.awt.*;
import java.awt.event.*;
class LoginPage2 extends Frame implements ActionListener
{
Label vj;
TextField tf1,tf2;
LoginPage2()
{
setLayout(null);
setBackground(Color.cyan);
setForeground(Color.black);
Font f1=new Font("Arial Black",Font.BOLD,25);
Font f2=new Font("Arial Black",Font.BOLD,15);
vj=new Label(" ");
Label L1=new Label("VJTech Software PVT LTD");
Label L2=new Label("Enter User Name:");
Label L3=new Label("Enter Password:");
L1.setFont(f1);
setFont(f2);
tf1=new TextField(15);
tf2=new TextField(15);
tf2.setEchoChar('*');
Button b1=new Button("Login");
b1.addActionListener(this);//Action Registration
L1.setBounds(200,200,500,40);
L2.setBounds(200,300,150,40);
tf1.setBounds(340,300,150,40);
L3.setBounds(200,350,150,40);
tf2.setBounds(340,350,150,40);
b1.setBounds(300,430,90,40);
vj.setBounds(300,500,250,40);
add(L1);
add(L2);
add(tf1);
add(L3);
add(tf2);
add(b1);
add(vj);

}
public void actionPerformed(ActionEvent ae)//Event
{
String un=tf1.getText();
String psw=tf2.getText();
if(un.equals("KARAN") && psw.equals("KARAN"))
{
vj.setText("Login Successful!!!");
}
else
{
vj.setText("Login Fail!!!");
}
}
public static void main(String args[])
{
LoginPage2 lp=new LoginPage2();
lp.setVisible(true);
lp.setTitle("Login Page");
lp.setSize(700,700);
}
}

Q.20] Write a program to demonstrate use of Window Adapter Class


Ans.
import java.awt.event.*;
import java.awt.*;
public class windowDemo extends Frame implements WindowListener
{
windowDemo()
{
addWindowListener(this);
}
public void windowActivated(WindowEvent we)
{
System.out.println("Window Activated");
}
public void windowDeactivated(WindowEvent we)
{
System.out.println("Window Deactivated");
}
public void windowOpened(WindowEvent we)
{
System.out.println("Window Opened");
}
public void windowClosed(WindowEvent we)
{
System.out.println("Window Closed");
}
public void windowClosing(WindowEvent we)
{
System.out.println("Window Closing");
}
public void windowIconified(WindowEvent we)
{
System.out.println("Window Iconified");
}
public void windowDeiconified(WindowEvent we)
{
System.out.println("window Deiconified");
}
public void windowGainfocus(WindowEvent we)
{
System.out.println("Window Gainfocus");
}
public void windowLostfocus(WindowEvent we)
{
System.out.println(" Window Lostfocus");
}
public static void main(String args[])
{
windowDemo c1=new windowDemo();
c1.setVisible(true);
c1.setSize(500,500);
}
}

You might also like