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

A Simple Calculator Using Java

This document describes how to create a simple calculator application in Java using a graphical user interface. It includes text fields for inputting numbers, buttons for arithmetic operations, and a label to display results. Event handling is implemented to detect button clicks and perform calculations.

Uploaded by

Nimsara De Zoysa
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)
34 views

A Simple Calculator Using Java

This document describes how to create a simple calculator application in Java using a graphical user interface. It includes text fields for inputting numbers, buttons for arithmetic operations, and a label to display results. Event handling is implemented to detect button clicks and perform calculations.

Uploaded by

Nimsara De Zoysa
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/ 3

A simple Calculator Using Java

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class MyFirst extends JFrame {
JButton add,sub, mul,div;
JTextField num1,num2;
JLabel result, enter1, enter2;

public MyFirst(){
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
enter1 = new JLabel("frist");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=0;
add(enter1,c);

num1 = new JTextField(12);


c.fill =GridBagConstraints.HORIZONTAL;
c.gridx=1;
c.gridy=0;
c.gridwidth = 3;
add(num1,c);

enter2 = new JLabel("second");


c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=0;
c.gridy=1;
c.gridwidth=1;
add(enter2,c);

num2 = new JTextField(12);


c.fill =GridBagConstraints.HORIZONTAL;
c.gridx=1;
c.gridy=1;
c.gridwidth = 3;
add(num2,c);

add = new JButton("+");


c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=1;
c.gridy=4;
c.gridwidth=1;
add(add,c);

//Java GUI Tutorial 13 - Simple calculator (Part 1 of 4)

sub = new JButton("-");


c.fill = GridBagConstraints.HORIZONTAL;
c.gridx= 2;
c.gridy=4;
c.gridwidth=1;

1
add(sub,c);

mul = new JButton("*");


c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=3;
c.gridy=4;
c.gridwidth=1;
add(mul,c);

div = new JButton("/");


c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=4;
c.gridy=4;
c.gridwidth=1;
add(div,c);

result = new JLabel("");


c.fill = GridBagConstraints.HORIZONTAL;
c.gridx=1;
c.gridy=10;
c.gridwidth=4;
add(result,c);

event a = new event();


add.addActionListener(a);
sub.addActionListener(a);
mul.addActionListener(a);
div.addActionListener(a);

public class event implements ActionListener{


public void actionPerformed(ActionEvent a){
double number1,number2;
try{
number1 = Double.parseDouble(num1.getText());

}catch(NumberFormatException e){
result.setText("illegal data to in the frist feild");
result.setForeground(Color.red);
return;
}

try{
number2 = Double.parseDouble(num2.getText());

}catch(NumberFormatException e){
result.setText("illegal data to in the frist feild");
result.setForeground(Color.RED);
return;
}

2
String op =a.getActionCommand();
if(op.equals("+")){
double sum = number1 + number2;
result.setText(""+ sum);
result.setForeground(Color.RED);
}
else if(op.equals("-")){
double diff = number1 - number2;
result.setText(""+diff);
result.setForeground(Color.RED);
}
else if(op.equals("*")){
double factor = number1 * number2;
result.setText(""+factor);
result.setForeground(Color.RED);
}
else if(op.equals("/")){
if(number2==0){
result.setText("Cannot devide number by 0");
result.setForeground(Color.RED);
}else{

double quatient = number1 / number2;


result.setText(""+quatient);
result.setForeground(Color.RED);
}

}
public static void main(String args[]){
MyFirst gui = new MyFirst();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(500,500);
gui.setTitle("My first program");

}
}

You might also like