Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- class cal extends Frame implements ActionListener{
- TextField tf;
- Button b11,b12,b13,b6,b7,b8,b,b49,b50,b9,b46,b3,b47,b45,b1,b2,b10,b4,b5,b14,b15;
- String op="";
- double num1=0.0;
- double num2=0.0;
- double num3=0.0;
- double res=0.0;
- Cal(){
- setTitle("Calculator");
- setVisible(true);
- setSize(500,200);
- Panel p1=new Panel();
- p1.setBackground(Color.red);
- Panel p2=new Panel();
- p2.setBackground(Color.magenta);
- Panel p3=new Panel();
- p3.setBackground(Color.GREEN);
- Panel p4=new Panel();
- p4.setBackground(Color.yellow);
- BorderLayout br=new BorderLayout();
- add(p1,br.NORTH);
- add(p2,br.CENTER);
- add(p3,br.EAST);
- p1.add(p4,br.NORTH);
- tf=new TextField(30);
- p1.add(tf);
- MenuBar mb=new MenuBar();
- setMenuBar(mb);
- Menu m1=new Menu("view");
- Menu m2=new Menu("Edit");
- Menu m3=new Menu("Help");
- mb.add(m1);
- mb.add(m2);
- mb.add(m3);
- MenuItem mi=new MenuItem();
- m1.add("Standerd");
- m1.add("Scentific");
- m1.add("Programmer");
- m2.add("copy");
- m2.add("paste");
- m2.add("History");
- m3.add("View Help");
- m3.add("about Calculater");
- CheckboxGroup a=new CheckboxGroup();
- Checkbox x=new Checkbox("Degrees",a,true);
- Checkbox x1=new Checkbox("Radians",a,true);
- Checkbox x2=new Checkbox("Grads",a,true);
- p4.add(x);
- p4.add(x1);
- p4.add(x2);
- Button b0=new Button("");
- b=new Button("3");
- b1=new Button("+");
- b2=new Button("-");
- b3=new Button("\u221A");
- b4=new Button("%");
- b5=new Button("*");
- b6=new Button("4");
- b7=new Button("C");
- b8=new Button("7");
- b9=new Button("8");
- b10=new Button("0");
- b11=new Button("1");
- b12=new Button("2");
- b13=new Button("6");
- b14=new Button("/");
- b15=new Button(".");
- Button b16=new Button("MC");
- Button b17=new Button("MR");
- Button b18=new Button("MS");
- Button b19=new Button("M+");
- Button b20=new Button("M-");
- Button b21=new Button("Inv");
- Button b22=new Button("In");
- Button b23=new Button("(");
- Button b24=new Button(")");
- Button b25=new Button("Int");
- Button b26=new Button("sinh");
- Button b27=new Button("sin");
- Button b28=new Button("(x*x)");
- Button b29=new Button("n!");
- Button b30=new Button("dms");
- Button b31=new Button("cosh");
- Button b32=new Button("cos");
- Button b33=new Button("y(x)");
- Button b34=new Button("y^x");
- Button b35=new Button("\u03c0");
- Button b36=new Button("tanh");
- Button b37=new Button("tan");
- Button b38=new Button("(x*x*x)");
- Button b39=new Button("3^x");
- Button b40=new Button("F-E");
- Button b41=new Button("Exp");
- Button b42=new Button("Mod");
- Button b43=new Button("Log");
- Button b44=new Button("10*x");
- b45=new Button("CE");
- b46=new Button("9");
- b47=new Button("=");
- Button b48=new Button("\u00B1");
- b49=new Button("\21");
- b50=new Button("5");
- Button b51=new Button("1/x");
- p2.add(b0);
- p2.add(b21);
- p2.add(b22);
- p2.add(b23);
- p2.add(b24);
- p2.add(b25);
- p2.add(b26);
- p2.add(b27);
- p2.add(b28);
- p2.add(b29);
- p2.add(b30);
- p2.add(b31);
- p2.add(b32);
- p2.add(b33);
- p2.add(b34);
- p2.add(b35);
- p2.add(b35);
- p2.add(b36);
- p2.add(b37);
- p2.add(b38);
- p2.add(b39);
- p2.add(b40);
- p2.add(b41);
- p2.add(b42);
- p2.add(b43);
- p2.add(b44);
- p3.add(b16);
- p3.add(b17);
- p3.add(b18);
- p3.add(b19);
- p3.add(b20);
- p3.add(b45);
- p3.add(b7);
- p3.add(b8);
- p3.add(b9);
- p3.add(b46);
- p3.add(b14);
- p3.add(b50);
- p3.add(b49);
- p3.add(b14);
- p3.add(b4);
- p3.add(b5);
- p3.add(b6);
- p3.add(b50);
- p3.add(b13);
- p3.add(b51);
- p3.add(b1);
- p3.add(b2);
- p3.add(b3);
- p3.add(b11);
- p3.add(b12);
- p3.add(b10);
- p3.add(b);
- p3.add(b10);
- p3.add(b48);
- p3.add(b15);
- p3.add(b47);
- b11.addActionListener(this);//1
- b12.addActionListener(this);//2
- b.addActionListener(this);//3
- b6.addActionListener(this);//4
- b50.addActionListener(this);//5
- b13.addActionListener(this);//6
- b8.addActionListener(this);//7
- b9.addActionListener(this);//8
- b46.addActionListener(this);//9
- b47.addActionListener(this);//=
- b1.addActionListener(this);//+
- b2.addActionListener(this);//-
- b10.addActionListener(this);//0
- b4.addActionListener(this);//%
- b5.addActionListener(this);//*
- b14.addActionListener(this);// /
- b15.addActionListener(this);//.
- b45.addActionListener(this);//CE
- b7.addActionListener(this);//b7
- b49.addActionListener(this);//<---
- b3.addActionListener(this);
- GridLayout gr=new GridLayout(5,5,2,2);
- GridLayout gr2=new GridLayout(4,5,2,2);
- p2.setLayout(gr);
- p3.setLayout(gr2);
- }
- public void actionPerformed(ActionEvent e){
- if(e.getSource().equals(b11)){
- tf.setText(tf.getText()+e.getActionCommand());
- }
- if(e.getSource().equals(b12)){
- tf.setText(tf.getText()+e.getActionCommand());
- }
- if(e.getSource().equals(b13)){
- tf.setText(tf.getText()+e.getActionCommand());
- }
- if(e.getSource().equals(b6)){
- tf.setText(tf.getText()+e.getActionCommand());
- }
- if(e.getSource().equals(b8)){
- tf.setText(tf.getText()+e.getActionCommand());
- }
- if(e.getSource().equals(b)){
- tf.setText(tf.getText()+e.getActionCommand());
- }
- if(e.getSource().equals(b50)){
- tf.setText(tf.getText()+e.getActionCommand());
- }
- if(e.getSource().equals(b9)){
- tf.setText(tf.getText()+e.getActionCommand());
- }
- if(e.getSource().equals(b46)){
- tf.setText(tf.getText()+e.getActionCommand());
- }
- if(e.getSource().equals(b10)){
- tf.setText(tf.getText()+e.getActionCommand());
- }
- if(e.getSource().equals(b1)){//+
- if(tf.getText().isEmpty()){
- tf.setText("");
- }else{
- String s=tf.getText();
- tf.setText("");
- op=e.getActionCommand();
- num1=Double.parseDouble(s);
- }
- }
- if(e.getSource().equals(b2)){//-
- if(tf.getText().isEmpty()){
- tf.setText("");
- }else{
- String s=tf.getText();
- tf.setText("");
- op=e.getActionCommand();
- num1=Double.parseDouble(s);
- }
- }
- if(e.getSource().equals(b5)){//*
- if(tf.getText().isEmpty()){
- tf.setText("");
- }else{
- String s=tf.getText();
- tf.setText("");
- op=e.getActionCommand();
- num1=Double.parseDouble(s);
- }
- }
- if(e.getSource().equals(b14)){// /
- if(tf.getText().isEmpty()){
- tf.setText("");
- }else{
- String s=tf.getText();
- tf.setText("");
- op=e.getActionCommand();
- num1=Double.parseDouble(s);
- }
- }
- if(e.getSource().equals(b15)){//.
- if(tf.getText().isEmpty()){
- tf.setText("");
- }else{
- tf.setText(tf.getText()+e.getActionCommand());
- }
- }
- if(e.getSource().equals(b45)){//CE
- tf.setText("");
- }
- if(e.getSource().equals(b7)){//C
- tf.setText("");
- }
- if(e.getSource().equals(b49)){//<--
- if(tf.getText().isEmpty()){
- tf.setText("");
- }else{
- String s=tf.getText();
- String ss=s.substring(0,s.length()-1);
- tf.setText(ss);
- }
- }
- if(e.getSource().equals(b3)){//math sqrt
- if(tf.getText().isEmpty()){
- tf.setText("");
- }else{
- String s=tf.getText();
- num1=Double.parseDouble(s);
- System.out.println(s);
- res=Math.sqrt(num1);
- tf.setText(res+"");
- }
- }
- if(e.getSource().equals(b4)){//%
- if(tf.getText().isEmpty()){
- tf.setText("");
- }else{
- String s=tf.getText();
- num2=Double.parseDouble(s);
- if(op.equals("+")){
- res=num1*num2/100;
- tf.setText(res+"");
- res=num2;
- }
- if(op.equals("-")){
- res=num1*num2/100;
- tf.setText(res+"");
- res=num2;
- }
- if(op.equals("*")){
- res=num1*num2/100;
- tf.setText(res+"");
- res=num2;
- }
- if(op.equals("/")){
- res=num2/num1;
- tf.setText(res+"");
- res=num2;
- }
- }
- }
- if(e.getSource().equals(b47)){//=
- if(tf.getText().isEmpty()){
- tf.setText("");
- }else{
- String s=tf.getText();
- num2=Double.parseDouble(s);
- if(op.equals("+")){
- res=num1+num2;
- tf.setText(res+"");
- }
- if(op.equals("-")){
- res=num1-num2;
- tf.setText(res+"");
- }
- if(op.equals("*")){
- res=num1*num2;
- tf.setText(res+"");
- }
- if(op.equals("/")){
- res=num1/num2;
- tf.setText(res+"");
- }
- }
- }
- }
- }
- public class Calculator{
- public static void main(String args[]){
- cal a=new cal();
- }
- }
Add Comment
Please, Sign In to add comment