0% found this document useful (0 votes)
9 views12 pages

Object Oriented Programming Using Java Laboratory (DJS23FLES201) F.Y B. Tech Semester: II Experiment No-14

The document outlines a laboratory experiment for F.Y B. Tech students focusing on Object Oriented Programming using Java. It includes program codes for a registration form, a basic calculator, and a user form, demonstrating GUI creation and event handling in Java. The conclusion emphasizes the learning outcome of collecting user data through GUI components and displaying it effectively.
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)
9 views12 pages

Object Oriented Programming Using Java Laboratory (DJS23FLES201) F.Y B. Tech Semester: II Experiment No-14

The document outlines a laboratory experiment for F.Y B. Tech students focusing on Object Oriented Programming using Java. It includes program codes for a registration form, a basic calculator, and a user form, demonstrating GUI creation and event handling in Java. The conclusion emphasizes the learning outcome of collecting user data through GUI components and displaying it effectively.
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/ 12

Object Oriented Programming using Java Laboratory (DJS23FLES201)

F.Y B. Tech Semester: II


(Academic Year 2024-25)
Experiment No-14

Name: Krish B Parmar SAP ID: 60019240112 Roll No:B032

Branch: CS(ICB) Div: B Batch:B-1

Program Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegistrationForm {


public static void main (String [] args) {
JFrame frame = new JFrame("Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
JLabel loginLabel = new JLabel("Login:");
JTextField loginField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
JPasswordField passwordField = new JPasswordField();

JButton okButton = new JButton("OK");


JButton resetButton = new JButton("RESET");
JTextField displayField = new JTextField();
displayField.setEditable(false);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed (ActionEvent e) {
String loginText = loginField.getText();
String passwordText = new String (passwordField.getPassword());
displayField.setText("Login: " + loginText + ", Password: " + passwordText);
panel.add(displayField); // Add the display field to the panel
frame.revalidate(); // Refresh the frame to show changes
}
});
resetButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed (ActionEvent e) {
loginField.setText("");
passwordField.setText("");
}
});
panel.add(loginLabel);
panel.add(loginField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(okButton);
panel.add(resetButton);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.setVisible(true);
}
}

Program output:
1. Write a program to create a basic calculator.

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

public class Calculator extends JFrame implements ActionListener {


private JTextField display;
private JButton[] buttons;
private String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
private String currentInput = "";
private double result = 0;
private String operator = "";
public Calculator() {
setTitle("Calculator");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

display = new JTextField();


display.setEditable(false);
add(display, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
buttons = new JButton[16];
for (int i = 0; i < 16; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
panel.add(buttons[i]);
}
add(panel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.charAt(0) >= '0' && command.charAt(0) <= '9' || command.equals(".")) {
currentInput += command;
display.setText(currentInput);
} else if (command.equals("=")) {
calculate();
display.setText(String.valueOf(result));
currentInput = "";
operator = "";
} else
{
if (!currentInput.isEmpty()) {
calculate();
operator = command;
currentInput = "";
}
}
}
private void calculate() {
double input = Double.parseDouble(currentInput);
switch (operator) {
case "+":
result += input;
break;
case "-":
result -= input;
break;
case "*":
result *= input;
break;
case "/":
result /= input;
break;
default:
result = input;
break;
}
}
public static void main(String [] args) {
SwingUtilities.invokeLater(() -> {
Calculator calculator = new Calculator ();
calculator.setVisible(true);
});
}
}
Program ouput:
Program code:

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class UserForm extends JFrame implements ActionListener {

private JTextField nameField;

private JRadioButton maleButton, femaleButton;

private JCheckBox musicCheckBox, swimmingCheckBox;

private JComboBox<String> placeComboBox;

private JTextArea detailsArea;

private JButton submitButton, exitButton;

public UserForm() {
setTitle("Welcome to fahmidasclassroom");

setSize(400, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new GridLayout(7, 2));

// Name

add(new JLabel("Name:"));

nameField = new JTextField();

add(nameField);

// Gender

add(new JLabel("Gender:"));

JPanel genderPanel = new JPanel();

maleButton = new JRadioButton("Male");

femaleButton = new JRadioButton("Female");

ButtonGroup genderGroup = new ButtonGroup();

genderGroup.add(maleButton);

genderGroup.add(femaleButton);

genderPanel.add(maleButton);

genderPanel.add(femaleButton);

add(genderPanel);

// Interest
add(new JLabel("Interest:"));

JPanel interestPanel = new JPanel();

musicCheckBox = new JCheckBox("Music");

swimmingCheckBox = new JCheckBox("Swimming");

interestPanel.add(musicCheckBox);

interestPanel.add(swimmingCheckBox);

add(interestPanel);

// Favourite Place

add(new JLabel("Favourite Place:"));

placeComboBox = new JComboBox<>(new String[]{"Satara"});

add(placeComboBox);

// Details

add(new JLabel("Details:"));

detailsArea = new JTextArea();

detailsArea.setEditable(false);

add(detailsArea);

// Buttons

submitButton = new JButton("Submit");

submitButton.addActionListener(this);

add(submitButton);
exitButton = new JButton("Exit");

exitButton.addActionListener(e -> System.exit(0));

add(exitButton);

public void actionPerformed(ActionEvent e) {

if (e.getSource() == submitButton) {

String name = nameField.getText();

String gender = maleButton.isSelected() ? "Male" : femaleButton.isSelected() ? "Female" : "";

String interests = "";

if (musicCheckBox.isSelected()) interests += "Music ";

if (swimmingCheckBox.isSelected()) interests += "Swimming ";

String place = (String) placeComboBox.getSelectedItem();

detailsArea.setText("Name: " + name + "\nGender: " + gender + "\nInterests: " + interests +


"\nFavourite Place: " + place);

public static void main(String[] args) {

SwingUtilities.invokeLater(() -> {

UserForm form = new UserForm();

form.setVisible(true);

});

}
}

Program Output:

Conclusion:

This experiment involved creating a form that displays selected inputs after clicking the Submit button.

Thus, we learnt how to collect user data from GUI components and display it using event handling.

You might also like