package taschenrechner;
import java.awt.*;
import java.awt.event.*;
public class Taschenrechner extends Frame implements ActionListener {
//Attribute
String sOperator;
private boolean btest3 = false;
private boolean btest = false;
private boolean btest2 = false;
private TextField tfDisplay1 = new TextField(15);
private TextField tfDisplay2 = new TextField(15);
private Label lbPunkt = new Label(".");
private Label lbnewLabel1 = new Label();
private Label lbnewLabel2 = new Label();
private Label lbnewLabel3 = new Label();
private Label lbnewLabel4 = new Label();
private Label lbnewLabel5 = new Label();
private Label lbnewLabel6 = new Label();
private Button bt1 = new Button("1");
private Button bt2 = new Button("2");
private Button bt3 = new Button("3");
private Button bt4 = new Button("4");
private Button bt5 = new Button("5");
private Button bt6 = new Button("6");
private Button bt7 = new Button("7");
private Button bt8 = new Button("8");
private Button bt9 = new Button("9");
private Button bt0 = new Button("0");
private Button btPunkt = new Button(".");
private Button btPlus = new Button("+");
private Button btMinus = new Button("-");
private Button btMal = new Button("*");
private Button btGeteilt = new Button("/");
private Button btErgebnis = new Button("=");
private Button btDelete = new Button("DELETE");
//Konstruktur
public Taschenrechner() {
//Größe des Fensters
this.setSize(500, 500);
//Hintergrundfarbe
this.setBackground(Color.WHITE);
//Title erstellen
this.setTitle("Taschenrechner");
//Layout
this.setLayout(new GridLayout(6, 4, 10, 10));
//Dialogelemente
this.add(tfDisplay1);
this.add(lbPunkt);
this.add(tfDisplay2);
this.add(lbnewLabel1);
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.add(btPlus);
this.add(bt4);
this.add(bt5);
this.add(bt6);
this.add(btMinus);
this.add(bt7);
this.add(bt8);
this.add(bt9);
this.add(btMal);
this.add(bt0);
this.add(lbnewLabel2);
this.add(btPunkt);
this.add(btGeteilt);
this.add(lbnewLabel4);
this.add(btDelete);
this.add(lbnewLabel6);
this.add(btErgebnis);
//TextFields nicht editierbar machen
tfDisplay1.setEditable(false);
tfDisplay2.setEditable(false);
//Buttons uebertragen
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt0.addActionListener(this);
btPunkt.addActionListener(this);
btPlus.addActionListener(this);
btMinus.addActionListener(this);
btMal.addActionListener(this);
btGeteilt.addActionListener(this);
btErgebnis.addActionListener(this);
//Sichtbarkeit des Fensters
this.setVisible(true);
//Schließen des Fensters
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
String sTest = new String();
String sTest2 = new String();
String sErgebniss = new String();
double dErgebnis;
double dTest;
double dTest2;
double dErgebnis2;
if (e.getActionCommand().equals("+")
|| e.getActionCommand().equals("-")
|| e.getActionCommand().equals("/")
|| e.getActionCommand().equals("*")) {
if (btest3 == false) {
btest2 = true;
sOperator = e.getActionCommand();
btest3 = true;
}
{
btest2 = true;
sOperator = e.getActionCommand();
}
} else if (btest2 == true) {
sTest = tfDisplay2.getText();
tfDisplay2.setText(sTest + e.getActionCommand());
} else if (btest2 != true) {
sTest = tfDisplay1.getText();
tfDisplay1.setText(sTest + e.getActionCommand());
} else if (e.getActionCommand().equals("=")) {
btest2 = false;
sTest = tfDisplay1.getText();
sTest2 = tfDisplay2.getText();
dTest = Double.parseDouble(sTest);
dTest2 = Double.parseDouble(sTest2);
if (sOperator == "+") {
dErgebnis = dTest + dTest2;
}
if (sOperator == "-") {
dErgebnis = dTest - dTest2;
}
if (sOperator == "*") {
dErgebnis = dTest * dTest2;
}
if (sOperator == "/") {
dErgebnis = dTest / dTest2;
}
}
}
}