0% found this document useful (0 votes)
26 views21 pages

Lab Report 7 - 1734068151

The document contains various Java applications developed by a student named K. Aravind, including a login form, calculator, to-do list app, employee management system, and a traffic light simulation. Each application is implemented using Java Swing, showcasing different functionalities such as user input handling, event-driven programming, and GUI design. The document serves as a lab report for the student's Java course, submitted on January 23, 2025.
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)
26 views21 pages

Lab Report 7 - 1734068151

The document contains various Java applications developed by a student named K. Aravind, including a login form, calculator, to-do list app, employee management system, and a traffic light simulation. Each application is implemented using Java Swing, showcasing different functionalities such as user input handling, event-driven programming, and GUI design. The document serves as a lab report for the student's Java course, submitted on January 23, 2025.
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/ 21

Student Name K.

Aravind
Student Registration Number 231U1R2044 Class & Section : AIML-2B
Study Level : UG/PG UG Year & Term : 2nd year and Term
2
Subject Name Java

Name of the Assessment Lab report 7


Date of Submission 23/01/2025

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

public class LoginForm {


public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Login Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setLayout(new GridBagLayout());
frame.setLocationRelativeTo(null); // Center the frame on the screen

// Create components
JLabel userLabel = new JLabel("Username:");
JTextField userTextField = new JTextField(20);

JLabel passwordLabel = new JLabel("Password:");


JPasswordField passwordField = new JPasswordField(20);
JButton loginButton = new JButton("Login");
JButton cancelButton = new JButton("Cancel");

JLabel messageLabel = new JLabel();

// Set layout constraints


GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5); // Add spacing between components

// Add components to frame


gbc.gridx = 0;
gbc.gridy = 0;
frame.add(userLabel, gbc);

gbc.gridx = 1;
frame.add(userTextField, gbc);

gbc.gridx = 0;
gbc.gridy = 1;
frame.add(passwordLabel, gbc);

gbc.gridx = 1;
frame.add(passwordField, gbc);

gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
frame.add(loginButton, gbc);

gbc.gridy = 3;
frame.add(cancelButton, gbc);
gbc.gridy = 4;
frame.add(messageLabel, gbc);

// Add action listeners


loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = userTextField.getText();
String password = new String(passwordField.getPassword());

// Updated credentials check


if (username.equals("231U1R2044") && password.equals("rocky"))
{ messageLabel.setText("Login successful!");
messageLabel.setForeground(Color.GREEN);
} else {
messageLabel.setText("Invalid credentials. Try again.");
messageLabel.setForeground(Color.RED);
}
}
});

cancelButton.addActionListener(e -> {
userTextField.setText("");
passwordField.setText("");
messageLabel.setText("");
});

// Set frame visibility


frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator extends JFrame implements ActionListener {


// Components of the calculator
private JTextField textField;
private JButton[] numberButtons;
private JButton[] functionButtons;
private JButton addButton, subButton, mulButton, divButton;
private JButton decButton, equButton, delButton, clrButton;
private JPanel panel;

// Variables for calculation


private double num1 = 0, num2 = 0, result = 0;
private char operator;

public Calculator() {
// Frame settings
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 550);
setLayout(null);

// Text field
textField = new JTextField();
textField.setBounds(30, 25, 320, 50);
textField.setEditable(false);
add(textField);

// Buttons
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equButton = new JButton("=");
delButton = new JButton("Del");
clrButton = new JButton("Clr");

functionButtons = new JButton[]{addButton, subButton, mulButton, divButton,


decButton, equButton, delButton, clrButton};

for (JButton button : functionButtons) {


button.addActionListener(this);
}

numberButtons = new JButton[10];


for (int i = 0; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
}
// Panel for buttons
panel = new JPanel();
panel.setBounds(30, 100, 320, 400);
panel.setLayout(new GridLayout(5, 4, 10, 10));

// Adding buttons to the panel


panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);

panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);

panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);

panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);

panel.add(delButton);
panel.add(clrButton);

add(panel);

setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
textField.setText(textField.getText().concat(String.valueOf(i)));
}
}

if (e.getSource() == decButton) {
textField.setText(textField.getText().concat("."));
}

if (e.getSource() == addButton) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
}

if (e.getSource() == subButton) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
}

if (e.getSource() == mulButton) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
}

if (e.getSource() == divButton) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
}

if (e.getSource() == equButton) {
num2 = Double.parseDouble(textField.getText());

switch (operator)
{ case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
textField.setText(String.valueOf(result));
num1 = result;
}

if (e.getSource() == clrButton) {
textField.setText("");
}

if (e.getSource() == delButton)
{ String text =
textField.getText();
textField.setText(text.isEmpty() ? "" : text.substring(0, text.length() - 1));
}
}
public static void main(String[] args) {

new Calculator();
}
}

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

public class ToDoListApp {


public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("To-Do List Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(new BorderLayout());
// Create components
DefaultListModel<String> listModel = new DefaultListModel<>();
JList<String> taskList = new JList<>(listModel);
JScrollPane scrollPane = new JScrollPane(taskList);

JTextField taskField = new JTextField(20);


JButton addButton = new JButton("Add Task");
JButton deleteButton = new JButton("Delete Selected Task");
JButton clearButton = new JButton("Clear All Tasks");

// Top panel for adding tasks


JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Task:"));
topPanel.add(taskField);
topPanel.add(addButton);

// Bottom panel for action buttons


JPanel bottomPanel = new JPanel();
bottomPanel.add(deleteButton);
bottomPanel.add(clearButton);

// Add components to frame


frame.add(topPanel, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);

// Add button functionality


addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{ String task = taskField.getText().trim();
if (!task.isEmpty()) {
listModel.addElement(task);
taskField.setText(""); // Clear input field
} else {
JOptionPane.showMessageDialog(frame, "Task cannot be empty!",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
});

deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedIndex = taskList.getSelectedIndex();
if (selectedIndex != -1) {
listModel.remove(selectedIndex);
} else {
JOptionPane.showMessageDialog(frame, "No task selected!", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
});

clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listModel.clear();
}
});

// Display frame
frame.setVisible(true);
}
}
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EmployeeManagementSystem extends JFrame implements


ActionListener {
// Components
private JTextField idField, nameField, positionField, salaryField;
private JButton addButton, deleteButton, clearButton;
private JTable table;
private DefaultTableModel tableModel;

public EmployeeManagementSystem() {
// Frame settings
setTitle("Employee Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLayout(new BorderLayout());

// Top panel for input fields


JPanel inputPanel = new
JPanel();
inputPanel.setLayout(new GridLayout(2, 4, 10, 10));
inputPanel.setBorder(BorderFactory.createTitledBorder("Employee Details"));

inputPanel.add(new JLabel("ID:"));
idField = new JTextField();
inputPanel.add(idField);

inputPanel.add(new JLabel("Name:"));
nameField = new JTextField();
inputPanel.add(nameField);

inputPanel.add(new JLabel("Position:"));
positionField = new JTextField();
inputPanel.add(positionField);

inputPanel.add(new JLabel("Salary:"));
salaryField = new JTextField();
inputPanel.add(salaryField);

add(inputPanel, BorderLayout.NORTH);
// Center panel for JTable
tableModel = new DefaultTableModel(new Object[]{"ID", "Name", "Position",
"Salary"}, 0);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBorder(BorderFactory.createTitledBorder("Employee Records"));
add(scrollPane, BorderLayout.CENTER);

// Bottom panel for buttons


JPanel buttonPanel = new JPanel();
addButton = new JButton("Add");
deleteButton = new JButton("Delete");
clearButton = new JButton("Clear");

addButton.addActionListener(this);
deleteButton.addActionListener(this);
clearButton.addActionListener(this);

buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(clearButton);

add(buttonPanel, BorderLayout.SOUTH);

setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e)
{ if (e.getSource() == addButton) {
// Add employee details to the table
String id = idField.getText();
String name = nameField.getText();
String position = positionField.getText();
String salary = salaryField.getText();

if (id.isEmpty() || name.isEmpty() || position.isEmpty() || salary.isEmpty())


{ JOptionPane.showMessageDialog(this, "All fields must be filled", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

tableModel.addRow(new Object[]{id, name, position, salary});


clearFields();
}

if (e.getSource() == deleteButton) {
// Delete selected row
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
tableModel.removeRow(selectedRow);
} else {
JOptionPane.showMessageDialog(this, "Select a row to delete", "Error",
JOptionPane.ERROR_MESSAGE);
}
}

if (e.getSource() == clearButton) {
// Clear all rows
tableModel.setRowCount(0);
}
}

private void clearFields() {


idField.setText("");
nameField.setText("");
positionField.setText("");
salaryField.setText("");
}

public static void main(String[] args)


{ new
EmployeeManagementSystem();
}
}

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

public class TrafficLightSimulation extends JFrame implements ActionListener {


// Components
private JRadioButton redButton, yellowButton, greenButton;
private JPanel lightPanel;

// Constructor
public TrafficLightSimulation() {
// Frame settings
setTitle("Traffic Light Simulation");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(new BorderLayout());

// Radio buttons for traffic lights


JPanel buttonPanel = new JPanel();
redButton = new JRadioButton("Red");
yellowButton = new JRadioButton("Yellow");
greenButton = new JRadioButton("Green");

ButtonGroup buttonGroup = new ButtonGroup();


buttonGroup.add(redButton);
buttonGroup.add(yellowButton);
buttonGroup.add(greenButton);

buttonPanel.add(redButton);
buttonPanel.add(yellowButton);
buttonPanel.add(greenButton);

add(buttonPanel, BorderLayout.SOUTH);

// Action listeners for buttons


redButton.addActionListener(this);
yellowButton.addActionListener(this);
greenButton.addActionListener(this);

// Light panel for displaying colors


lightPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw traffic lights (circles)
g.setColor(Color.BLACK);
g.fillRect(150, 50, 100, 200);

g.setColor(Color.GRAY);
g.fillOval(175, 60, 50, 50); // Red light
g.fillOval(175, 120, 50, 50); // Yellow light
g.fillOval(175, 180, 50, 50); // Green light
}
};

add(lightPanel, BorderLayout.CENTER);

setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e)
{ Graphics g = lightPanel.getGraphics();

// Reset lights to gray


g.setColor(Color.GRAY);
g.fillOval(175, 60, 50, 50); // Red light
g.fillOval(175, 120, 50, 50); // Yellow light
g.fillOval(175, 180, 50, 50); // Green light

// Change the light color based on the selected button


if (e.getSource() == redButton) {
g.setColor(Color.RED);
g.fillOval(175, 60, 50, 50); // Red light
} else if (e.getSource() == yellowButton) {
g.setColor(Color.YELLOW);
g.fillOval(175, 120, 50, 50); // Yellow light
} else if (e.getSource() == greenButton) {
g.setColor(Color.GREEN);
g.fillOval(175, 180, 50, 50); // Green light
}
}

public static void main(String[] args) {


new TrafficLightSimulation();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy