Grundlagen der Programmierung 2 SS 2005 - Datei Calculator.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame
{
// Konstanten für die 6 verschiedene Zustände
public final static int INIT_STATE = 0;
public final static int FIRST_NUMBER_STATE = 1;
public final static int NEXT_NUMBER_BEGIN_STATE = 2;
public final static int NEXT_NUMBER_END_STATE = 3;
public final static int RESULT_STATE = 4;
public final static int CLOSE_STATE = 5;
// Variable zum Speichern des aktuellen Zustandes
private int state = INIT_STATE;
// Arrays die angeben welche Buttons im jeweiligen Zustand aktiviert sind
boolean[][] buttonStateArray = {{ false,false,false,false,true, true, true, true,
true, true, true, true, true, true, false,false},
{true ,true ,true ,true ,true, true, true, true,
true, true, true, true, true, true, true, true },
{false,false,false,false,true, true, true, true,
true, true, true, true, true, true, false,true },
{false,false,false,false,true, true, true, true,
true, true, true, true, true, true, true, true },
{true, true, true, true, false,false,false,false,
false,false,false,false,false,false,false,true }};
// Rechen-Operatoren
private static final int PLUS_OPERATOR = 0;
private static final int MINUS_OPERATOR = 1;
private static final int MUL_OPERATOR = 2;
private static final int DIV_OPERATOR = 3;
// Variabeln fuer Zwischenstaende, Endergebnisse und Operatoren
private int value, result, operator;
// Textfeld erzeugen
private JTextField textField = new JTextField();
// Button Beschriftung
private String[] buttonLabelArray = {"+","-","*","/","1","2","3","4","5","6","7","8","9","0","=","C"};
// 16 Buttons des Taschenrechners
private JButton[] buttonArray = new JButton [16];
// Taschenrechner Konstruktor
public Calculator()
{
super("Calculator");
// Schließverhalten des Fensters festlegen
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Listener fuer die Buttons
class buttonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// ermitteln welcher Button gedrueckt wurde
String command = event.getActionCommand();
// bei einer Zahl wird nachfolgender Code ausgefuehrt
if (command == "0" || command == "1" ||
command == "2" || command == "3" ||
command == "4" || command == "5" ||
command == "6" || command == "7" ||
command == "8" || command == "9")
{
textField.setText(textField.getText() + command);
value = Integer.parseInt(textField.getText());
// Zustand in Abhaengigkeit vom aktuellen Zustand wechseln
switch (state)
{
case INIT_STATE:
toState(FIRST_NUMBER_STATE);
break;
case FIRST_NUMBER_STATE:
toState(FIRST_NUMBER_STATE);
break;
case NEXT_NUMBER_BEGIN_STATE:
toState(NEXT_NUMBER_END_STATE);
break;
case NEXT_NUMBER_END_STATE:
toState(NEXT_NUMBER_END_STATE);
break;
}
}
// bei einem Operator wird nachfolgender Code ausgefuehrt
if (command == "+" || command == "-" ||
command == "*" || command == "/")
{
result = value;
// Operater bis nach der 2. Eingabe einer Zahl merken
if (command == "+") operator = PLUS_OPERATOR;
if (command == "-") operator = MINUS_OPERATOR;
if (command == "*") operator = MUL_OPERATOR;
if (command == "/") operator = DIV_OPERATOR;
// Zustand in Abhaengigkeit vom aktuellen Zustand wechseln
switch (state)
{
case FIRST_NUMBER_STATE:
toState(NEXT_NUMBER_BEGIN_STATE);
break;
case NEXT_NUMBER_BEGIN_STATE:
toState(NEXT_NUMBER_END_STATE);
break;
case RESULT_STATE:
toState(NEXT_NUMBER_BEGIN_STATE);
break;
}
}
// bei einem "=" wird das Ergebnis ausgerechnet
if (command == "=")
{
// Textfeld in die Eingabe Variable speichern
value = Integer.parseInt(textField.getText());
// Berechnung in Abhaengigkeit von der Operation
switch (operator)
{
case PLUS_OPERATOR:
result = result + value;
break;
case MINUS_OPERATOR:
result = result - value;
break;
case MUL_OPERATOR:
result = result * value;
break;
case DIV_OPERATOR:
result = result / value;
break;
}
// fuer das weitere Rechnen wird das Ergebnis in die Eingabe gespeichert
value = result;
// Zustand wechseln
toState(RESULT_STATE);
}
/* beim Loeschen Kommando (Taste "C") werden alle bisherigen Eingaben
geloescht und es wird in den initialen Zustand gewechselt */
if (command == "C")
toState(INIT_STATE);
}
}
// Fenstergroesse festlegen
setSize(180,240);
// Textfeld
textField.setText("");
textField.setVisible(true);
textField.setEnabled(false);
// Panel für die Button erzeugen
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(4,4));
// Layout festlegen und Komponenten hinzufuegen
Container content = getContentPane();
content.setLayout(new BorderLayout());
content.add(centerPanel, BorderLayout.CENTER);
content.add(textField, BorderLayout.NORTH);
// Buttons mit Labeln und ActionListener generieren
for (int i = 0; i<16; i++)
{
buttonArray[i] = new JButton(buttonLabelArray[i]);
centerPanel.add(buttonArray[i]);
buttonArray[i].addActionListener(new buttonActionListener());
}
// Fenster anzeigen
setVisible(true);
// ersten Zustand setzen
toState(INIT_STATE);
}
// Methode zum Wechseln der Zustaende
public void toState (int nextState)
{
// TODO: Hier Code für Zustandsübergänge einfügen.
state = nextState;
switch (state)
{
case INIT_STATE:
value = 0;
result = 0;
operator = PLUS_OPERATOR;
textField.setText("");
break;
case FIRST_NUMBER_STATE:
break;
case NEXT_NUMBER_BEGIN_STATE:
textField.setText("");
break;
case NEXT_NUMBER_END_STATE:
break;
case RESULT_STATE:
textField.setText(String.valueOf(result));
break;
case CLOSE_STATE:
System.exit(0);
break;
}
// fuer alle Zustandswechsel wird die Darstellung der Buttons neu gesetzt
for (int i = 0; i < 16; i++)
buttonArray[i].setEnabled(buttonStateArray[state][i]);
}
//Main Methode erzeugt ein Frame
public static void main(String[] args) {
JFrame calculatorFrame = new Calculator();
}
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 13.05.2005


