Skip to content

Instantly share code, notes, and snippets.

@MohamedRamadanSaad
Created May 31, 2015 23:28
Show Gist options
  • Save MohamedRamadanSaad/f70981098612492eedcc to your computer and use it in GitHub Desktop.
Save MohamedRamadanSaad/f70981098612492eedcc to your computer and use it in GitHub Desktop.
JAVA , Calculation + GUI
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package calcu;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.lang.ArithmeticException;
// public static void main(String[] args) {
// TODO code application logic here
// public static void main(String[] args) {
// TODO code application logic here
class Calcu implements ActionListener{
//public static class ActionEvente {
//public ActionEvente() {
//}
//}
char op ;
public String res="",sh="",sh2="";
public int i=0;
JFrame f;
JPanel p;
JLabel l1;
JLabel l2;
JLabel l3;
JTextField t;
JButton b0;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JButton b5;
JButton b6;
JButton b7;
JButton b8;
JButton b9;
JButton b10;
JButton b11;
JButton b12;
JButton b13;
JButton b14;
JButton b15;
JButton b16;
JButton b17;
JButton b18;
JButton b19;
JButton b20;
JButton b21;
JButton b22;
JButton b23;
JButton b24;
JButton b25;
JButton b26;
Double c1=new Double(0);
Double c2=new Double(0);
Double c=new Double(0);
//////////////////////////////////////////////////////////////////////
public static void main(String[] args)
{
Calcu c2=new Calcu();
c2.new2();
}
// private Object ae;
//////////////////////////////////////////////////////////////////////////
void new2()
{
f=new JFrame("Calculator");
f.setBounds(30,30,370,260);
f.setVisible(true);
p=new JPanel();
f.setContentPane(p);
f.setLayout(null);
t=new JTextField();
t.setBounds(10,40,300,30);
p.add(t);
l1=new JLabel("Edit");
l1.setBounds(10,20,40,20);
p.add(l1);
l2=new JLabel("View");
l2.setBounds(60,20,40,20);
p.add(l2);
l3=new JLabel("Help");
l3.setBounds(110,20,40,20);
p.add(l3);
///////////////////////////////////////////////////////////////////////////
b0=new JButton("Backspace");
b0.setBounds(70,90,90,20);
p.add(b0);
b1=new JButton("CE");
b1.setBounds(170,90,90,20);
p.add(b1);
b2=new JButton("C");
b2.setBounds(270,90,90,20);
p.add(b2);
b3=new JButton("MC");
b3.setBounds(10,120,50,20);
p.add(b3);
b4=new JButton("7");
b4.setBounds(70,120,50,20);
p.add(b4);
b5=new JButton("8");
b5.setBounds(130,120,50,20);
p.add(b5);
b6=new JButton("9");
b6.setBounds(190,120,50,20);
p.add(b6);
b7=new JButton("/");
b7.setBounds(250,120,50,20);
p.add(b7);
b8=new JButton("sqrt");
b8.setBounds(310,120,50,20);
p.add(b8);
b9=new JButton("MR");
b9.setBounds(10,150,50,20);
p.add(b9);
b10=new JButton("4");
b10.setBounds(70,150,50,20);
p.add(b10);
b11=new JButton("5");
b11.setBounds(130,150,50,20);
p.add(b11);
b12=new JButton("6");
b12.setBounds(190,150,50,20);
p.add(b12);
b13=new JButton("*");
b13.setBounds(250,150,50,20);
p.add(b13);
b14=new JButton("%");
b14.setBounds(310,150,50,20);
p.add(b14);
b15=new JButton("MS");
b15.setBounds(10,180,50,20);
p.add(b15);
b16=new JButton("1");
b16.setBounds(70,180,50,20);
p.add(b16);
b17=new JButton("2");
b17.setBounds(130,180,50,20);
p.add(b17);
b18=new JButton("3");
b18.setBounds(190,180,50,20);
p.add(b18);
b19=new JButton("-");
b19.setBounds(250,180,50,20);
p.add(b19);
b20=new JButton("1/x");
b20.setBounds(310,180,50,20);
p.add(b20);
b21=new JButton("M+");
b21.setBounds(10,210,50,20);
p.add(b21);
b22=new JButton("0");
b22.setBounds(70,210,50,20);
p.add(b22);
b23=new JButton("+/-");
b23.setBounds(130,210,50,20);
p.add(b23);
b24=new JButton(".");
b24.setBounds(190,210,50,20);
p.add(b24);
b25=new JButton("+");
b25.setBounds(250,210,50,20);
p.add(b25);
b26=new JButton("=");
b26.setBounds(310,210,50,20);
p.add(b26);
//////////////////////////////////////////////////////////
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
b18.addActionListener(this);
b19.addActionListener(this);
b20.addActionListener(this);
b21.addActionListener(this);
b22.addActionListener(this);
b23.addActionListener(this);
b24.addActionListener(this);
b25.addActionListener(this);
b26.addActionListener(this);
}
///////////////////////////////////////////////////////////////////
@Override
//public void actionPerformed(ActionEvente ){
public void actionPerformed(ActionEvent ae) {
//ActionEvent = e;
String label=ae.getActionCommand();
if(label.equals("1"))
{
if(op=='=')
{
//t.setText("");
t.setText(t.getText().concat("1"));
}
t.setText(t.getText().concat("1"));
}
if(label.equals("C"))
{
t.setText("");
}
if(label.equals("."))
{
if(i==0)
{
t.setText(t.getText().concat("."));
i++;
}
}
if(label.equals("+/-"))
{
if(op=='=')
{
t.setText("");
// t.setText("-"+sh);
}
t.setText("-"+sh);
}
if(label.equals("CE"))
{
i=0;
sh="";
t.setText("");
}
if(label.equals("2"))
{
if(op=='=')
{
t.setText("");
//t.setText(t.getText().concat("2"));
}
t.setText(t.getText().concat("2"));
}
if(label.equals("3"))
{
if(op=='=')
{
t.setText("");
// t.setText(t.getText().concat("3"));
}
t.setText(t.getText().concat("3"));
}
if(label.equals("4"))
{
if(op=='=')
{
t.setText("");
//t.setText(t.getText().concat("3"));
}
t.setText(t.getText().concat("4"));
}
if(label.equals("5"))
{
if(op=='=')
{
t.setText("");
//t.setText(t.getText().concat("5"));
}
t.setText(t.getText().concat("5"));
}
if(label.equals("7"))
{
if(op=='=')
{
t.setText("");
//t.setText(t.getText().concat("6"));
}
t.setText(t.getText().concat("7"));
}
if(label.equals("6"))
{
if(op=='=')
{
t.setText("");
// t.setText(t.getText().concat("7"));
}
t.setText(t.getText().concat("6"));
}
if(label.equals("8"))
{
if(op=='=')
{
t.setText("");
//t.setText(t.getText().concat("8"));
}
t.setText(t.getText().concat("8"));
}
if(label.equals("9"))
{
if(op=='=')
{
t.setText("");
t.setText(t.getText().concat("9"));
}
t.setText(t.getText().concat("9"));
}
if(label.equals("0"))
{
if(op=='=')
{
t.setText("");
t.setText(t.getText().concat("0"));
}
t.setText(t.getText().concat("0"));
}
if(label.equals("+"))
{
sh=t.getText();
t.setText("");
op='+';
i=0;
}
if(label.equals("-"))
{
sh=t.getText();
t.setText("");
op='-';
i=0;
}
if(label.equals("/"))
{
sh=t.getText();
t.setText("");
op='/';
i=0;
}
if(label.equals("*"))
{
sh=t.getText();
t.setText("");
op='*';
i=0;
}
if(label.equals("%"))
{
sh=t.getText();
t.setText("");
op='%';
i=0;
}
if(label.equals("="))
{
sh2=t.getText();
c1=Double.parseDouble(sh);
c2=Double.parseDouble(sh2);
if(op=='+')
{
c=c1+c2;
res=c.toString();
t.setText(res);
op='=';
}
if(op=='-')
{
c=c1-c2;
res=c.toString();
t.setText(res);
op='=';
}
if(op=='*')
{
try{
c=c1*c2;
res=c.toString();
t.setText(res);
op='=';
}
catch(Exception NumberFormatException)
{
System.out.println("error");
}
}
if(op=='/')
{
c=c1/c2;
res=c.toString();
t.setText(res);
op='=';
}
if(op=='%')
{
c=c1%c2;
res=c.toString();
t.setText(res);
op='=';
}
}
}
//@Override
//public void actionPerformed(ActionEvent ae) {
// throw new UnsupportedOperationException("Not supported yet.");
//}
}
// public static void main(String[] args) {
// TODO code application logic here
// @Override
// public void actionPerformed(ActionEvent ae) {
// throw new UnsupportedOperationException("Not supported yet.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment