Sie sind auf Seite 1von 4

import java.awt.

*;
import java.awt.event.*;

class Calc implements ActionListener


{
static String s ="";
static String prevs = "";
static String temp="";
Frame f;
Panel p1,p2;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button b0;
Button badd;
Button bsub;
Button bmul;
Button bdiv;
Button bequ;
Button cancel;
Label lb;

Calc()
{
f = new Frame();
p1 = new Panel();
p2 = new Panel();
lb = new Label("
",Label.LEFT);
lb.setBackground(Color.white);

System.out.println("size of label" + lb.getSize());


Font font = new Font("SERIF",Font.BOLD,28);
lb.setFont(font);

p1.setBackground(Color.cyan);
p1.setSize(400,300);
p1.add(lb);
p2.setLayout(new GridLayout(4,4));

f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);

b1 = new Button("1");
b1.resize(30,30);
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
b0 = new Button("0");
badd = new Button("+");
bsub = new Button("-");
bdiv = new Button("/");
bmul = new Button("*");
bequ = new Button("=");
cancel = new Button("C");

p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b0);
p2.add(badd);
p2.add(bsub);
p2.add(bmul);
p2.add(bdiv);
p2.add(bequ);
p2.add(cancel);

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);
b0.addActionListener(this);
badd.addActionListener(this);
bsub.addActionListener(this);
bmul.addActionListener(this);
bdiv.addActionListener(this);
bequ.addActionListener(this);
cancel.addActionListener(this);

f.setVisible(true);
f.setSize(400,400);
f.setResizable(false);
}

public void actionPerformed(ActionEvent e)


{
System.out.println ("get source" +e.getSource());
Button button = (Button)e.getSource();
System.out.println (button.getLabel());
lb.setAlignment(Label.LEFT);

//lb.setText(button.getLabel());
//int a = Integer.parseInt(button.getLabel());
String but = button.getLabel();
if((but.equals("+") || but.equals("-") || but.equals("*") ||
but.equals("/")))
{

prevs =s;
System.out.println ("Previous s = " +prevs);
s="";
temp=button.getLabel();
System.out.println("Temp = " +temp);
}

else if(button.getLabel().equals("="))
{
System.out.println("I m in =(equal)");
if(temp.equals("+")) // For Addition
---------------------------
{

System.out.println ("Previous s agao = " +prevs);


int value1 =Integer.parseInt(prevs);
int value2 = Integer.parseInt(s);
value1 = value1 + value2;
prevs=Integer.toString(value1);
s=prevs;
System.out.println(value1);
lb.setText(Integer.toString(value1));
}

if(temp.equals("-")) // For Substraction -----------------------


{
System.out.println ("Previous s agao = " +prevs);
int value1 =Integer.parseInt(prevs);
int value2 = Integer.parseInt(s);
value1 = value1 - value2;
prevs=Integer.toString(value1);
s=prevs;
System.out.println(value1);
lb.setText(Integer.toString(value1));
}

if(temp.equals("*"))
{
System.out.println ("Previous s agao = " +prevs);
int value1 =Integer.parseInt(prevs);
int value2 = Integer.parseInt(s);
value1 = value1 * value2;
prevs=Integer.toString(value1);
s=prevs;
System.out.println(value1);
lb.setText(Integer.toString(value1));
}

if(temp.equals("/")) // For Division -------------------------


{
System.out.println ("Previous s agao = " +prevs);
int value1 =Integer.parseInt(prevs);
int value2 = Integer.parseInt(s);
value1 = value1 / value2;
prevs=Integer.toString(value1);
s=prevs;
System.out.println(value1);
lb.setText(Integer.toString(value1));
}

else if(button.getLabel().equals("C"))
{
s="";
prevs="";
lb.setText("");
}
else
{
s = s + button.getLabel();

lb.setText(s);
}

public static void main(String[] args)


{
Calc c = new Calc();
}
}

Das könnte Ihnen auch gefallen