NATIONAL UNIVERSITY OF TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE
OOPS LAB
Name Ahsan Ali
Roll No F24610011
Batch 2024
Section A
Lab Number GUI
Instructor Name LEC MUNTAHA NOOR
Date of submission 9 JUNE 2025
Topic Covered GUI
1|Page
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Task1GUI extends JFrame {
public Task1GUI() {
setTitle("Task 1 GUI");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// JLabel showing name
JLabel nameLabel = new JLabel("Your Name Here");
add(nameLabel);
// JLabel displaying an image
ImageIcon imageIcon = new ImageIcon("image.jpg"); // Replace with your
image path
JLabel imageLabel = new JLabel(imageIcon);
add(imageLabel);
// JTextField and JButton
JTextField textField = new JTextField(20);
JButton printButton = new JButton("Print Text");
printButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Entered text: " + textField.getText());
}
});
add(textField);
add(printButton);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Task1GUI gui = new Task1GUI();
gui.setVisible(true);
});
}
}
OUTPUT:
2|Page
TASK 2:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Task2TemperatureConverter extends JFrame {
public Task2TemperatureConverter() {
setTitle("Temperature Converter");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JLabel promptLabel = new JLabel("Enter Fahrenheit:");
JTextField fahrenheitField = new JTextField(10);
JButton convertButton = new JButton("Convert");
JLabel resultLabel = new JLabel("Celsius: ");
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
3|Page
double fahrenheit =
Double.parseDouble(fahrenheitField.getText());
double celsius = 5.0 / 9.0 * (fahrenheit - 32);
resultLabel.setText(String.format("Celsius: %.2f",
celsius));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(Task2TemperatureConverter.th
is,
"Please enter a valid number", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
add(promptLabel);
add(fahrenheitField);
add(convertButton);
add(resultLabel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Task2TemperatureConverter converter = new
Task2TemperatureConverter();
converter.setVisible(true);
});
}
}
OUTPUT:
TASK 3:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Task3PrimeNumbers extends JFrame {
4|Page
private JTextField inputField;
private JTextArea outputArea;
public Task3PrimeNumbers() {
setTitle("Prime Numbers");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
JLabel label = new JLabel("Enter N:");
inputField = new JTextField(10);
JButton generateButton = new JButton("Generate");
generateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
generatePrimes();
}
});
topPanel.add(label);
topPanel.add(inputField);
topPanel.add(generateButton);
outputArea = new JTextArea();
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
add(topPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
}
private void generatePrimes() {
outputArea.setText("");
try {
int n = Integer.parseInt(inputField.getText());
int count = 0;
int num = 2;
while (count < n) {
if (isPrime(num)) {
outputArea.append(num + "\n");
count++;
}
num++;
}
} catch (NumberFormatException e) {
5|Page
JOptionPane.showMessageDialog(this, "Please enter a valid
integer", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private boolean isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Task3PrimeNumbers frame = new Task3PrimeNumbers();
frame.setVisible(true);
});
}
}
OUTPUT:
TASK 4:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Task4CircleCalculator extends JFrame {
private JTextField radiusField;
private JTextArea resultArea;
6|Page
public Task4CircleCalculator() {
setTitle("Circle Calculator");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JLabel promptLabel = new JLabel("Enter radius:");
radiusField = new JTextField(10);
JButton calculateButton = new JButton("Calculate");
resultArea = new JTextArea(8, 30);
resultArea.setEditable(false);
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
calculateCircle();
}
});
add(promptLabel);
add(radiusField);
add(calculateButton);
add(new JScrollPane(resultArea));
}
private void calculateCircle() {
try {
double radius = Double.parseDouble(radiusField.getText());
double diameter = 2 * radius;
double area = Math.PI * radius * radius;
double circumference = 2 * Math.PI * radius;
StringBuilder sb = new StringBuilder();
sb.append(String.format("Radius: %.2f\n", radius));
sb.append(String.format("Diameter: %.2f\n", diameter));
sb.append(String.format("Area: %.2f\n", area));
sb.append(String.format("Circumference: %.2f\n", circumference));
resultArea.setText(sb.toString());
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Please enter a valid number",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
7|Page
Task4CircleCalculator frame = new Task4CircleCalculator();
frame.setVisible(true);
});
}
}
OUTPUT:
POST LAB TASKS
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private StringBuilder currentInput;
private double result;
private String lastOperator;
private boolean startNewNumber;
public Calculator() {
currentInput = new StringBuilder();
result = 0;
lastOperator = "=";
startNewNumber = true;
setTitle("Calculator");
setSize(300, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
8|Page
setLocationRelativeTo(null);
display = new JTextField("0");
display.setEditable(false);
display.setFont(new Font("Arial", Font.BOLD, 24));
display.setHorizontalAlignment(JTextField.RIGHT);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 4, 5, 5));
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "C", "+",
"="
};
for (String text : buttons) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.BOLD, 20));
button.addActionListener(this);
buttonPanel.add(button);
}
// Add an empty label to fill the grid (since we have 17 buttons but
grid is 5x4=20)
for (int i = buttons.length; i < 20; i++) {
buttonPanel.add(new JLabel());
}
getContentPane().setLayout(new BorderLayout(5, 5));
getContentPane().add(display, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("0123456789.".contains(command)) {
if (startNewNumber) {
currentInput.setLength(0);
startNewNumber = false;
}
if (command.equals(".") && currentInput.toString().contains("."))
{
// Ignore multiple decimals
return;
9|Page
}
currentInput.append(command);
display.setText(currentInput.toString());
} else if ("+-*/".contains(command)) {
calculate(Double.parseDouble(currentInput.toString()));
lastOperator = command;
startNewNumber = true;
} else if (command.equals("=")) {
calculate(Double.parseDouble(currentInput.toString()));
lastOperator = "=";
display.setText(String.valueOf(result));
startNewNumber = true;
} else if (command.equals("C")) {
currentInput.setLength(0);
result = 0;
lastOperator = "=";
display.setText("0");
startNewNumber = true;
}
}
private void calculate(double number) {
switch (lastOperator) {
case "=":
result = number;
break;
case "+":
result += number;
break;
case "-":
result -= number;
break;
case "*":
result *= number;
break;
case "/":
if (number == 0) {
JOptionPane.showMessageDialog(this, "Cannot divide by
zero", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
result /= number;
break;
}
display.setText(String.valueOf(result));
}
public static void main(String[] args) {
10 | P a g e
SwingUtilities.invokeLater(() -> {
Calculator calc = new Calculator();
calc.setVisible(true);
});
}
}
OUTPUT:
11 | P a g e