import java.awt.*;
import java.awt.event.*;
public class Taschenrechner extends Frame
implements WindowListener,ActionListener,KeyListener
{
Label l1;
TextField t1;
Button b0, b1, b2, b3, b4, b5, b6, b7, b8, b9,
bplus, bminus, bmal, bgleich, bpunkt, bclear, bgeteilt;
boolean Rechne=true;
double Zahl = 0;
String Operation="";
public void keyPressed(keyEvent e){}
public void keyTyped(keyEvent e){}
public void keyReleased(keyEvent e){}
/**
* erstellt die Oberflaeche des Taschenrechners
**/
public Taschenrechner()
{Frame window = new Frame("");
window.addWindowListener(this);
window.setTitle("Roche");
window.setSize(165,280);
window.setLocation(200,200);
window.setBackground(Color.black);
/**
* erstellt das Anzeigefeld des Taschenrechners
**/
t1= new TextField("");
t1.setSize(125,20);
t1.setLocation(20,40);
t1.setEditable(false);
t1.setBackground(Color.white);
window.add(t1);
/**
* erstellt die Buttons (0-9) des Taschenrechners
**/
b7= new Button("7");
b7.setLocation(20,110);
window.add(b7);
b8= new Button("8");
b8.setLocation(50,100);
window.add(b8 );
b9= new Button("9");
b9.setLocation(80,110);
window.add(b9);
b4= new Button("4");
b4.setLocation(20,140);
window.add(b4);
b5= new Button("5");
b5.setLocation(50,130);
window.add(b5);
b6= new Button("6");
b6.setLocation(80,140);
window.add(b6);
b1= new Button("1");
b1.setLocation(20,170);
window.add(b1);
b2= new Button("2");
b2.setLocation(50,160);
window.add(b2);
b3= new Button("3");
b3.setLocation(80,170);
window.add(b3);
b0= new Button("0");
b0.setLocation(50,190);
window.add(b0);
/**
* erstellt die Operator-Buttons (C, +, -, *, /, =, .) des Taschenrechners
**/
bclear= new Button("C");
bclear.setLocation(115,70);
window.add(bclear);
bplus= new Button("+");
bplus.setLocation(115,190);
window.add(bplus);
bminus= new Button("-");
bminus.setLocation(115,130);
window.add(bminus);
bpunkt= new Button(".");
bpunkt.setLocation(20,220);
window.add(bpunkt);
bgleich= new Button("=");
bgleich.setLocation(52,220);
window.add(bgleich);
bmal= new Button("*");
bmal.setLocation(115,160);
window.add(bmal);
bgeteilt= new Button("/");
bgeteilt.setLocation(115,100);
window.add(bgeteilt);
/**
* Actionlistener
**/
b9.addActionListener(this);
b8.addActionListener(this);
b7.addActionListener(this);
b6.addActionListener(this);
b5.addActionListener(this);
b4.addActionListener(this);
b3.addActionListener(this);
b2.addActionListener(this);
b1.addActionListener(this);
b0.addActionListener(this);
bgeteilt.addActionListener(this);
bmal.addActionListener(this);
bgleich.addActionListener(this);
bpunkt.addActionListener(this);
bminus.addActionListener(this);
bplus.addActionListener(this);
bclear.addActionListener(this);
/**
* Größe der Buttons
**/
b9.setSize(30,25);
b8.setSize(30,25);
b7.setSize(30,25);
b6.setSize(30,25);
b5.setSize(30,25);
b4.setSize(30,25);
b3.setSize(30,25);
b2.setSize(30,25);
b1.setSize(30,25);
b0.setSize(30,25);
bgeteilt.setSize(30,25);
bmal.setSize(30,25);
bgleich.setSize(60,25);
bpunkt.setSize(30,25);
bminus.setSize(30,25);
bplus.setSize(30,55);
bclear.setSize(20,15);
/**
* Farbe der Buttons
**/
b9.setBackground(Color.white);
b8.setBackground(Color.white);
b7.setBackground(Color.white);
b6.setBackground(Color.white);
b5.setBackground(Color.white);
b4.setBackground(Color.white);
b3.setBackground(Color.white);
b2.setBackground(Color.white);
b1.setBackground(Color.white);
b0.setBackground(Color.white);
bclear.setBackground(Color.red);
bgeteilt.setBackground(Color.red);
bmal.setBackground(Color.red);
bgleich.setBackground(Color.red);
bpunkt.setBackground(Color.red);
bminus.setBackground(Color.red);
bplus.setBackground(Color.red);
/**
* macht die Oberfläche des Taschenrechners sichtbar
**/
l1= new Label("");window.add(l1);
window.setVisible(true);
}//Ende Taschenrechner
/**
* Wird aufgerufen, wenn das Fenster geschlossen wird
**/
public void windowClosing(WindowEvent e)
{System.exit(0);}
/**
* Wird aufgerufen, wenn das Fenster aktiviert wird
**/
public void windowActivated(WindowEvent e){}
/**
* Wird aufgerufen, wenn das Fenster mit dispose() geschlossen wurde
**/
public void windowClosed(WindowEvent e){}
/**
* Wird aufgerufen, wenn das Fenster deaktiviert wird
**/
public void windowDeactivated(WindowEvent e){}
/**
* Wird aufgerufen, wenn das Fenster wieder hochgeholt wird
**/
public void windowDeiconified(WindowEvent e){}
/**
*Wird aufgerufen, wenn das Fenster zum Icon verkleinert wird
**/
public void windowIconified(WindowEvent e){}
/**
*Wird aufgerufen, wenn Fenster geöffnet wurde
**/
public void windowOpened(WindowEvent e){}
/**
* Erkennung von Eingaben
**/
public void actionPerformed(ActionEvent ea)
{
int h;
String inh = new String();
String but=ea.getActionCommand();
if (but=="0"){ziffer("0");}
else if (but=="1"){ziffer("1");}
else if (but=="2"){ziffer("2");}
else if (but=="3"){ziffer("3");}
else if (but=="4"){ziffer("4");}
else if (but=="5"){ziffer("5");}
else if (but=="6"){ziffer("6");}
else if (but=="7"){ziffer("7");}
else if (but=="8"){ziffer("8");}
else if (but=="9"){ziffer("9");}
else if (but=="+"){verkn("+");}
else if (but=="-"){verkn("-");}
else if (but=="*"){verkn("*");}
else if (but=="/"){verkn("/");}
else if (but==".")
{
if (Rechne) {t1.setText("0.");Rechne = false;}
else
{
inh = new String(t1.getText());
h = inh.indexOf(".");
if (h < 0) {t1.setText(t1.getText() + ".");}
}//else
}
else if (but=="C")
{
t1.setText("");
Rechne=true;
Zahl=0;
Operation="";
}
else if (but=="="){verkn("");}
}//Ende actionPerformed
/**
* schreib in Display
**/
public void ziffer(String s)
{
if (Rechne) {t1.setText(s);Rechne = false;}
else
{
String inh = new String(t1.getText());
/**zähle die Punkte**/
int h = inh.indexOf(".");
/**bei 6 ohne punkt oder 25 mit punkt**/
if (((h<0)&&(inh.length()<6)) || ((h>0)&&(inh.length()<25))) t1.setText(t1.getText() + s);
else return;
}
}//Ende ziffer
/**
* durchführung der Rechnung
**/
public void verkn(String s)
{
Double wert;
Rechne = true;
try
{
if (Operation=="")
{
wert=new Double(t1.getText());
Zahl= wert.doubleValue();
Operation=s;
}
else
{
wert=new Double(t1.getText());
if (Operation=="+")
{Zahl=Zahl+wert.doubleValue();}
else if (Operation=="-")
{Zahl=Zahl-wert.doubleValue();}
else if (Operation=="*")
{Zahl=Zahl*wert.doubleValue();}
else if (Operation=="/")
{Zahl=Zahl/wert.doubleValue();}
Operation=s;
t1.setText(s.valueOf(Zahl));
}
}//try
catch (ArithmeticException e)
{
t1.setText("Error:" + e );ziffer("");
}
catch (Throwable e)
{
t1.setText("Fehler:" + e );ziffer("");
}
}//Ende verkn
/**
* main
**/
public static void main(String[]args)
{
Taschenrechner meinFenster=new Taschenrechner();
}//Ende main
}