0% found this document useful (0 votes)
12 views6 pages

Practical 14

This document is a lab manual for Java programming that includes practical exercises using AWT components to create user interfaces. It provides code examples for applications demonstrating radio buttons, checkboxes, text fields, buttons, and lists. The manual is designed for educational purposes, guiding students through the development of simple Java GUI applications.

Uploaded by

Huzeefa Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Practical 14

This document is a lab manual for Java programming that includes practical exercises using AWT components to create user interfaces. It provides code examples for applications demonstrating radio buttons, checkboxes, text fields, buttons, and lists. The manual is designed for educational purposes, guiding students through the development of simple Java GUI applications.

Uploaded by

Huzeefa Pathan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Programming Practical:14 Lab Manual (Solved)

Resource required (additional)


Name of Resource Broad Specification Remark if any
Java Development Kit
java 17.0.4 For Java compilation
(JDK)
Integrated To simplify Java coding with syntax
Development VS code highlighting, debugging, and suggestions.
Environment (IDE)

Conclusion
In this practical, we wrote a program to design a form using AWT components. We used
components like buttons, text fields, and labels to create a basic user interface in Java.

Practical related Questions


1. Design an applet/application to demonstrate the use of Radio Button and Checkbox.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Swi extends JFrame implements ActionListener {


JRadioButton rb1, rb2;
JCheckBox cb1, cb2;
JButton submitButton;
JLabel resultLabel;

Swi() {
setTitle("Radio Button & Checkbox Demo");
setSize(400, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Radio Buttons
rb1 = new JRadioButton("Male");
rb2 = new JRadioButton("Female");
rb1.setBounds(50, 30, 100, 30);
rb2.setBounds(150, 30, 100, 30);

Jamia Polytechnic Akkalkuwa Page No:1 Prepared by: Sayyed Waliullah


Java Programming Practical:14 Lab Manual (Solved)

// Grouping Radio Buttons


ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(rb1);
genderGroup.add(rb2);

// Checkboxes
cb1 = new JCheckBox("Java");
cb2 = new JCheckBox("Python");
cb1.setBounds(50, 80, 100, 30);
cb2.setBounds(150, 80, 100, 30);

// Submit Button
submitButton = new JButton("Submit");
submitButton.setBounds(100, 130, 100, 30);
submitButton.addActionListener(this);

// Result Label
resultLabel = new JLabel("");
resultLabel.setBounds(50, 180, 300, 30);

// Adding Components to Frame


add(rb1);
add(rb2);
add(cb1);
add(cb2);
add(submitButton);
add(resultLabel);

setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String gender = rb1.isSelected() ? "Male" : rb2.isSelected() ? "Female" : "Not Selected";
String skills = "";
if (cb1.isSelected()) skills += "Java ";
if (cb2.isSelected()) skills += "Python";
if (skills.isEmpty()) skills = "No skills selected";
resultLabel.setText("Gender: " + gender + ", Skills: " + skills);
}
Jamia Polytechnic Akkalkuwa Page No:2 Prepared by: Sayyed Waliullah
Java Programming Practical:14 Lab Manual (Solved)

public static void main(String[] args) {


new Swi();
// it automatically creates an instance of the Swi class, which is a JFrame-based GUI
application.
// we can write like this as well:
// Swi obj = new Swi(); // Explicitly creating an object
}
}
2. Design an applet/application to create form using Text Filed, Text Area, Button and
Label

import javax.swing.*;
import java.awt.event.*;

class FormApp extends JFrame implements ActionListener {


JTextField nameField, emailField;
JTextArea addressArea;
JButton submitButton;
JLabel resultLabel;

FormApp() {
setTitle("Simple Form");
setSize(300, 300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

nameField = new JTextField(); nameField.setBounds(100, 20, 150, 25);


emailField = new JTextField(); emailField.setBounds(100, 50, 150, 25);
addressArea = new JTextArea(); addressArea.setBounds(100, 80, 150, 50);

submitButton = new JButton("Submit");


submitButton.setBounds(100, 140, 100, 30);
submitButton.addActionListener(this);

resultLabel = new JLabel(); resultLabel.setBounds(50, 180, 200, 30);

add(new JLabel("Name:")).setBounds(30, 20, 60, 25);


Jamia Polytechnic Akkalkuwa Page No:3 Prepared by: Sayyed Waliullah
Java Programming Practical:14 Lab Manual (Solved)

add(new JLabel("Email:")).setBounds(30, 50, 60, 25);


add(new JLabel("Address:")).setBounds(30, 80, 60, 25);

add(nameField); add(emailField); add(addressArea);


add(submitButton); add(resultLabel);

setVisible(true);
}

public void actionPerformed(ActionEvent e) {


resultLabel.setText("Submitted: " + nameField.getText());
}

public static void main(String[] args) {


new FormApp();
}
}
3. Write a program to create three Buttons with Caption OK RESET and CANCEL
import javax.swing.*;
import java.awt.event.*;

class ButtonApp extends JFrame implements ActionListener {


JButton okButton, resetButton, cancelButton;
JLabel resultLabel;

ButtonApp() {
setTitle("Button Example");
setSize(300, 200);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

okButton = new JButton("OK"); okButton.setBounds(30, 50, 80, 30);


resetButton = new JButton("RESET"); resetButton.setBounds(110, 50, 80, 30);
cancelButton = new JButton("CANCEL"); cancelButton.setBounds(190, 50, 80, 30);
resultLabel = new JLabel(); resultLabel.setBounds(50, 100, 200, 30);

add(okButton); add(resetButton); add(cancelButton); add(resultLabel);

Jamia Polytechnic Akkalkuwa Page No:4 Prepared by: Sayyed Waliullah


Java Programming Practical:14 Lab Manual (Solved)

okButton.addActionListener(this);
resetButton.addActionListener(this);
cancelButton.addActionListener(this);

setVisible(true);
}

public void actionPerformed(ActionEvent e) {


resultLabel.setText(e.getActionCommand() + " Clicked");
}

public static void main(String[] args) {


new ButtonApp();
}
}
4. Develop an applet/application using List components to add names of 10 different cities.
import javax.swing.*;
class CityListApp extends JFrame {
CityListApp() {
setTitle("City List");
setSize(250, 200);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String[] cities = {"Mumbai", "Delhi", "Chennai", "Kolkata", "Bangalore",


"Hyderabad", "Pune", "Ahmedabad", "Jaipur", "Lucknow"};

JList<String> cityList = new JList<>(cities);


JScrollPane scrollPane = new JScrollPane(cityList);
scrollPane.setBounds(50, 30, 150, 100);

add(scrollPane);
setVisible(true);
}
public static void main(String[] args) {
new CityListApp();
}
}
Jamia Polytechnic Akkalkuwa Page No:5 Prepared by: Sayyed Waliullah
Java Programming Practical:14 Lab Manual (Solved)

5. Develop applet/application to select multiple names of news papers.


import javax.swing.*;
class NewspaperApp extends JFrame {
NewspaperApp() {
setTitle("Select Newspapers");
setSize(250, 220);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String[] newspapers = {"Times of India", "Hindustan Times", "The Hindu", "Indian


Express",
"Dainik Bhaskar", "The Telegraph", "Economic Times", "The Tribune",
"Roznama Inquilab (Urdu)", "Siasat (Urdu)", "Loksatta (Marathi)"};

JList<String> newspaperList = new JList<>(newspapers);

newspaperList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTIO
N);
JScrollPane scrollPane = new JScrollPane(newspaperList);
scrollPane.setBounds(30, 30, 180, 120);

add(scrollPane);
setVisible(true);
}

public static void main(String[] args) {


new NewspaperApp();
}
}

Jamia Polytechnic Akkalkuwa Page No:6 Prepared by: Sayyed Waliullah

You might also like