0% found this document useful (0 votes)
4 views

ooplab910

The document is a lab manual for Object Oriented Programming, specifically detailing Lab Task 9 and 10, authored by Abdul Samad. It includes Java code for a graphical user interface (GUI) of a simple calculator that performs basic arithmetic operations such as addition, subtraction, multiplication, and division. The code outlines the initialization of components and action listeners for the calculator's buttons and text fields.

Uploaded by

mjoy14129
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)
4 views

ooplab910

The document is a lab manual for Object Oriented Programming, specifically detailing Lab Task 9 and 10, authored by Abdul Samad. It includes Java code for a graphical user interface (GUI) of a simple calculator that performs basic arithmetic operations such as addition, subtraction, multiplication, and division. The code outlines the initialization of components and action listeners for the calculator's buttons and text fields.

Uploaded by

mjoy14129
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/ 10

Object Oriented Programming (OOP)

Lab Manual
Lab Task 9+10
Name: Abdul Samad
Roll Number: 39337

1
GUI for Simple Calculator
Code:
public class GUI extends javax.swing.JPanel {

/**

* Creates new form GUI

*/

public GUI() {

initComponents();

/**

* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jTextField1 = new javax.swing.JTextField();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

a = new javax.swing.JTextField();

b = new javax.swing.JTextField();

jLabel4 = new javax.swing.JLabel();

add = new javax.swing.JButton();

jButton1 = new javax.swing.JButton();

2
jButton3 = new javax.swing.JButton();

jButton4 = new javax.swing.JButton();

c = new javax.swing.JTextField();

jTextField1.setText("jTextField1");

jLabel1.setText("Number 1");

jLabel2.setText("Number 2");

b.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

bActionPerformed(evt);

});

jLabel4.setText("Result");

add.setText("+");

add.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

addActionPerformed(evt);

});

jButton1.setText("-");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

3
}

});

jButton3.setText("*");

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt);

});

jButton4.setText("/");

jButton4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton4ActionPerformed(evt);

});

c.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

cActionPerformed(evt);

});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

this.setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

4
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel4)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel1)

.addComponent(jLabel2)))

.addGap(37, 37, 37)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(b)

.addComponent(a)

.addComponent(c))

.addContainerGap())

.addGroup(layout.createSequentialGroup()

.addGap(51, 51, 51)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(jButton3)

.addGap(42, 42, 42)

.addComponent(jButton4))

.addGroup(layout.createSequentialGroup()

.addComponent(add)

.addGap(37, 37, 37)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 105,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(162, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

5
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(a, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(b, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(31, 31, 31)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel4)

.addComponent(c, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(58, 58, 58)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(add)

.addComponent(jButton1))

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton3)

.addComponent(jButton4))

.addContainerGap(32, Short.MAX_VALUE))

);

}// </editor-fold>

private void bActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

6
private void addActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

int num1=Integer.parseInt(a.getText());

int num2=Integer.parseInt(b.getText());

int num3=num1+num2;

c.setText(""+num3);

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

int num1=Integer.parseInt(a.getText());

int num2=Integer.parseInt(b.getText());

int num3=num1-num2;

c.setText(""+num3); // TODO add your handling code here:

private void cActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

int num1=Integer.parseInt(a.getText());

int num2=Integer.parseInt(b.getText());

int num3=num1*num2;

c.setText(""+num3); // TODO add your handling code here:

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

int num1=Integer.parseInt(a.getText());

7
int num2=Integer.parseInt(b.getText());

int num3=num1/num2;

c.setText(""+num3); // TODO add your handling code here:

// Variables declaration - do not modify

private javax.swing.JTextField a;

private javax.swing.JButton add;

private javax.swing.JTextField b;

private javax.swing.JTextField c;

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton3;

private javax.swing.JButton jButton4;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel4;

private javax.swing.JTextField jTextField1;

// End of variables declaration

8
Screenshots:

9
10

You might also like