/* Basic Calculator */ import java.awt.*; import java.awt.event.*; class MyCalculator extends Frame implements ActionListener { double x=0.0,y=0.0,z=0.0; int op=0; Panel p1,p2; Label lblR; Button btn1,btn2,btn3,btnA,btn4,btn5,btn6,btnS,btn7,btn8,btn9,btnM,btn0,btnC,btnE,btnD; public MyCalculator() { super("My Calculator"); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } } ); setSize(300,200); setResizable(false); p1=new Panel(); p2=new Panel(); lblR=new Label("0"); btn1=new Button("1"); btn2=new Button("2"); btn3=new Button("3"); btnA=new Button("+"); btn4=new Button("4"); btn5=new Button("5"); btn6=new Button("6"); btnS=new Button("-"); btn7=new Button("7"); btn8=new Button("8"); btn9=new Button("9"); btnM=new Button("*"); btn0=new Button("0"); btnC=new Button("C"); btnE=new Button("="); btnD=new Button("/"); lblR.setFont(new Font("Arial",Font.BOLD,20)); btn1.setFont(new Font("Arial",Font.BOLD,20)); btn2.setFont(new Font("Arial",Font.BOLD,20)); btn3.setFont(new Font("Arial",Font.BOLD,20)); btnA.setFont(new Font("Arial",Font.BOLD,20)); btn4.setFont(new Font("Arial",Font.BOLD,20)); btn5.setFont(new Font("Arial",Font.BOLD,20)); btn6.setFont(new Font("Arial",Font.BOLD,20)); btnS.setFont(new Font("Arial",Font.BOLD,20)); btn7.setFont(new Font("Arial",Font.BOLD,20)); btn8.setFont(new Font("Arial",Font.BOLD,20)); btn9.setFont(new Font("Arial",Font.BOLD,20)); btnM.setFont(new Font("Arial",Font.BOLD,20)); btn0.setFont(new Font("Arial",Font.BOLD,20)); btnC.setFont(new Font("Arial",Font.BOLD,20)); btnE.setFont(new Font("Arial",Font.BOLD,20)); btnD.setFont(new Font("Arial",Font.BOLD,20)); p1.setLayout(new BorderLayout()); p1.add(lblR,BorderLayout.NORTH); p2.setLayout(new GridLayout(4,4)); p2.add(btn1); p2.add(btn2); p2.add(btn3); p2.add(btnA); p2.add(btn4); p2.add(btn5); p2.add(btn6); p2.add(btnS); p2.add(btn7); p2.add(btn8); p2.add(btn9); p2.add(btnM); p2.add(btn0); p2.add(btnC); p2.add(btnE); p2.add(btnD); setLayout(new BorderLayout()); add(p1,BorderLayout.NORTH); add(p2,BorderLayout.CENTER); btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); btnA.addActionListener(this); btn4.addActionListener(this); btn5.addActionListener(this); btn6.addActionListener(this); btnS.addActionListener(this); btn7.addActionListener(this); btn8.addActionListener(this); btn9.addActionListener(this); btnM.addActionListener(this); btn0.addActionListener(this); btnC.addActionListener(this); btnE.addActionListener(this); btnD.addActionListener(this); setVisible(true); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==btn1) { x=Double.valueOf(lblR.getText()); x=x*10+1; lblR.setText(String.valueOf(x)); } if(ae.getSource()==btn2) { x=Double.valueOf(lblR.getText()); x=x*10+2; lblR.setText(String.valueOf(x)); } if(ae.getSource()==btn3) { x=Double.valueOf(lblR.getText()); x=x*10+3; lblR.setText(String.valueOf(x)); } if(ae.getSource()==btn4) { x=Double.valueOf(lblR.getText()); x=x*10+4; lblR.setText(String.valueOf(x)); } if(ae.getSource()==btn5) { x=Double.valueOf(lblR.getText()); x=x*10+5; lblR.setText(String.valueOf(x)); } if(ae.getSource()==btn6) { x=Double.valueOf(lblR.getText()); x=x*10+6; lblR.setText(String.valueOf(x)); } if(ae.getSource()==btn7) { x=Double.valueOf(lblR.getText()); x=x*10+7; lblR.setText(String.valueOf(x)); } if(ae.getSource()==btn8) { x=Double.valueOf(lblR.getText()); x=x*10+8; lblR.setText(String.valueOf(x)); } if(ae.getSource()==btn9) { x=Double.valueOf(lblR.getText()); x=x*10+9; lblR.setText(String.valueOf(x)); } if(ae.getSource()==btn0) { x=Double.valueOf(lblR.getText()); x=x*10+0; lblR.setText(String.valueOf(x)); } if(ae.getSource()==btnC) { x=Double.valueOf(lblR.getText()); x=Math.floor(x/10); lblR.setText(String.valueOf(x)); } if(ae.getSource()==btnA) { y=x; x=0.0; lblR.setText(String.valueOf(x)); op=1; } if(ae.getSource()==btnS) { y=x; x=0.0; lblR.setText(String.valueOf(x)); op=2; } if(ae.getSource()==btnM) { y=x; x=0.0; lblR.setText(String.valueOf(x)); op=3; } if(ae.getSource()==btnD) { y=x; x=0.0; lblR.setText(String.valueOf(x)); op=4; } if(ae.getSource()==btnE) { if(op==1) { z=y+x; lblR.setText(String.valueOf(z)); x=z; } if(op==2) { z=y-x; lblR.setText(String.valueOf(z)); x=z; } if(op==3) { z=y*x; lblR.setText(String.valueOf(z)); x=z; } if(op==4) { if(x!=0) { z=y/x; lblR.setText(String.valueOf(z)); x=z; } } } } public static void main(String[] args) { new MyCalculator(); } }
Search Here
Java program for creating Basic Calculator
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment