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

A Simple Calculator Using Java

This document describes a simple Java calculator program that uses a graphical user interface. It imports necessary packages and defines a class called MyFirst that extends JFrame. The class initializes GUI components like buttons, text fields and labels for the two numbers and result. It adds an event handler class that parses the user input, performs calculations based on the operation selected, and displays the result. The main method creates an instance of MyFirst, sets properties and makes the frame visible.

Uploaded by

Dinindu Kanchana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

A Simple Calculator Using Java

This document describes a simple Java calculator program that uses a graphical user interface. It imports necessary packages and defines a class called MyFirst that extends JFrame. The class initializes GUI components like buttons, text fields and labels for the two numbers and result. It adds an event handler class that parses the user input, performs calculations based on the operation selected, and displays the result. The main method creates an instance of MyFirst, sets properties and makes the frame visible.

Uploaded by

Dinindu Kanchana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

A simple Calculator Using Java

import
import
import
public

java.awt.event.*;
java.awt.*;
javax.swing.*;
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;
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;
}
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