Sie sind auf Seite 1von 6

import java.awt.

*;
import java.awt.event.*;
class Calc extends WindowAdapter 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.L
EFT);
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);
f.addWindowListener(this); // Add windows listner to frame
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);
for(int i=0;i<300;i++)
{
f.setBounds(300,250,i-1,i);
try
{
Thread.sleep(1);
}
catch(Exception e){}
}
f.setTitle("Welcome to my Calculator");
f.setVisible(true);
// f.setSize(300,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());
System.out.println (button.getColorModel());

// button.setBackground());
String but = button.getLabel();
if((but.equals("+") || but.equals("-") || but.equals("*") || but
.equals("/")))
{
if(!(prevs.equals("")) && !(s.equals("")))
{
if(temp.equals("+"))
{
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(valu
e1));
}
if(temp.equals("-")) // For Substractio
n -----------------------
{
System.out.println ("Previous s
agao = " +prevs);
int value1 =Integer.parseInt(pre
vs);
int value2 = Integer.parseInt(s)
;
value1 = value1 - value2;
prevs=Integer.toString(value1);
s=prevs;
System.out.println(value1);
lb.setText(Integer.toString(valu
e1));
}
if(temp.equals("*")) // For mu
ltiplication................
{
System.out.println ("Previous s
agao = " +prevs);
int value1 =Integer.parseInt(pre
vs);
int value2 = Integer.parseInt(s)
;
value1 = value1 * value2;
prevs=Integer.toString(value1);
s=prevs;
System.out.println(value1);
lb.setText(Integer.toString(valu
e1));
}
if(temp.equals("/")) // For Division -
------------------------
{
System.out.println ("Previous s
agao = " +prevs);
int value1 =Integer.parseInt(pre
vs);
int value2 = Integer.parseInt(s)
;
value1 = value1 / value2;
prevs=Integer.toString(value1);
s=prevs;
System.out.println(value1);
lb.setText(Integer.toString(valu
e1));
}
}
if(!(s.equals("")))
{
prevs =s;
System.out.println ("Previous s = " +prevs);
s="";
}
temp=button.getLabel();
System.out.println("Temp = " +temp);

}
else if(button.getLabel().equals("="))
{
if(!(prevs.equals("")) && !(s.equals("")))
{
System.out.println("I m in =(equal)");
if(temp.equals("+")) // For Addition -
--------------------------
{
System.out.println ("Previous s
agao = " +prevs);
int value1 =Integer.parseInt(pre
vs);
int value2 = Integer.parseInt(s)
;
value1 = value1 + value2;
prevs=Integer.toString(value1);
// s=prevs;
s="";
System.out.println(value1);
lb.setText(Integer.toString(valu
e1));
}
if(temp.equals("-")) // For Substractio
n -----------------------
{
System.out.println ("Previous s
agao = " +prevs);
int value1 =Integer.parseInt(pre
vs);
int value2 = Integer.parseInt(s)
;
value1 = value1 - value2;
prevs=Integer.toString(value1);
// s=prevs;
s="";
System.out.println(value1);
lb.setText(Integer.toString(valu
e1));
}
if(temp.equals("*")) // For multiplica
tion................
{
System.out.println ("Previous s
agao = " +prevs);
int value1 =Integer.parseInt(pre
vs);
int value2 = Integer.parseInt(s)
;
value1 = value1 * value2;
prevs=Integer.toString(value1);
//s=prevs;
s="";
System.out.println(value1);
lb.setText(Integer.toString(valu
e1));
}
if(temp.equals("/")) // For Division -
------------------------
{
System.out.println ("Previous s
agao = " +prevs);
int value1 =Integer.parseInt(pre
vs);
int value2 = Integer.parseInt(s)
;
value1 = value1 / value2;
prevs=Integer.toString(value1);
s=prevs;
s="";
System.out.println(value1);
lb.setText(Integer.toString(valu
e1));
}
}
}
else if(button.getLabel().equals("C"))
{
s="";
prevs="";
lb.setText("");
}
else
{
if(s.equals(""))
s = s + button.getLabel();

lb.setText(s);
}

}
public void windowActivated(WindowEvent we)
{
System.out.println("------------------------- Windows Activated-
-----------------");
}
public void windowOpened(WindowEvent we)
{
System.out.println("------------------------- Windows Opened----
--------------");
}
public void windowClosing(WindowEvent we)
{
System.out.println("------------------------- Windows Closed----
--------------");
//f.dispose();
System.out.println (we.getWindow());
Frame f =(Frame)we.getWindow();
f.dispose();

}
public static void main(String[] args)
{
Calc c = new Calc();
}
}

Das könnte Ihnen auch gefallen